-
-
Couldn't load subscription status.
- Fork 6.2k
Added checks for protected branches in pull requests #3544
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
Conversation
Signed-off-by: Christian Wulff <NChris@posteo.net>
Codecov Report
@@ Coverage Diff @@
## master #3544 +/- ##
==========================================
- Coverage 35.88% 35.86% -0.03%
==========================================
Files 287 287
Lines 41331 41362 +31
==========================================
+ Hits 14833 14835 +2
- Misses 24316 24339 +23
- Partials 2182 2188 +6
Continue to review full report at Codecov.
|
models/error.go
Outdated
| } | ||
|
|
||
| // ErrBranchProtected represents an error that a branch is protected and the current user is not allowed to modify it | ||
| type ErrBranchProtected struct { |
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.
ErrNotAllowedToMerge would be better name for this error
models/pull.go
Outdated
| } | ||
| prConfig := prUnit.PullRequestsConfig() | ||
|
|
||
| if protected, err := pr.BaseRepo.IsProtectedBranch(pr.BaseBranch, doer); err != nil { |
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.
Would be better to create new function pr.CheckUserAllowedToMerge that would return error (nil for allowed to merge)
routers/repo/issue.go
Outdated
| } | ||
| prConfig := prUnit.PullRequestsConfig() | ||
|
|
||
| if ctx.IsSigned { |
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.
Reuse function pull.CheckUserAllowedToMerge here
routers/repo/issue.go
Outdated
|
|
||
| if ctx.IsSigned { | ||
| if err := pull.GetBaseRepo(); err != nil { | ||
| log.Error(4, "GetBaseRepo: %v", err) |
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.
in such cases ctx.ServerError(... should be used
routers/repo/issue.go
Outdated
| log.Error(4, "IsProtectedBranch: %v", err) | ||
| ctx.Data["BaseBranchNotProtected"] = false | ||
| } else { | ||
| ctx.Data["BaseBranchNotProtected"] = !protected |
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.
Reuse IsRepositoryWriter && CheckUserAllowedToMerge to assign to something like ctx.Data["AllowMerge"] and check that in pull.tmpl with {{if .AllowMerge}}
|
With moving that check to function would allow to reuse that function to add additional conditions for merge limitations |
…Merge Signed-off-by: Christian Wulff <NChris@posteo.net>
Signed-off-by: Christian Wulff <NChris@posteo.net>
|
Sorry, had a problem with the first commit after your change request :( I hope the last commit meets your requirements. |
models/pull.go
Outdated
| "Not signed in", | ||
| } | ||
| } | ||
| if err = pr.GetBaseRepo(); err != nil { |
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.
No need to load repository if it is already loaded (need check if pr.BaseRepo == nil {)
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.
I added it, but GetBaseRepo() already checks for this:
func (pr *PullRequest) GetBaseRepo() (err error) {
if pr.BaseRepo != nil {
return nil
}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.
Sorry I overlooked that function than... I was checking code but somehow missed that check. If it does than yes there is no need to do additional check
| } | ||
| prConfig := prUnit.PullRequestsConfig() | ||
|
|
||
| if err := pull.CheckUserAllowedToMerge(ctx.User); err != nil { |
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.
This could be rewritten as:
ctx.Data["AllowMerge"] = ctx.Data["IsRepositoryWriter"]
if err := pull.CheckUserAllowedToMerge(ctx.User); err != nil {
if !models.IsErrNotAllowedToMerge(err) {
ctx.ServerError(err)
return
}
ctx.Data["AllowMerge"] = false
}|
The CI build failed because of a git error (https://drone.gitea.io/go-gitea/gitea/3754/2): I don't think that this has something to do with my latest commit, is there some way to tell drone to retry? |
Signed-off-by: Christian Wulff <NChris@posteo.net>
|
LGTM |
|
need @lafriks 's review. |
Fixes #2875. This pull request adds checks whether the user is allowed to push to the base branch of a pull request.
If a user may not push to the base branch, the green merge button is hidden on the pull request view. If the user tries merge via the web api, an error (http code 500) with
{"message":"branch is protected [name: %s]","url":"https://godoc.org/github.com/go-gitea/go-sdk/gitea"}is returned.
There shouldn't be a way to circumvent the branch protection via a pull request.