Skip to content

Commit

Permalink
Simplify branchMatch function
Browse files Browse the repository at this point in the history
It was hard to read, and with the help of codeassist i simplyfied it to
make it more understandable.

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel authored and savitaashture committed Aug 8, 2023
1 parent f09a8a7 commit 9e3a84c
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions pkg/matcher/annotation_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,31 @@ const (
)

func branchMatch(prunBranch, baseBranch string) bool {
// if target is refs/heads/.. and base is without ref (for pullRequest action)
// Helper function to match glob pattern
matchGlob := func(pattern, branch string) bool {
g := glob.MustCompile(pattern)
return g.Match(branch)
}

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
ref = "refs/heads/" + baseBranch
}
g := glob.MustCompile(prunBranch)
if g.Match(ref) {
return true
}
} 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
}
// Match the prunBranch pattern with the modified baseBranch
return matchGlob(prunBranch, ref)
}

// match globs like refs/tags/0.*
g := glob.MustCompile(prunBranch)
return g.Match(baseBranch)
// 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
}
// Match the prunRef pattern with the baseBranch
return matchGlob(prunRef, baseBranch)
}

// TODO: move to another file since it's common to all annotations_* files
Expand Down

0 comments on commit 9e3a84c

Please sign in to comment.