-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Branch protection: Possibility to not use whitelist but allow anyone with write access #9055
Merged
techknowlogick
merged 28 commits into
go-gitea:master
from
davidsvantesson:branch-protection-anyone
Dec 4, 2019
Merged
Changes from 18 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
22be3e9
Possibility to not use whitelist but allow anyone with write access
davidsvantesson 11a5954
fix existing test
davidsvantesson 5bcf80e
rename migration function
davidsvantesson b488f1d
Try to give a better name for migration step
davidsvantesson 21f8590
Merge branch 'master' into branch-protection-anyone
davidsvantesson 2461d52
Merge remote-tracking branch 'upstream/master' into branch-protection…
davidsvantesson d1ecef6
Merge branch 'master' into branch-protection-anyone
davidsvantesson d48fcb4
Merge branch 'master' into branch-protection-anyone
davidsvantesson 459c59c
Merge branch 'master' into branch-protection-anyone
davidsvantesson 283be88
Merge branch 'master' into branch-protection-anyone
davidsvantesson 9d3da22
Clear settings if higher level setting is not set
davidsvantesson 148e1d6
Move official reviews to db instead of counting approvals each time
davidsvantesson 8afcf90
migration
davidsvantesson c296126
Merge branch 'master' into branch-protection-anyone
davidsvantesson 5ee7ae2
fix
davidsvantesson b63974e
fix migration
davidsvantesson 6b04006
fix migration
davidsvantesson e6908f5
Remove NOT NULL from EnableWhitelist as migration isn't possible
davidsvantesson 2cad7bb
Fix migration, reviews are connected to issues.
davidsvantesson 8af34e3
Fix SQL query issues in GetReviewersByPullID.
davidsvantesson 2187c6f
Merge branch 'master' into branch-protection-anyone
davidsvantesson b1537d4
Simplify function GetReviewersByIssueID
davidsvantesson 8198531
Handle reviewers that has been deleted
davidsvantesson 5aa2e18
Ensure reviews for test is in a well defined order
davidsvantesson 6ca746a
Only clear and set official reviews when it is an approve or reject.
davidsvantesson f81f209
Merge branch 'master' into branch-protection-anyone
techknowlogick 52760dd
Merge branch 'master' into branch-protection-anyone
davidsvantesson 93ede5a
Merge branch 'master' into branch-protection-anyone
techknowlogick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 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 ( | ||
"errors" | ||
|
||
"code.gitea.io/gitea/models" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
func addBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error { | ||
|
||
type ProtectedBranch struct { | ||
CanPush bool `xorm:"NOT NULL DEFAULT false"` | ||
EnableApprovalsWhitelist bool `xorm:"NOT NULL DEFAULT false"` | ||
RequiredApprovals int64 `xorm:"NOT NULL DEFAULT 0"` | ||
} | ||
|
||
type Review struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
Official bool `xorm:"NOT NULL DEFAULT false"` | ||
} | ||
|
||
sess := x.NewSession() | ||
defer sess.Close() | ||
|
||
if err := sess.Sync2(new(ProtectedBranch)); err != nil { | ||
return err | ||
} | ||
|
||
if err := sess.Sync2(new(Review)); err != nil { | ||
return err | ||
} | ||
|
||
if _, err := sess.Exec("UPDATE `protected_branch` SET `can_push` = `enable_whitelist`"); err != nil { | ||
return err | ||
} | ||
if _, err := sess.Exec("UPDATE `protected_branch` SET `enable_approvals_whitelist` = ? WHERE `required_approvals` > ?", true, 0); err != nil { | ||
return err | ||
} | ||
|
||
var pageSize int64 = 20 | ||
qresult, err := sess.QueryInterface("SELECT max(id) as max_id FROM pull_request") | ||
if err != nil { | ||
return err | ||
} | ||
var totalPRs int64 | ||
totalPRs, ok := qresult[0]["max_id"].(int64) | ||
if !ok { | ||
// This shouldn't happen | ||
return errors.New("Failed to get max id for Pull Requests") | ||
} | ||
totalPages := totalPRs / pageSize | ||
|
||
// Find latest review of each user in each pull request, and set official field if appropriate | ||
reviews := []*models.Review{} | ||
var page int64 | ||
for page = 0; page <= totalPages; page++ { | ||
if err := sess.SQL("SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id > ? AND issue_id <= ? AND type in (?, ?) GROUP BY issue_id, reviewer_id)", | ||
page*pageSize, (page+1)*pageSize, models.ReviewTypeApprove, models.ReviewTypeReject). | ||
Find(&reviews); err != nil { | ||
return err | ||
} | ||
|
||
for _, review := range reviews { | ||
if err := review.LoadAttributes(); err != nil { | ||
// Error might occur if user or issue doesn't exist, ignore it. | ||
continue | ||
davidsvantesson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
official, err := models.IsOfficialReviewer(review.Issue, review.Reviewer) | ||
if err != nil { | ||
// Branch might not be proteced or other error, ignore it. | ||
continue | ||
} | ||
review.Official = official | ||
|
||
if _, err := sess.ID(review.ID).Cols("official").Update(review); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
} | ||
|
||
return sess.Commit() | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current query seem to work, although your suggestion maybe looks a bit simpler.