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
+4 -2
View File
@@ -8,7 +8,7 @@ import (
"errors"
"fmt"
"net/url"
"strings"
"strconv"
"time"
"gitea.dev/models/db"
@@ -123,7 +123,9 @@ func (run *ActionRun) RefLink() string {
func (run *ActionRun) PrettyRef() string {
refName := git.RefName(run.Ref)
if refName.IsPull() {
return "#" + strings.TrimSuffix(strings.TrimPrefix(run.Ref, git.PullPrefix), "/head")
if pullIndex, ok := refName.PullIndex(); ok {
return "#" + strconv.FormatInt(pullIndex, 10)
}
}
return refName.ShortName()
}
+1 -1
View File
@@ -415,7 +415,7 @@ func (pr *PullRequest) getReviewedByLines(ctx context.Context, writer io.Writer)
// GetGitHeadRefName returns git ref for hidden pull request branch
func (pr *PullRequest) GetGitHeadRefName() string { // TODO: make it return RefName but not string
return fmt.Sprintf("%s%d/head", git.PullPrefix, pr.Index)
return git.RefNameFromPullIndex(pr.Index).String()
}
// GetReviewCommentsCount returns the number of review comments made on the diff of a PR review (not including comments on commits or issues in a PR)
+6
View File
@@ -80,6 +80,12 @@ func GetScheduledMergeByPullID(ctx context.Context, pullID int64) (bool, *AutoMe
return true, scheduledPRM, err
}
func GetScheduledMergePullIDsSince(ctx context.Context, since timeutil.TimeStamp) ([]int64, error) {
var pullIDs []int64
err := db.GetEngine(ctx).Table(&AutoMerge{}).Where("created_unix >= ?", since).Cols("pull_id").Find(&pullIDs)
return pullIDs, err
}
// DeleteScheduledAutoMerge delete a scheduled pull request
func DeleteScheduledAutoMerge(ctx context.Context, pullID int64) error {
exist, scheduledPRM, err := GetScheduledMergeByPullID(ctx, pullID)