-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore(dummy): test yaml sequence compatibility * fix: use single tick for regex default values * feat: add string array reader utility * feat: add username whitelist * docs: write documentation about username whitelist
- Loading branch information
Showing
11 changed files
with
221 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ var ( | |
NewBotWhitelist, | ||
NewDraftWhitelist, | ||
NewPermissionWhitelist, | ||
NewUsernameWhitelist, | ||
} | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package whitelist | ||
|
||
import ( | ||
"github.com/Namchee/conventional-pr/internal" | ||
"github.com/Namchee/conventional-pr/internal/constants" | ||
"github.com/Namchee/conventional-pr/internal/entity" | ||
"github.com/Namchee/conventional-pr/internal/utils" | ||
"github.com/google/go-github/v32/github" | ||
) | ||
|
||
type usernameWhitelist struct { | ||
client internal.GithubClient | ||
config *entity.Config | ||
Name string | ||
} | ||
|
||
// NewUsernameWhitelist creates a whitelist that bypasses checks for certain usernames | ||
func NewUsernameWhitelist(client internal.GithubClient, config *entity.Config, _ *entity.Meta) internal.Whitelist { | ||
return &usernameWhitelist{ | ||
client: client, | ||
config: config, | ||
Name: constants.UsernameWhitelistName, | ||
} | ||
} | ||
|
||
func (w *usernameWhitelist) IsWhitelisted(pullRequest *github.PullRequest) *entity.WhitelistResult { | ||
if len(w.config.IgnoredUsers) == 0 { | ||
return &entity.WhitelistResult{ | ||
Name: w.Name, | ||
Active: false, | ||
Result: false, | ||
} | ||
} | ||
|
||
user := pullRequest.GetUser().GetLogin() | ||
|
||
return &entity.WhitelistResult{ | ||
Name: w.Name, | ||
Active: true, | ||
Result: utils.ContainsString(w.config.IgnoredUsers, user), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package whitelist | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Namchee/conventional-pr/internal/constants" | ||
"github.com/Namchee/conventional-pr/internal/entity" | ||
"github.com/Namchee/conventional-pr/internal/mocks" | ||
"github.com/google/go-github/v32/github" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUsernameWhitelist_IsWhitelisted(t *testing.T) { | ||
type args struct { | ||
name string | ||
config []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *entity.WhitelistResult | ||
}{ | ||
{ | ||
name: "should be skipped if config is empty", | ||
args: args{ | ||
name: "foo", | ||
config: []string{}, | ||
}, | ||
want: &entity.WhitelistResult{ | ||
Name: constants.UsernameWhitelistName, | ||
Active: false, | ||
Result: false, | ||
}, | ||
}, | ||
{ | ||
name: "should be checked if user is not on whitelist", | ||
args: args{ | ||
name: "foo", | ||
config: []string{"bar"}, | ||
}, | ||
want: &entity.WhitelistResult{ | ||
Name: constants.UsernameWhitelistName, | ||
Active: true, | ||
Result: false, | ||
}, | ||
}, | ||
{ | ||
name: "should be skipped if user is on whitelist", | ||
args: args{ | ||
name: "bar", | ||
config: []string{"bar"}, | ||
}, | ||
want: &entity.WhitelistResult{ | ||
Name: constants.UsernameWhitelistName, | ||
Active: true, | ||
Result: true, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
user := &github.User{ | ||
Login: &tc.args.name, | ||
} | ||
pull := &github.PullRequest{ | ||
User: user, | ||
} | ||
config := &entity.Config{ | ||
IgnoredUsers: tc.args.config, | ||
} | ||
client := mocks.NewGithubClientMock() | ||
|
||
whitelister := NewUsernameWhitelist(client, config, nil) | ||
|
||
got := whitelister.IsWhitelisted(pull) | ||
|
||
assert.Equal(t, got, tc.want) | ||
}) | ||
} | ||
} |