From 5f34f7802796ce2a64b7753c4d3dccf6e53652e5 Mon Sep 17 00:00:00 2001 From: savitaashture Date: Thu, 3 Aug 2023 18:18:15 +0530 Subject: [PATCH] Fix branch matching when there is a slashes in the base branch name --- pkg/matcher/annotation_matcher.go | 20 ++++++++++------- pkg/matcher/annotation_matcher_test.go | 30 ++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/pkg/matcher/annotation_matcher.go b/pkg/matcher/annotation_matcher.go index 94a053f09..b9eafd648 100644 --- a/pkg/matcher/annotation_matcher.go +++ b/pkg/matcher/annotation_matcher.go @@ -25,18 +25,22 @@ const ( ) func branchMatch(prunBranch, baseBranch string) bool { - // if target is refs/heads/.. and base is without ref (for pullRequest) - if strings.HasPrefix(prunBranch, "refs/heads") && !strings.Contains(baseBranch, "/") { - ref := "refs/heads/" + baseBranch + // if target is refs/heads/.. and base is without ref (for pullRequest action) + if strings.HasPrefix(prunBranch, "refs/heads/") { + ref := baseBranch + if !strings.HasPrefix(baseBranch, "refs/heads/") { + ref = "refs/heads/" + baseBranch + } g := glob.MustCompile(prunBranch) if g.Match(ref) { return true } - } - - // if base is refs/heads/.. and target is without ref (for push rerequested action) - if strings.HasPrefix(baseBranch, "refs/heads") && !strings.Contains(prunBranch, "/") { - prunRef := "refs/heads/" + prunBranch + } else { + // if base is refs/heads/.. and target is without ref (for push request action) + prunRef := prunBranch + if !strings.HasPrefix(prunBranch, "refs/heads/") { + prunRef = "refs/heads/" + prunBranch + } g := glob.MustCompile(prunRef) if g.Match(baseBranch) { return true diff --git a/pkg/matcher/annotation_matcher_test.go b/pkg/matcher/annotation_matcher_test.go index c798dd665..24194f938 100644 --- a/pkg/matcher/annotation_matcher_test.go +++ b/pkg/matcher/annotation_matcher_test.go @@ -833,6 +833,16 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) { }, } + pipelineWithSlashInBranchName := &tektonv1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pipeline-withslashesinbranch", + Annotations: map[string]string{ + keys.OnEvent: "[pull_request, push]", + keys.OnTargetBranch: "[test/main]", + }, + }, + } + pipelineRefAll := &tektonv1.PipelineRun{ ObjectMeta: metav1.ObjectMeta{ Name: "pipeline-other", @@ -1061,7 +1071,7 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) { wantErr: false, }, { - name: "not-match-push-branch-matching", + name: "branch-matching-doesnot-match-for-push-event", args: args{ runevent: info.Event{TriggerTarget: "push", EventType: "push", BaseBranch: "refs/heads/someothername/then/main"}, pruns: []*tektonv1.PipelineRun{pipelineGood, pipelinePush}, @@ -1069,13 +1079,29 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) { wantErr: true, }, { - name: "not-match-pull-request-branch-matching", + name: "branch-matching-doesnot-match-for-pull-request", args: args{ runevent: info.Event{TriggerTarget: "pull_request", EventType: "pull_request", BaseBranch: "someothername/then/main"}, pruns: []*tektonv1.PipelineRun{pipelineGood, pipelinePush}, }, wantErr: true, }, + { + name: "branch-matching-match-for-push-when-there-are-slashes-in-between-branch-name", + args: args{ + runevent: info.Event{TriggerTarget: "push", EventType: "push", BaseBranch: "refs/heads/test/main"}, + pruns: []*tektonv1.PipelineRun{pipelineWithSlashInBranchName}, + }, + wantErr: false, + }, + { + name: "branch-matching-match-for-pull_request-when-there-are-slashes-in-between-branch-name", + args: args{ + runevent: info.Event{TriggerTarget: "pull_request", EventType: "pull_request", BaseBranch: "refs/heads/test/main"}, + pruns: []*tektonv1.PipelineRun{pipelineWithSlashInBranchName}, + }, + wantErr: false, + }, } for _, tt := range tests {