From 8b60a3ba42d9a45fc45a5cf1d95aeddcad5ecf25 Mon Sep 17 00:00:00 2001 From: savitaashture Date: Wed, 19 Jul 2023 20:17:28 +0530 Subject: [PATCH] Fix branch matching based on base name --- pkg/matcher/annotation_matcher.go | 7 ------- pkg/matcher/annotation_matcher_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/pkg/matcher/annotation_matcher.go b/pkg/matcher/annotation_matcher.go index 25989d576..94a053f09 100644 --- a/pkg/matcher/annotation_matcher.go +++ b/pkg/matcher/annotation_matcher.go @@ -3,7 +3,6 @@ package matcher import ( "context" "fmt" - "path/filepath" "regexp" "strings" @@ -26,12 +25,6 @@ const ( ) func branchMatch(prunBranch, baseBranch string) bool { - // If we have targetBranch in annotation and refs/heads/targetBranch from - // webhook, then allow it. - if filepath.Base(baseBranch) == filepath.Base(prunBranch) { - return true - } - // 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 diff --git a/pkg/matcher/annotation_matcher_test.go b/pkg/matcher/annotation_matcher_test.go index da39818d5..c2422074b 100644 --- a/pkg/matcher/annotation_matcher_test.go +++ b/pkg/matcher/annotation_matcher_test.go @@ -813,6 +813,16 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) { }, } + pipelinePush := &tektonv1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pipeline-push", + Annotations: map[string]string{ + keys.OnEvent: "[push]", + keys.OnTargetBranch: "[main]", + }, + }, + } + pipelineOther := &tektonv1.PipelineRun{ ObjectMeta: metav1.ObjectMeta{ Name: "pipeline-other", @@ -1050,6 +1060,22 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) { }, wantErr: false, }, + { + name: "not-match-push-branch-matching", + args: args{ + runevent: info.Event{TriggerTarget: "push", EventType: "push", BaseBranch: "refs/heads/someothername/then/main"}, + pruns: []*tektonv1.PipelineRun{pipelineGood, pipelinePush}, + }, + wantErr: true, + }, + { + name: "not-match-pull-request-branch-matching", + args: args{ + runevent: info.Event{TriggerTarget: "pull_request", EventType: "pull_request", BaseBranch: "someothername/then/main"}, + pruns: []*tektonv1.PipelineRun{pipelineGood, pipelinePush}, + }, + wantErr: true, + }, } for _, tt := range tests {