Skip to content

Commit

Permalink
Fix the branchMatching for refs/tags
Browse files Browse the repository at this point in the history
This will fix the branchMatching handling where we were missing
handling the scenario of refs/tags

Also adding unit test for scenarios that can happen for branchMatching
  • Loading branch information
piyush-garg committed Aug 24, 2023
1 parent 557ccab commit ed5f2d0
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 10 deletions.
26 changes: 16 additions & 10 deletions pkg/matcher/annotation_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,38 @@ const (
reValidateTag = `^\[(.*)\]$|^[^[\]\s]*$`
)

// prunBranch is value from annotations and baseBranch is event.Base value from event
func branchMatch(prunBranch, baseBranch string) bool {
// Helper function to match glob pattern
matchGlob := func(pattern, branch string) bool {
g := glob.MustCompile(pattern)
return g.Match(branch)
}

// Case: target is refs/heads/..
if strings.HasPrefix(prunBranch, "refs/heads/") {
// Case: target is refs/heads/..
ref := baseBranch
if !strings.HasPrefix(baseBranch, "refs/heads/") {
// If base is without refs/heads/ prefix, add it
if !strings.HasPrefix(baseBranch, "refs/heads/") && !strings.HasPrefix(baseBranch, "refs/tags/") {
// If base is without refs/heads/.. and not refs/tags/.. prefix, add it
ref = "refs/heads/" + baseBranch
}
// Match the prunBranch pattern with the modified baseBranch
return matchGlob(prunBranch, ref)
}

// Case: base is refs/heads/..
prunRef := prunBranch
if !strings.HasPrefix(prunBranch, "refs/heads/") {
// If prunBranch is without refs/heads/ prefix, add it
prunRef = "refs/heads/" + prunBranch
// Case: target is not refs/heads/.. and not refs/tags/..
if !strings.HasPrefix(prunBranch, "refs/heads/") && !strings.HasPrefix(prunBranch, "refs/tags/") {
prunRef := "refs/heads/" + prunBranch
ref := baseBranch
if !strings.HasPrefix(baseBranch, "refs/heads/") && !strings.HasPrefix(baseBranch, "refs/tags/") {
// If base is without refs/heads/.. and not refs/tags/.. prefix, add it
ref = "refs/heads/" + baseBranch
}
return matchGlob(prunRef, ref)
}

// Match the prunRef pattern with the baseBranch
return matchGlob(prunRef, baseBranch)
// this will cover the scenarios of match globs like refs/tags/0.* and any other if any
return matchGlob(prunBranch, baseBranch)
}

// TODO: move to another file since it's common to all annotations_* files
Expand Down
106 changes: 106 additions & 0 deletions pkg/matcher/annotation_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1199,3 +1199,109 @@ func Test_getAnnotationValues(t *testing.T) {
})
}
}

func TestBranchMatch(t *testing.T) {
tests := []struct {
name string
baseBranch string
prunBranch string
output bool
}{
{
name: "both names",
baseBranch: "main",
prunBranch: "main",
output: true,
},
{
name: "baseBranch refs/head",
baseBranch: "refs/heads/main",
prunBranch: "main",
output: true,
},
{
name: "prunBranch refs/head",
baseBranch: "main",
prunBranch: "refs/heads/main",
output: true,
},
{
name: "both refs/heads",
baseBranch: "refs/heads/main",
prunBranch: "refs/heads/main",
output: true,
},
{
name: "baseBranch refs/tags",
baseBranch: "refs/tags/v0.20.0",
prunBranch: "main",
output: false,
},
{
name: "prunBranch refs/tags",
baseBranch: "main",
prunBranch: "refs/tags/*",
output: false,
},
{
name: "baseBranch refs/tags and prunBranch refs/head",
baseBranch: "refs/tags/v0.20.0",
prunBranch: "refs/heads/main",
output: false,
},
{
name: "baseBranch refs/head and prunBranch refs/tags",
baseBranch: "refs/heads/main",
prunBranch: "refs/tags/*",
output: false,
},
{
name: "both refs/tags",
baseBranch: "refs/tags/v0.20.0",
prunBranch: "refs/tags/*",
output: true,
},
{
name: "different names",
baseBranch: "main",
prunBranch: "test",
output: false,
},
{
name: "base value of path is same",
baseBranch: "refs/heads/foo/test",
prunBranch: "test",
output: false,
},
{
name: "base value of path is same opposite",
baseBranch: "test",
prunBranch: "refs/heads/foo/test",
output: false,
},
{
name: "base value of path is same in both",
baseBranch: "refs/heads/bar/test",
prunBranch: "refs/heads/foo/test",
output: false,
},
{
name: "different refs/tags",
baseBranch: "refs/tags/v0.20.0",
prunBranch: "refs/tags/v0.19.0",
output: false,
},
{
name: "different refs/heads",
baseBranch: "refs/heads/main",
prunBranch: "refs/heads/mains",
output: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := branchMatch(tt.prunBranch, tt.baseBranch)
assert.Equal(t, got, tt.output)
})
}
}

0 comments on commit ed5f2d0

Please sign in to comment.