Skip to content

Commit 5f34f78

Browse files
committed
Fix branch matching when there is a slashes in the base branch name
1 parent 48f9019 commit 5f34f78

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

pkg/matcher/annotation_matcher.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@ const (
2525
)
2626

2727
func branchMatch(prunBranch, baseBranch string) bool {
28-
// if target is refs/heads/.. and base is without ref (for pullRequest)
29-
if strings.HasPrefix(prunBranch, "refs/heads") && !strings.Contains(baseBranch, "/") {
30-
ref := "refs/heads/" + baseBranch
28+
// if target is refs/heads/.. and base is without ref (for pullRequest action)
29+
if strings.HasPrefix(prunBranch, "refs/heads/") {
30+
ref := baseBranch
31+
if !strings.HasPrefix(baseBranch, "refs/heads/") {
32+
ref = "refs/heads/" + baseBranch
33+
}
3134
g := glob.MustCompile(prunBranch)
3235
if g.Match(ref) {
3336
return true
3437
}
35-
}
36-
37-
// if base is refs/heads/.. and target is without ref (for push rerequested action)
38-
if strings.HasPrefix(baseBranch, "refs/heads") && !strings.Contains(prunBranch, "/") {
39-
prunRef := "refs/heads/" + prunBranch
38+
} else {
39+
// if base is refs/heads/.. and target is without ref (for push request action)
40+
prunRef := prunBranch
41+
if !strings.HasPrefix(prunBranch, "refs/heads/") {
42+
prunRef = "refs/heads/" + prunBranch
43+
}
4044
g := glob.MustCompile(prunRef)
4145
if g.Match(baseBranch) {
4246
return true

pkg/matcher/annotation_matcher_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,16 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
833833
},
834834
}
835835

836+
pipelineWithSlashInBranchName := &tektonv1.PipelineRun{
837+
ObjectMeta: metav1.ObjectMeta{
838+
Name: "pipeline-withslashesinbranch",
839+
Annotations: map[string]string{
840+
keys.OnEvent: "[pull_request, push]",
841+
keys.OnTargetBranch: "[test/main]",
842+
},
843+
},
844+
}
845+
836846
pipelineRefAll := &tektonv1.PipelineRun{
837847
ObjectMeta: metav1.ObjectMeta{
838848
Name: "pipeline-other",
@@ -1061,21 +1071,37 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
10611071
wantErr: false,
10621072
},
10631073
{
1064-
name: "not-match-push-branch-matching",
1074+
name: "branch-matching-doesnot-match-for-push-event",
10651075
args: args{
10661076
runevent: info.Event{TriggerTarget: "push", EventType: "push", BaseBranch: "refs/heads/someothername/then/main"},
10671077
pruns: []*tektonv1.PipelineRun{pipelineGood, pipelinePush},
10681078
},
10691079
wantErr: true,
10701080
},
10711081
{
1072-
name: "not-match-pull-request-branch-matching",
1082+
name: "branch-matching-doesnot-match-for-pull-request",
10731083
args: args{
10741084
runevent: info.Event{TriggerTarget: "pull_request", EventType: "pull_request", BaseBranch: "someothername/then/main"},
10751085
pruns: []*tektonv1.PipelineRun{pipelineGood, pipelinePush},
10761086
},
10771087
wantErr: true,
10781088
},
1089+
{
1090+
name: "branch-matching-match-for-push-when-there-are-slashes-in-between-branch-name",
1091+
args: args{
1092+
runevent: info.Event{TriggerTarget: "push", EventType: "push", BaseBranch: "refs/heads/test/main"},
1093+
pruns: []*tektonv1.PipelineRun{pipelineWithSlashInBranchName},
1094+
},
1095+
wantErr: false,
1096+
},
1097+
{
1098+
name: "branch-matching-match-for-pull_request-when-there-are-slashes-in-between-branch-name",
1099+
args: args{
1100+
runevent: info.Event{TriggerTarget: "pull_request", EventType: "pull_request", BaseBranch: "refs/heads/test/main"},
1101+
pruns: []*tektonv1.PipelineRun{pipelineWithSlashInBranchName},
1102+
},
1103+
wantErr: false,
1104+
},
10791105
}
10801106

10811107
for _, tt := range tests {

0 commit comments

Comments
 (0)