Skip to content

Commit

Permalink
Fix branch matching based on base name (#1352)
Browse files Browse the repository at this point in the history
  • Loading branch information
savitaashture committed Aug 1, 2023
1 parent 306fd47 commit bb75a63
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
7 changes: 0 additions & 7 deletions pkg/matcher/annotation_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package matcher
import (
"context"
"fmt"
"path/filepath"
"regexp"
"strings"

Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions pkg/matcher/annotation_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit bb75a63

Please sign in to comment.