mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-26 10:47:04 -06:00
Adds a merge queue for protected branches, modeled on GitHub's. Merging
into a branch that requires it queues the pull request, tests it with
the ones ahead of it in `merge_group` runs and fast-forwards the base
once the required checks pass. Failing or conflicting pull requests are
removed.
Rules configure merge method, group sizes, grouping and timeouts. Each
queue has a page at `/{owner}/{repo}/queue/{branch}`. The API can queue,
jump, look up and remove a pull request and list or clear a queue, and
webhooks and Actions get the `merge_group` event and
`enqueued`/`dequeued` actions. The merge box's command line hint now
sits beside the merge buttons like on GitHub, also outside of merge
queues.
One notable difference from GitHub is that Gitea allows merge queue to
be enabled in any repo, as opposed to GitHub which only allows it in
organization repos.
| View | Screenshot |
| --- | --- |
| Merge box, ready | <img width="440" alt="Merge box, ready"
src="https://github.com/user-attachments/assets/7d2ac984-90c4-4744-8ac8-5daf37cd96e9"
/> |
| Merge box, queued | <img width="440" alt="Merge box, queued"
src="https://github.com/user-attachments/assets/33b85cab-dfd6-405f-8ffd-283cd920e59f"
/> |
| Pull request header, queued | <img width="600" alt="Pull request
header, queued"
src="https://github.com/user-attachments/assets/d61c635b-8552-4b47-a900-e493912e8c16"
/> |
| Timeline, removal after failed checks | <img width="440"
alt="Timeline, removal after failed checks"
src="https://github.com/user-attachments/assets/bee95e79-4c76-4592-ad35-82109d9da4ed"
/> |
| Queue page | <img width="600" alt="Queue page"
src="https://github.com/user-attachments/assets/847a595a-1e13-4099-aa37-7f44018dc188"
/> |
| Pull request list, queued | <img width="600" alt="Pull request list,
queued"
src="https://github.com/user-attachments/assets/a382a143-a67f-4298-aa11-594e281d54db"
/> |
| Merge queue settings | <img width="600" alt="Merge queue settings"
src="https://github.com/user-attachments/assets/cdaf4aa5-350e-4570-a095-c91c41d03cde"
/> |
Fixes: https://github.com/go-gitea/gitea/issues/36392
Docs: https://gitea.com/gitea/docs/pulls/655
Assisted-by: Claude Code:claude-opus-5-5
---------
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: TheFox0x7 <thefox0x7@gmail.com>
107 lines
3.6 KiB
Go
107 lines
3.6 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package pull
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
issues_model "gitea.dev/models/issues"
|
|
repo_model "gitea.dev/models/repo"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/git/gitcmd"
|
|
"gitea.dev/modules/log"
|
|
repo_module "gitea.dev/modules/repository"
|
|
"gitea.dev/modules/setting"
|
|
)
|
|
|
|
// updateHeadByRebaseOnToBase handles updating a PR's head branch by rebasing it on the PR current base branch
|
|
func updateHeadByRebaseOnToBase(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User) error {
|
|
// "Clone" base repo and add the cache headers for the head repo and branch
|
|
mergeCtx, cancel, err := createTemporaryRepoForMerge(ctx, pr, doer, "", "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cancel()
|
|
|
|
// Determine the old merge-base before the rebase - we use this for LFS push later on
|
|
oldMergeBase, _, _ := gitcmd.NewCommand("merge-base").AddDashesAndList(tmpRepoBaseBranch, tmpRepoTrackingBranch).
|
|
WithRepo(mergeCtx.tmpRepo).RunStdString(ctx)
|
|
oldMergeBase = strings.TrimSpace(oldMergeBase)
|
|
|
|
// Rebase the tracking branch on to the base as the staging branch
|
|
if err := rebaseTrackingOnToBase(mergeCtx, repo_model.MergeStyleRebaseUpdate); err != nil {
|
|
return err
|
|
}
|
|
|
|
if setting.LFS.StartServer {
|
|
// Now we need to ensure that the head repository contains any LFS objects between the new base and the old mergebase
|
|
// It's questionable about where this should go - either after or before the push
|
|
// I think in the interests of data safety - failures to push to the lfs should prevent
|
|
// the push as you can always re-rebase.
|
|
if err := LFSPush(ctx, mergeCtx.tmpBasePath, mergeCtx.tmpRepo, tmpRepoBaseBranch, oldMergeBase, &issues_model.PullRequest{
|
|
HeadRepoID: pr.BaseRepoID,
|
|
BaseRepoID: pr.HeadRepoID,
|
|
}); err != nil {
|
|
log.Error("Unable to push lfs objects between %s and %s up to head branch in %-v: %v", tmpRepoBaseBranch, oldMergeBase, pr, err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Now determine who the pushing author should be
|
|
var headUser *user_model.User
|
|
if err := pr.HeadRepo.LoadOwner(ctx); err != nil {
|
|
if !user_model.IsErrUserNotExist(err) {
|
|
log.Error("Can't find user: %d for head repository in %-v - %v", pr.HeadRepo.OwnerID, pr, err)
|
|
return err
|
|
}
|
|
log.Error("Can't find user: %d for head repository in %-v - defaulting to doer: %-v - %v", pr.HeadRepo.OwnerID, pr, doer, err)
|
|
headUser = doer
|
|
} else {
|
|
headUser = pr.HeadRepo.Owner
|
|
}
|
|
|
|
pushCmd := gitcmd.NewCommand("push", "-f", "head_repo").
|
|
AddDynamicArguments(tmpRepoStagingBranch + ":" + git.BranchPrefix + pr.HeadBranch)
|
|
|
|
// Push back to the head repository.
|
|
// TODO: this cause an api call to "/api/internal/hook/post-receive/...",
|
|
// that prevents us from doint the whole merge in one db transaction
|
|
mergeCtx.outbuf.Reset()
|
|
|
|
if err := pushCmd.
|
|
WithEnv(repo_module.FullPushingEnvironment(
|
|
headUser,
|
|
doer,
|
|
pr.HeadRepo,
|
|
pr.HeadRepo.Name,
|
|
pr.ID,
|
|
pr.Index,
|
|
)).
|
|
WithRepo(mergeCtx.tmpRepo).
|
|
WithStdoutBuffer(mergeCtx.outbuf).
|
|
RunWithStderr(ctx); err != nil {
|
|
if strings.Contains(err.Stderr(), "non-fast-forward") {
|
|
return &git.ErrPushOutOfDate{
|
|
StdOut: mergeCtx.outbuf.String(),
|
|
StdErr: err.Stderr(),
|
|
Err: err,
|
|
}
|
|
} else if strings.Contains(err.Stderr(), "! [remote rejected]") {
|
|
err := &git.ErrPushRejected{
|
|
StdOut: mergeCtx.outbuf.String(),
|
|
StdErr: err.Stderr(),
|
|
Err: err,
|
|
}
|
|
err.GenerateMessage()
|
|
return err
|
|
}
|
|
return fmt.Errorf("git push: %s", err.Stderr())
|
|
}
|
|
mergeCtx.outbuf.Reset()
|
|
return nil
|
|
}
|