Skip to content

Commit 18e0447

Browse files
davidsvantessonsapk
authored andcommitted
Fix admin handling at merge of PR (go-gitea#9749)
* Admin shall be able to bypass merge checks. * Repository admin should not bypass if merge whitelist is set. * Add code comment about checks that PR are ready * notAllOverrideableChecksOk->notAllOverridableChecksOk * Fix merge, require signed currently not overridable. * fix Co-authored-by: Antoine GIRARD <[email protected]>
1 parent d3468ed commit 18e0447

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

routers/private/hook.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func HookPreReceive(ctx *macaron.Context, opts private.HookOptions) {
224224
canPush = protectBranch.CanUserPush(opts.UserID)
225225
}
226226
if !canPush && opts.ProtectedBranchID > 0 {
227-
// Manual merge
227+
// Merge (from UI or API)
228228
pr, err := models.GetPullRequestByID(opts.ProtectedBranchID)
229229
if err != nil {
230230
log.Error("Unable to get PullRequest %d Error: %v", opts.ProtectedBranchID, err)
@@ -264,19 +264,21 @@ func HookPreReceive(ctx *macaron.Context, opts private.HookOptions) {
264264
})
265265
return
266266
}
267-
// Manual merge only allowed if PR is ready (even if admin)
268-
if err := pull_service.CheckPRReadyToMerge(pr); err != nil {
269-
if models.IsErrNotAllowedToMerge(err) {
270-
log.Warn("Forbidden: User %d is not allowed push to protected branch %s in %-v and pr #%d is not ready to be merged: %s", opts.UserID, branchName, repo, pr.Index, err.Error())
271-
ctx.JSON(http.StatusForbidden, map[string]interface{}{
272-
"err": fmt.Sprintf("Not allowed to push to protected branch %s and pr #%d is not ready to be merged: %s", branchName, opts.ProtectedBranchID, err.Error()),
267+
// Check all status checks and reviews is ok, unless repo admin which can bypass this.
268+
if !perm.IsAdmin() {
269+
if err := pull_service.CheckPRReadyToMerge(pr); err != nil {
270+
if models.IsErrNotAllowedToMerge(err) {
271+
log.Warn("Forbidden: User %d is not allowed push to protected branch %s in %-v and pr #%d is not ready to be merged: %s", opts.UserID, branchName, repo, pr.Index, err.Error())
272+
ctx.JSON(http.StatusForbidden, map[string]interface{}{
273+
"err": fmt.Sprintf("Not allowed to push to protected branch %s and pr #%d is not ready to be merged: %s", branchName, opts.ProtectedBranchID, err.Error()),
274+
})
275+
return
276+
}
277+
log.Error("Unable to check if mergable: protected branch %s in %-v and pr #%d. Error: %v", opts.UserID, branchName, repo, pr.Index, err)
278+
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
279+
"err": fmt.Sprintf("Unable to get status of pull request %d. Error: %v", opts.ProtectedBranchID, err),
273280
})
274-
return
275281
}
276-
log.Error("Unable to check if mergable: protected branch %s in %-v and pr #%d. Error: %v", opts.UserID, branchName, repo, pr.Index, err)
277-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
278-
"err": fmt.Sprintf("Unable to get status of pull request %d. Error: %v", opts.ProtectedBranchID, err),
279-
})
280282
}
281283
} else if !canPush {
282284
log.Warn("Forbidden: User %d is not allowed to push to protected branch: %s in %-v", opts.UserID, branchName, repo)

services/pull/merge.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,6 @@ func IsSignedIfRequired(pr *models.PullRequest, doer *models.User) (bool, error)
487487

488488
// IsUserAllowedToMerge check if user is allowed to merge PR with given permissions and branch protections
489489
func IsUserAllowedToMerge(pr *models.PullRequest, p models.Permission, user *models.User) (bool, error) {
490-
if p.IsAdmin() {
491-
return true, nil
492-
}
493490
if !p.CanWrite(models.UnitTypeCode) {
494491
return false, nil
495492
}

templates/repo/issue/view_content/pull.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@
133133
{{$.i18n.Tr (printf "repo.signing.wont_sign.%s" .WontSignReason) }}
134134
</div>
135135
{{end}}
136-
{{$notAllOk := or .IsBlockedByApprovals .IsBlockedByRejection (and .RequireSigned (not .WillSign)) (and .EnableStatusCheck (not .IsRequiredStatusCheckSuccess))}}
137-
{{if and (or $.IsRepoAdmin (not $notAllOk)) (or (not .RequireSigned) .WillSign)}}
138-
{{if $notAllOk}}
136+
{{$notAllOverridableChecksOk := or .IsBlockedByApprovals .IsBlockedByRejection (and .EnableStatusCheck (not .IsRequiredStatusCheckSuccess))}}
137+
{{if and (or $.IsRepoAdmin (not $notAllOverridableChecksOk)) (or (not .RequireSigned) .WillSign)}}
138+
{{if $notAllOverridableChecksOk}}
139139
<div class="item text yellow">
140140
<i class="icon icon-octicon"><span class="octicon octicon-primitive-dot"></span></i>
141141
{{$.i18n.Tr "repo.pulls.required_status_check_administrator"}}
@@ -233,7 +233,7 @@
233233
</form>
234234
</div>
235235
{{end}}
236-
<div class="ui {{if $notAllOk}}red{{else}}green{{end}} buttons merge-button">
236+
<div class="ui {{if $notAllOverridableChecksOk}}red{{else}}green{{end}} buttons merge-button">
237237
<button class="ui button" data-do="{{.MergeStyle}}">
238238
<span class="octicon octicon-git-merge"></span>
239239
<span class="button-text">

0 commit comments

Comments
 (0)