diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-08-11 13:27:23 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-11 05:27:23 +0000 |
commit | 368e9e0f1bf1094aad881336c698f540c047d9ac (patch) | |
tree | b65c37ad8c941cbf1ae70d57af81e49c5d557653 /models | |
parent | d6cf261be860e8022ccbad5585fc3508e840d409 (diff) | |
download | gitea-368e9e0f1bf1094aad881336c698f540c047d9ac.tar.gz gitea-368e9e0f1bf1094aad881336c698f540c047d9ac.zip |
Add transaction when creating pull request created dirty data (#26259) (#26437)
Backport #26259
This PR will introduce a transaction on creating pull request so that if
some step failed, it will rollback totally. And there will be no dirty
pull request exist.
Co-authored-by: Giteabot <teabot@gitea.io>
Diffstat (limited to 'models')
-rw-r--r-- | models/issues/pull.go | 5 | ||||
-rw-r--r-- | models/issues/pull_test.go | 4 | ||||
-rw-r--r-- | models/issues/review.go | 16 |
3 files changed, 12 insertions, 13 deletions
diff --git a/models/issues/pull.go b/models/issues/pull.go index 444ae49e05..a403ebd4dd 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -531,13 +531,12 @@ func (pr *PullRequest) SetMerged(ctx context.Context) (bool, error) { } // NewPullRequest creates new pull request with labels for repository. -func NewPullRequest(outerCtx context.Context, repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) { - ctx, committer, err := db.TxContext(outerCtx) +func NewPullRequest(ctx context.Context, repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) { + ctx, committer, err := db.TxContext(ctx) if err != nil { return err } defer committer.Close() - ctx.WithContext(outerCtx) idx, err := db.GetNextResourceIndex(ctx, "issue_index", repo.ID) if err != nil { diff --git a/models/issues/pull_test.go b/models/issues/pull_test.go index 7a183ac312..81f64e0104 100644 --- a/models/issues/pull_test.go +++ b/models/issues/pull_test.go @@ -87,14 +87,14 @@ func TestLoadRequestedReviewers(t *testing.T) { user1, err := user_model.GetUserByID(db.DefaultContext, 1) assert.NoError(t, err) - comment, err := issues_model.AddReviewRequest(issue, user1, &user_model.User{}) + comment, err := issues_model.AddReviewRequest(db.DefaultContext, issue, user1, &user_model.User{}) assert.NoError(t, err) assert.NotNil(t, comment) assert.NoError(t, pull.LoadRequestedReviewers(db.DefaultContext)) assert.Len(t, pull.RequestedReviewers, 1) - comment, err = issues_model.RemoveReviewRequest(issue, user1, &user_model.User{}) + comment, err = issues_model.RemoveReviewRequest(db.DefaultContext, issue, user1, &user_model.User{}) assert.NoError(t, err) assert.NotNil(t, comment) diff --git a/models/issues/review.go b/models/issues/review.go index 83ca7d3d87..124f69d00d 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -557,8 +557,8 @@ func InsertReviews(reviews []*Review) error { } // AddReviewRequest add a review request from one reviewer -func AddReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Comment, error) { - ctx, committer, err := db.TxContext(db.DefaultContext) +func AddReviewRequest(ctx context.Context, issue *Issue, reviewer, doer *user_model.User) (*Comment, error) { + ctx, committer, err := db.TxContext(ctx) if err != nil { return nil, err } @@ -612,8 +612,8 @@ func AddReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Comment, } // RemoveReviewRequest remove a review request from one reviewer -func RemoveReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Comment, error) { - ctx, committer, err := db.TxContext(db.DefaultContext) +func RemoveReviewRequest(ctx context.Context, issue *Issue, reviewer, doer *user_model.User) (*Comment, error) { + ctx, committer, err := db.TxContext(ctx) if err != nil { return nil, err } @@ -673,8 +673,8 @@ func restoreLatestOfficialReview(ctx context.Context, issueID, reviewerID int64) } // AddTeamReviewRequest add a review request from one team -func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_model.User) (*Comment, error) { - ctx, committer, err := db.TxContext(db.DefaultContext) +func AddTeamReviewRequest(ctx context.Context, issue *Issue, reviewer *organization.Team, doer *user_model.User) (*Comment, error) { + ctx, committer, err := db.TxContext(ctx) if err != nil { return nil, err } @@ -732,8 +732,8 @@ func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_ } // RemoveTeamReviewRequest remove a review request from one team -func RemoveTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_model.User) (*Comment, error) { - ctx, committer, err := db.TxContext(db.DefaultContext) +func RemoveTeamReviewRequest(ctx context.Context, issue *Issue, reviewer *organization.Team, doer *user_model.User) (*Comment, error) { + ctx, committer, err := db.TxContext(ctx) if err != nil { return nil, err } |