mirror of
https://git.vectorsigma.ru/public/atlantis.git
synced 2026-07-28 23:58:19 +00:00
Fix to allow whitelist globbing for strings after wildcard (#796)
* almost ready for PR * updated test and fmt * added one more test per feedback * updates to testing and code per feedback
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user