-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Branch protection: Possibility to not use whitelist but allow anyone with write access #9055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
22be3e9
11a5954
5bcf80e
b488f1d
21f8590
2461d52
d1ecef6
d48fcb4
459c59c
283be88
9d3da22
148e1d6
8afcf90
c296126
5ee7ae2
b63974e
6b04006
e6908f5
2cad7bb
8af34e3
2187c6f
b1537d4
8198531
5aa2e18
6ca746a
f81f209
52760dd
93ede5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,11 +26,11 @@ const ( | |
|
|
||
| // ProtectedBranch struct | ||
| type ProtectedBranch struct { | ||
| ID int64 `xorm:"pk autoincr"` | ||
| RepoID int64 `xorm:"UNIQUE(s)"` | ||
| BranchName string `xorm:"UNIQUE(s)"` | ||
| CanPush bool `xorm:"NOT NULL DEFAULT false"` | ||
| EnableWhitelist bool | ||
| ID int64 `xorm:"pk autoincr"` | ||
| RepoID int64 `xorm:"UNIQUE(s)"` | ||
| BranchName string `xorm:"UNIQUE(s)"` | ||
| CanPush bool `xorm:"NOT NULL DEFAULT false"` | ||
| EnableWhitelist bool `xorm:"NOT NULL DEFAULT false"` | ||
| WhitelistUserIDs []int64 `xorm:"JSON TEXT"` | ||
| WhitelistTeamIDs []int64 `xorm:"JSON TEXT"` | ||
| EnableMergeWhitelist bool `xorm:"NOT NULL DEFAULT false"` | ||
|
|
@@ -39,6 +39,7 @@ type ProtectedBranch struct { | |
| MergeWhitelistTeamIDs []int64 `xorm:"JSON TEXT"` | ||
| EnableStatusCheck bool `xorm:"NOT NULL DEFAULT false"` | ||
| StatusCheckContexts []string `xorm:"JSON TEXT"` | ||
| EnableApprovalsWhitelist bool `xorm:"NOT NULL DEFAULT false"` | ||
| ApprovalsWhitelistUserIDs []int64 `xorm:"JSON TEXT"` | ||
| ApprovalsWhitelistTeamIDs []int64 `xorm:"JSON TEXT"` | ||
| RequiredApprovals int64 `xorm:"NOT NULL DEFAULT 0"` | ||
|
|
@@ -53,10 +54,25 @@ func (protectBranch *ProtectedBranch) IsProtected() bool { | |
|
|
||
| // CanUserPush returns if some user could push to this protected branch | ||
| func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool { | ||
| if !protectBranch.EnableWhitelist { | ||
| if !protectBranch.CanPush { | ||
| return false | ||
| } | ||
|
|
||
| if !protectBranch.EnableWhitelist { | ||
| if user, err := GetUserByID(userID); err != nil { | ||
| log.Error("GetUserByID: %v", err) | ||
| return false | ||
| } else if repo, err := GetRepositoryByID(protectBranch.RepoID); err != nil { | ||
| log.Error("GetRepositoryByID: %v", err) | ||
| return false | ||
| } else if writeAccess, err := HasAccessUnit(user, repo, UnitTypeCode, AccessModeWrite); err != nil { | ||
| log.Error("HasAccessUnit: %v", err) | ||
| return false | ||
| } else { | ||
| return writeAccess | ||
| } | ||
| } | ||
|
|
||
| if base.Int64sContains(protectBranch.WhitelistUserIDs, userID) { | ||
| return true | ||
| } | ||
|
|
@@ -111,12 +127,28 @@ func (protectBranch *ProtectedBranch) GetGrantedApprovalsCount(pr *PullRequest) | |
| return 0 | ||
| } | ||
|
|
||
| repo, err := GetRepositoryByID(protectBranch.RepoID) | ||
| if err != nil { | ||
| log.Error("GetRepositoryByID: %v", err) | ||
| return 0 | ||
| } | ||
|
|
||
| approvals := int64(0) | ||
| userIDs := make([]int64, 0) | ||
| for _, review := range reviews { | ||
| if review.Type != ReviewTypeApprove { | ||
| continue | ||
| } | ||
| if !protectBranch.EnableApprovalsWhitelist { | ||
|
||
| if user, err := GetUserByID(review.ID); err != nil { | ||
| log.Error("GetUserByID: %v", err) | ||
| } else if writeAccess, err := HasAccessUnit(user, repo, UnitTypeCode, AccessModeWrite); err != nil { | ||
| log.Error("HasAccessUnit: %v", err) | ||
| } else if writeAccess { | ||
| approvals++ | ||
| } | ||
| continue | ||
| } | ||
| if base.Int64sContains(protectBranch.ApprovalsWhitelistUserIDs, review.ID) { | ||
| approvals++ | ||
| continue | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright 2019 The Gitea Authors. All rights reserved. | ||
| // Use of this source code is governed by a MIT-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package migrations | ||
|
|
||
| import ( | ||
| "xorm.io/xorm" | ||
| ) | ||
|
|
||
| func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error { | ||
|
|
||
| type ProtectedBranch struct { | ||
| CanPush bool `xorm:"NOT NULL DEFAULT false"` | ||
| EnableWhitelist bool `xorm:"NOT NULL DEFAULT false"` | ||
| EnableApprovalsWhitelist bool `xorm:"NOT NULL DEFAULT false"` | ||
| RequiredApprovals int64 `xorm:"NOT NULL DEFAULT 0"` | ||
| } | ||
|
|
||
| sess := x.NewSession() | ||
| defer sess.Close() | ||
|
|
||
| if err := x.Sync2(new(ProtectedBranch)); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if _, err := x.Exec("UPDATE `protected_branch` SET `can_push` = `enable_whitelist`"); err != nil { | ||
| return err | ||
| } | ||
| _, err := x.Exec("UPDATE `protected_branch` SET `enable_approvals_whitelist` = ? WHERE `required_approvals` > ?", true, 0) | ||
| return err | ||
| } |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.