Skip to content

Commit bb75a63

Browse files
committed
Fix branch matching based on base name (#1352)
1 parent 306fd47 commit bb75a63

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

pkg/matcher/annotation_matcher.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package matcher
33
import (
44
"context"
55
"fmt"
6-
"path/filepath"
76
"regexp"
87
"strings"
98

@@ -26,12 +25,6 @@ const (
2625
)
2726

2827
func branchMatch(prunBranch, baseBranch string) bool {
29-
// If we have targetBranch in annotation and refs/heads/targetBranch from
30-
// webhook, then allow it.
31-
if filepath.Base(baseBranch) == filepath.Base(prunBranch) {
32-
return true
33-
}
34-
3528
// if target is refs/heads/.. and base is without ref (for pullRequest)
3629
if strings.HasPrefix(prunBranch, "refs/heads") && !strings.Contains(baseBranch, "/") {
3730
ref := "refs/heads/" + baseBranch

pkg/matcher/annotation_matcher_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,16 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
813813
},
814814
}
815815

816+
pipelinePush := &tektonv1.PipelineRun{
817+
ObjectMeta: metav1.ObjectMeta{
818+
Name: "pipeline-push",
819+
Annotations: map[string]string{
820+
keys.OnEvent: "[push]",
821+
keys.OnTargetBranch: "[main]",
822+
},
823+
},
824+
}
825+
816826
pipelineOther := &tektonv1.PipelineRun{
817827
ObjectMeta: metav1.ObjectMeta{
818828
Name: "pipeline-other",
@@ -1050,6 +1060,22 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
10501060
},
10511061
wantErr: false,
10521062
},
1063+
{
1064+
name: "not-match-push-branch-matching",
1065+
args: args{
1066+
runevent: info.Event{TriggerTarget: "push", EventType: "push", BaseBranch: "refs/heads/someothername/then/main"},
1067+
pruns: []*tektonv1.PipelineRun{pipelineGood, pipelinePush},
1068+
},
1069+
wantErr: true,
1070+
},
1071+
{
1072+
name: "not-match-pull-request-branch-matching",
1073+
args: args{
1074+
runevent: info.Event{TriggerTarget: "pull_request", EventType: "pull_request", BaseBranch: "someothername/then/main"},
1075+
pruns: []*tektonv1.PipelineRun{pipelineGood, pipelinePush},
1076+
},
1077+
wantErr: true,
1078+
},
10531079
}
10541080

10551081
for _, tt := range tests {

0 commit comments

Comments
 (0)