Skip to content

Commit

Permalink
Only allow admin to bypass rules if bypassing is allowed (#2047)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesHarness authored and Harness committed May 17, 2024
1 parent fdd401c commit 6da5c93
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
3 changes: 1 addition & 2 deletions app/services/protection/bypass.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ type DefBypass struct {

func (v DefBypass) matches(actor *types.Principal, isRepoOwner bool) bool {
return actor != nil &&
(actor.Admin ||
v.RepoOwners && isRepoOwner ||
(v.RepoOwners && isRepoOwner ||
slices.Contains(v.UserIDs, actor.ID))
}

Expand Down
7 changes: 4 additions & 3 deletions app/services/protection/bypass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ func TestBranch_matches(t *testing.T) {
exp: false,
},
{
name: "admin",
bypass: DefBypass{UserIDs: nil, RepoOwners: false},
name: "admin-no-owner",
bypass: DefBypass{UserIDs: nil, RepoOwners: true},
actor: admin,
exp: true,
owner: false,
exp: false,
},
{
name: "repo-owners-false",
Expand Down
23 changes: 13 additions & 10 deletions app/services/protection/rule_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestBranch_MergeVerify(t *testing.T) {
expVs: []types.RuleViolations{},
},
{
name: "admin-no-bypass",
name: "admin-no-owner",
branch: Branch{
Bypass: DefBypass{},
PullReq: DefPullReq{
Expand All @@ -58,7 +58,8 @@ func TestBranch_MergeVerify(t *testing.T) {
},
in: MergeVerifyInput{
Actor: admin,
AllowBypass: false,
IsRepoOwner: false,
AllowBypass: true,
PullReq: &types.PullReq{UnresolvedCount: 1},
},
expOut: MergeVerifyOutput{
Expand All @@ -68,7 +69,7 @@ func TestBranch_MergeVerify(t *testing.T) {
},
expVs: []types.RuleViolations{
{
Bypassable: true,
Bypassable: false,
Bypassed: false,
Violations: []types.Violation{
{Code: codePullReqCommentsReqResolveAll},
Expand Down Expand Up @@ -314,19 +315,20 @@ func TestBranch_RequiredChecks(t *testing.T) {
},
},
{
name: "admin-bypassable",
name: "admin-no-owner",
branch: Branch{
Bypass: DefBypass{},
PullReq: DefPullReq{
StatusChecks: DefStatusChecks{RequireIdentifiers: []string{"abc"}},
},
},
in: RequiredChecksInput{
Actor: admin,
Actor: admin,
IsRepoOwner: false,
},
expOut: RequiredChecksOutput{
RequiredIdentifiers: nil,
BypassableIdentifiers: map[string]struct{}{"abc": {}},
RequiredIdentifiers: map[string]struct{}{"abc": {}},
BypassableIdentifiers: nil,
},
},
{
Expand Down Expand Up @@ -407,21 +409,22 @@ func TestBranch_RefChangeVerify(t *testing.T) {
expVs: []types.RuleViolations{},
},
{
name: "admin-no-bypass",
name: "admin-no-owner",
branch: Branch{
Bypass: DefBypass{},
Lifecycle: DefLifecycle{DeleteForbidden: true},
},
in: RefChangeVerifyInput{
Actor: admin,
AllowBypass: false,
IsRepoOwner: false,
AllowBypass: true,
RefAction: RefActionDelete,
RefType: RefTypeBranch,
RefNames: []string{"abc"},
},
expVs: []types.RuleViolations{
{
Bypassable: true,
Bypassable: false,
Bypassed: false,
Violations: []types.Violation{
{Code: codeLifecycleDelete},
Expand Down

0 comments on commit 6da5c93

Please sign in to comment.