diff options
author | Jason Song <i@wolfogre.com> | 2023-02-21 23:42:22 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-21 09:42:22 -0600 |
commit | 90982bffa5fe472d528577409cb77f85a745d1a0 (patch) | |
tree | 4802f5471d7f47992635295a6968e96cdcb70862 /services/pull | |
parent | 8fa62be9059ddbce53e8eb81884d888ec7b741ec (diff) | |
download | gitea-90982bffa5fe472d528577409cb77f85a745d1a0.tar.gz gitea-90982bffa5fe472d528577409cb77f85a745d1a0.zip |
Add force_merge to merge request and fix checking mergable (#23010) (#23032)
Backport #23010.
Fix #23000.
The bug was introduced in #22633, and it seems that it has been noticed:
https://github.com/go-gitea/gitea/pull/22633#discussion_r1095395359 .
However, #22633 did nothing wrong, the logic should be "check if they is
admin only when `force` is true".
So we should provide the `ForceMerge` when merging from UI.
After this, an admin can also send a normal merge request with
`ForceMerge` false. So it fixes a potential bug: if the admin doesn't
want to do a force merge, they just see the green "Merge" button and
click it. At the same time, the status of the PR changed, and it
shouldn't be merged now, so the admin could send an unexpected force
merge.
In addition, I updated `ForceMerge *bool` to `ForceMerge bool`, I don't
see the reason to use a pointer.
And fixed the logic of CheckPullMergable to handle auto merge and force
merge correctly.
Diffstat (limited to 'services/pull')
-rw-r--r-- | services/pull/check.go | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/services/pull/check.go b/services/pull/check.go index d828ee8cd7..f7e9e4a269 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -59,8 +59,16 @@ func AddToTaskQueue(pr *issues_model.PullRequest) { } } +type MergeCheckType int + +const ( + MergeCheckTypeGeneral MergeCheckType = iota // general merge checks for "merge", "rebase", "squash", etc + MergeCheckTypeManually // Manually Merged button (mark a PR as merged manually) + MergeCheckTypeAuto // Auto Merge (Scheduled Merge) After Checks Succeed +) + // CheckPullMergable check if the pull mergable based on all conditions (branch protection, merge options, ...) -func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *access_model.Permission, pr *issues_model.PullRequest, manuallMerge, force bool) error { +func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *access_model.Permission, pr *issues_model.PullRequest, mergeCheckType MergeCheckType, adminSkipProtectionCheck bool) error { return db.WithTx(func(ctx context.Context) error { if pr.HasMerged { return ErrHasMerged @@ -80,8 +88,8 @@ func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *acce return ErrUserNotAllowedToMerge } - if manuallMerge { - // don't check rules to "auto merge", doer is going to mark this pull as merged manually + if mergeCheckType == MergeCheckTypeManually { + // if doer is doing "manually merge" (mark as merged manually), do not check anything return nil } @@ -103,14 +111,25 @@ func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *acce return err } - if !force { - return err + // Now the branch protection check failed, check whether the failure could be skipped (skip by setting err = nil) + + // * when doing Auto Merge (Scheduled Merge After Checks Succeed), skip the branch protection check + if mergeCheckType == MergeCheckTypeAuto { + err = nil + } + + // * if the doer is admin, they could skip the branch protection check + if adminSkipProtectionCheck { + if isRepoAdmin, errCheckAdmin := access_model.IsUserRepoAdmin(ctx, pr.BaseRepo, doer); errCheckAdmin != nil { + log.Error("Unable to check if %-v is a repo admin in %-v: %v", doer, pr.BaseRepo, errCheckAdmin) + return errCheckAdmin + } else if isRepoAdmin { + err = nil // repo admin can skip the check, so clear the error + } } - if isRepoAdmin, err2 := access_model.IsUserRepoAdmin(ctx, pr.BaseRepo, doer); err2 != nil { - log.Error("Unable to check if %-v is a repo admin in %-v: %v", doer, pr.BaseRepo, err2) - return err2 - } else if !isRepoAdmin { + // If there is still a branch protection check error, return it + if err != nil { return err } } |