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:
Dave D'Amico
2019-10-03 06:48:13 -04:00
committed by GitHub
parent c4c439aba4
commit ae57df9c44
2 changed files with 43 additions and 1 deletions

View File

@@ -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

View File

@@ -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 {