aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2022-10-11 17:26:22 +0100
committerGitHub <noreply@github.com>2022-10-11 12:26:22 -0400
commit4a2321210233e0b92c6e5894483e0929fb157a1b (patch)
treedc7572aa6b906dd812cb33f7f404129b3e0e1518
parent5aceedc7fda39546a1e9fda423eb51a8286f86a7 (diff)
downloadgitea-4a2321210233e0b92c6e5894483e0929fb157a1b.tar.gz
gitea-4a2321210233e0b92c6e5894483e0929fb157a1b.zip
Do DB update after merge in hammer context (#21401)
When merge was changed to run in the background context, the db updates were still running in request context. This means that the merge could be successful but the db not be updated. This PR changes both these to run in the hammer context, this is not complete rollback protection but it's much better. Fix #21332 Signed-off-by: Andrew Thornton <art27@cantab.net> Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
-rw-r--r--services/pull/merge.go22
1 files changed, 12 insertions, 10 deletions
diff --git a/services/pull/merge.go b/services/pull/merge.go
index 4cd4e3bd7e..6f3df6ab2a 100644
--- a/services/pull/merge.go
+++ b/services/pull/merge.go
@@ -28,6 +28,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/references"
@@ -165,9 +166,10 @@ func Merge(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.U
go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false, "", "")
}()
- // TODO: make it able to do this in a database session
- mergeCtx := context.Background()
- pr.MergedCommitID, err = rawMerge(mergeCtx, pr, doer, mergeStyle, expectedHeadCommitID, message)
+ // Run the merge in the hammer context to prevent cancellation
+ hammerCtx := graceful.GetManager().HammerContext()
+
+ pr.MergedCommitID, err = rawMerge(hammerCtx, pr, doer, mergeStyle, expectedHeadCommitID, message)
if err != nil {
return err
}
@@ -176,18 +178,18 @@ func Merge(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.U
pr.Merger = doer
pr.MergerID = doer.ID
- if _, err := pr.SetMerged(ctx); err != nil {
+ if _, err := pr.SetMerged(hammerCtx); err != nil {
log.Error("setMerged [%d]: %v", pr.ID, err)
}
- if err := pr.LoadIssueCtx(ctx); err != nil {
+ if err := pr.LoadIssueCtx(hammerCtx); err != nil {
log.Error("loadIssue [%d]: %v", pr.ID, err)
}
- if err := pr.Issue.LoadRepo(ctx); err != nil {
+ if err := pr.Issue.LoadRepo(hammerCtx); err != nil {
log.Error("loadRepo for issue [%d]: %v", pr.ID, err)
}
- if err := pr.Issue.Repo.GetOwner(ctx); err != nil {
+ if err := pr.Issue.Repo.GetOwner(hammerCtx); err != nil {
log.Error("GetOwner for issue repo [%d]: %v", pr.ID, err)
}
@@ -197,17 +199,17 @@ func Merge(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.U
cache.Remove(pr.Issue.Repo.GetCommitsCountCacheKey(pr.BaseBranch, true))
// Resolve cross references
- refs, err := pr.ResolveCrossReferences(ctx)
+ refs, err := pr.ResolveCrossReferences(hammerCtx)
if err != nil {
log.Error("ResolveCrossReferences: %v", err)
return nil
}
for _, ref := range refs {
- if err = ref.LoadIssueCtx(ctx); err != nil {
+ if err = ref.LoadIssueCtx(hammerCtx); err != nil {
return err
}
- if err = ref.Issue.LoadRepo(ctx); err != nil {
+ if err = ref.Issue.LoadRepo(hammerCtx); err != nil {
return err
}
close := ref.RefAction == references.XRefActionCloses