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 3, 2023
1 parent 78c0159 commit e1c8610
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pkg/matcher/annotation_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ 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, "/") {
if strings.HasPrefix(prunBranch, "refs/heads") && !strings.Contains(baseBranch, "refs/heads") {
ref := "refs/heads/" + baseBranch
g := glob.MustCompile(prunBranch)
if g.Match(ref) {
Expand All @@ -35,7 +35,7 @@ func branchMatch(prunBranch, baseBranch string) bool {
}

// if base is refs/heads/.. and target is without ref (for push rerequested action)
if strings.HasPrefix(baseBranch, "refs/heads") && !strings.Contains(prunBranch, "/") {
if strings.HasPrefix(baseBranch, "refs/heads") && !strings.Contains(prunBranch, "refs/heads") {
prunRef := "refs/heads/" + prunBranch
g := glob.MustCompile(prunRef)
if g.Match(baseBranch) {
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 e1c8610

Please sign in to comment.