refactor(automerge): fix error handling, populate recent automerge tasks on restart (#39001)

* Refactor "automerge" related code, clarify many details (including "unique queue item", start check by pull head or commit)
* Fix automerge queue handler's error handling, clarify error messages
* Populate recent automerge tasks on restart to restore the previous aborted automerge tasks

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Willem Kokke
2026-08-28 22:17:01 +01:00
committed by GitHub
parent 1652dfe62a
commit 60326ca03d
15 changed files with 246 additions and 176 deletions
+55
View File
@@ -0,0 +1,55 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package pull
import (
"context"
"errors"
issues_model "gitea.dev/models/issues"
repo_model "gitea.dev/models/repo"
"gitea.dev/modules/git"
"gitea.dev/modules/util"
)
func EnumPullRequestsByHeadCommitID(ctx context.Context, repo *repo_model.Repository, commitID string, filter func(*issues_model.PullRequest) bool) (pulls []*issues_model.PullRequest, _ error) {
gitRepo, err := git.OpenRepository(ctx, repo)
if err != nil {
return nil, err
}
defer gitRepo.Close()
refs, err := gitRepo.GetRefsBySha(ctx, commitID, git.PullPrefix)
if err != nil {
return nil, err
}
for _, refStr := range refs {
ref := git.RefName(refStr)
prIndex, ok := ref.PullIndex()
if !ok {
continue
}
pull, err := issues_model.GetPullRequestByIndex(ctx, repo.ID, prIndex)
if err != nil {
if errors.Is(err, util.ErrNotExist) {
continue // ignore non-existing pull requests
}
return nil, err
}
if filter(pull) {
pulls = append(pulls, pull)
}
}
return pulls, nil
}
func GetMergeablePullRequestsByHeadCommitID(ctx context.Context, repo *repo_model.Repository, commitID string) ([]*issues_model.PullRequest, error) {
return EnumPullRequestsByHeadCommitID(ctx, repo, commitID, func(pr *issues_model.PullRequest) bool {
_ = pr.LoadIssue(ctx)
return pr.Issue != nil && !pr.Issue.IsClosed && !pr.HasMerged && pr.IsStatusMergeable()
})
}