forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move database operations of merging a pull request to post receive ho…
…ok and add a transaction (go-gitea#30805) Merging PR may fail because of various problems. The pull request may have a dirty state because there is no transaction when merging a pull request. ref go-gitea#25741 (comment) This PR moves all database update operations to post-receive handler for merging a pull request and having a database transaction. That means if database operations fail, then the git merging will fail, the git client will get a fail result. There are already many tests for pull request merging, so we don't need to add a new one. --------- Co-authored-by: wxiaoguang <[email protected]>
- Loading branch information
1 parent
6ad7712
commit ebf0c96
Showing
8 changed files
with
150 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package private | ||
|
||
import ( | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
issues_model "code.gitea.io/gitea/models/issues" | ||
pull_model "code.gitea.io/gitea/models/pull" | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/models/unittest" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/private" | ||
repo_module "code.gitea.io/gitea/modules/repository" | ||
"code.gitea.io/gitea/services/contexttest" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHandlePullRequestMerging(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
pr, err := issues_model.GetUnmergedPullRequest(db.DefaultContext, 1, 1, "branch2", "master", issues_model.PullRequestFlowGithub) | ||
assert.NoError(t, err) | ||
assert.NoError(t, pr.LoadBaseRepo(db.DefaultContext)) | ||
|
||
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) | ||
|
||
err = pull_model.ScheduleAutoMerge(db.DefaultContext, user1, pr.ID, repo_model.MergeStyleSquash, "squash merge a pr") | ||
assert.NoError(t, err) | ||
|
||
autoMerge := unittest.AssertExistsAndLoadBean(t, &pull_model.AutoMerge{PullID: pr.ID}) | ||
|
||
ctx, resp := contexttest.MockPrivateContext(t, "/") | ||
handlePullRequestMerging(ctx, &private.HookOptions{ | ||
PullRequestID: pr.ID, | ||
UserID: 2, | ||
}, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, []*repo_module.PushUpdateOptions{ | ||
{NewCommitID: "01234567"}, | ||
}) | ||
assert.Equal(t, 0, len(resp.Body.String())) | ||
pr, err = issues_model.GetPullRequestByID(db.DefaultContext, pr.ID) | ||
assert.NoError(t, err) | ||
assert.True(t, pr.HasMerged) | ||
assert.EqualValues(t, "01234567", pr.MergedCommitID) | ||
|
||
unittest.AssertNotExistsBean(t, &pull_model.AutoMerge{ID: autoMerge.ID}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters