Skip to content

Commit

Permalink
Fix branch matching when there is a slashes in the base branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
savitaashture committed Aug 4, 2023
1 parent 48f9019 commit 5f34f78
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
20 changes: 12 additions & 8 deletions pkg/matcher/annotation_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 28 additions & 2 deletions pkg/matcher/annotation_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -1061,21 +1071,37 @@ 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},
},
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 {
Expand Down

0 comments on commit 5f34f78

Please sign in to comment.