diff --git a/server/events/repo_whitelist_checker.go b/server/events/repo_whitelist_checker.go index 8b7d82ee4..0086cfb84 100644 --- a/server/events/repo_whitelist_checker.go +++ b/server/events/repo_whitelist_checker.go @@ -72,7 +72,21 @@ func (r *RepoWhitelistChecker) matchesRule(rule string, candidate string) bool { return false } - // Finally we can use the wildcard. Substring both so they're comparing before the wildcard. Example: + // If wildcard is not the last character, substring both to compare what is after the wildcard. Example: + // candidate: repo-abc + // rule: *-abc + // substr(candidate): -abc + // substr(rule): -abc + if wildcardIdx != len(rule)-1 { + // If the rule substring after wildcard does not exist in the candidate, then it is not a match. + idx := strings.LastIndex(candidate, rule[wildcardIdx+1:]) + if idx == -1 { + return false + } + return candidate[idx:] == rule[wildcardIdx+1:] + } + + // If wildcard is last character, substring both so they're comparing before the wildcard. Example: // candidate: abcd // rule: abc* // substr(candidate): abc diff --git a/server/events/repo_whitelist_checker_test.go b/server/events/repo_whitelist_checker_test.go index 4df1e12fe..1c01f8c52 100644 --- a/server/events/repo_whitelist_checker_test.go +++ b/server/events/repo_whitelist_checker_test.go @@ -147,6 +147,34 @@ func TestRepoWhitelistChecker_IsWhitelisted(t *testing.T) { "github.com", true, }, + { + "should match if wildcard is not last character", + "github.com/owner/*-repo", + "owner/prefix-repo", + "github.com", + true, + }, + { + "should match if wildcard is first character within owner name", + "github.com/*-owner/repo", + "prefix-owner/repo", + "github.com", + true, + }, + { + "should match if wildcard is at beginning", + "*-owner/repo", + "prefix-owner/repo", + "github.com", + true, + }, + { + "should match with duplicate", + "*runatlantis", + "runatlantis/runatlantis", + "github.com", + true, + }, } for _, c := range cases {