mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-28 16:47:01 +09:00
Backport #39831 by @lunny When pushing the merge commit to the base repo fails (e.g. non-fast-forward), the merge intent recorded by https://github.com/go-gitea/gitea/pull/39442 is left behind. `merged_commit_id` then points to a commit that does not exist in the base repo, and `git branch --contains` fails with `no such commit`, so the PR can never be checked or merged again. - Clear the recorded merge intent if the push fails and the commit is not on the base branch - `IsCommitInBranch` now returns false for a missing commit, so already-stuck PRs recover on the next check Fixes: https://github.com/go-gitea/gitea/issues/39824 Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
4 files changed
+77
-2
No files matched your search
@@ -279,6 +279,18 @@ func recordMergeIntent(ctx context.Context, pr *issues_model.PullRequest, doer *
|
||||
})
|
||||
}
|
||||
|
||||
// clearMergeIntent reverts recordMergeIntent after the merged commit failed to reach the base repo
|
||||
func clearMergeIntent(ctx context.Context, pr *issues_model.PullRequest) error {
|
||||
return db.WithTx(ctx, func(ctx context.Context) error {
|
||||
pr.MergedUnix, pr.MergedCommitID, pr.MergerID = 0, "", 0
|
||||
if _, err := db.GetEngine(ctx).NoAutoTime().ID(pr.ID).Cols("merged_unix", "merged_commit_id", "merger_id").Update(pr); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := db.GetEngine(ctx).Where("pull_id=?", pr.ID).Cols("merged_commit_id").Update(&pull_model.AutoMerge{})
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func hasPullRequestCommitBeenMerged(ctx context.Context, pr *issues_model.PullRequest) (bool, error) {
|
||||
if pr.MergedCommitID == "" {
|
||||
return false, nil
|
||||
@@ -286,6 +298,7 @@ func hasPullRequestCommitBeenMerged(ctx context.Context, pr *issues_model.PullRe
|
||||
if err := pr.LoadBaseRepo(ctx); err != nil {
|
||||
return false, err
|
||||
}
|
||||
// the recorded commit is missing from the base repo if the push never succeeded, then it is not merged
|
||||
return git.IsCommitInBranch(ctx, pr.BaseRepo, pr.MergedCommitID, pr.BaseBranch)
|
||||
}
|
||||
|
||||
@@ -340,6 +353,12 @@ func Merge(outerCtx context.Context, prID int64, doer *user_model.User, mergeSty
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
// clear the intent only if the commit verifiably didn't reach the base branch
|
||||
if merged, checkErr := hasPullRequestCommitBeenMerged(ctx, pr); checkErr == nil && !merged && pr.MergedCommitID != "" {
|
||||
if clearErr := clearMergeIntent(ctx, pr); clearErr != nil {
|
||||
log.Error("Unable to clear merge intent of %-v: %v", pr, clearErr)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user