From cfe3d6e9b507a9331328e55ff98a1f582abae185 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 15 Apr 2023 02:18:28 +0800 Subject: [PATCH] Make more functions use ctx instead of db.DefaultContext (#24068) Continue the "ctx refactoring" work. There are still a lot db.DefaultContext, incorrect context could cause database deadlock errors. --- models/issues/assignees.go | 4 +-- models/issues/assignees_test.go | 6 ++-- models/issues/issue.go | 4 +-- models/issues/issue_xref_test.go | 2 +- routers/api/v1/repo/issue.go | 8 ++--- routers/api/v1/repo/pull.go | 2 +- routers/api/v1/repo/pull_review.go | 4 +-- routers/web/repo/issue.go | 16 ++++----- services/issue/assignee.go | 25 +++++++------ services/issue/assignee_test.go | 2 +- services/issue/comments.go | 8 ++--- services/issue/issue.go | 57 +++++++++++++++--------------- services/issue/issue_test.go | 6 ++-- services/pull/comment.go | 2 +- services/pull/pull.go | 6 ++-- services/pull/review.go | 4 +-- tests/integration/api_pull_test.go | 2 +- 17 files changed, 79 insertions(+), 79 deletions(-) diff --git a/models/issues/assignees.go b/models/issues/assignees.go index 16f675a83..fdd0d6f22 100644 --- a/models/issues/assignees.go +++ b/models/issues/assignees.go @@ -63,8 +63,8 @@ func IsUserAssignedToIssue(ctx context.Context, issue *Issue, user *user_model.U } // ToggleIssueAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it. -func ToggleIssueAssignee(issue *Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *Comment, err error) { - ctx, committer, err := db.TxContext(db.DefaultContext) +func ToggleIssueAssignee(ctx context.Context, issue *Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *Comment, err error) { + ctx, committer, err := db.TxContext(ctx) if err != nil { return false, nil, err } diff --git a/models/issues/assignees_test.go b/models/issues/assignees_test.go index 8a2cef8ac..2185f6fc4 100644 --- a/models/issues/assignees_test.go +++ b/models/issues/assignees_test.go @@ -24,17 +24,17 @@ func TestUpdateAssignee(t *testing.T) { // Assign multiple users user2, err := user_model.GetUserByID(db.DefaultContext, 2) assert.NoError(t, err) - _, _, err = issues_model.ToggleIssueAssignee(issue, &user_model.User{ID: 1}, user2.ID) + _, _, err = issues_model.ToggleIssueAssignee(db.DefaultContext, issue, &user_model.User{ID: 1}, user2.ID) assert.NoError(t, err) user3, err := user_model.GetUserByID(db.DefaultContext, 3) assert.NoError(t, err) - _, _, err = issues_model.ToggleIssueAssignee(issue, &user_model.User{ID: 1}, user3.ID) + _, _, err = issues_model.ToggleIssueAssignee(db.DefaultContext, issue, &user_model.User{ID: 1}, user3.ID) assert.NoError(t, err) user1, err := user_model.GetUserByID(db.DefaultContext, 1) // This user is already assigned (see the definition in fixtures), so running UpdateAssignee should unassign him assert.NoError(t, err) - _, _, err = issues_model.ToggleIssueAssignee(issue, &user_model.User{ID: 1}, user1.ID) + _, _, err = issues_model.ToggleIssueAssignee(db.DefaultContext, issue, &user_model.User{ID: 1}, user1.ID) assert.NoError(t, err) // Check if he got removed diff --git a/models/issues/issue.go b/models/issues/issue.go index 64b0edd3e..583603047 100644 --- a/models/issues/issue.go +++ b/models/issues/issue.go @@ -743,8 +743,8 @@ func ChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.User, } // ChangeIssueTitle changes the title of this issue, as the given user. -func ChangeIssueTitle(issue *Issue, doer *user_model.User, oldTitle string) (err error) { - ctx, committer, err := db.TxContext(db.DefaultContext) +func ChangeIssueTitle(ctx context.Context, issue *Issue, doer *user_model.User, oldTitle string) (err error) { + ctx, committer, err := db.TxContext(ctx) if err != nil { return err } diff --git a/models/issues/issue_xref_test.go b/models/issues/issue_xref_test.go index 6d96c398d..6e94c2627 100644 --- a/models/issues/issue_xref_test.go +++ b/models/issues/issue_xref_test.go @@ -83,7 +83,7 @@ func TestXRef_NeuterCrossReferences(t *testing.T) { d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) i.Title = "title2, no mentions" - assert.NoError(t, issues_model.ChangeIssueTitle(i, d, title)) + assert.NoError(t, issues_model.ChangeIssueTitle(db.DefaultContext, i, d, title)) ref = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}) assert.Equal(t, issues_model.CommentTypeIssueRef, ref.Type) diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index 06bf06b4e..5bf5fc8c8 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -651,7 +651,7 @@ func CreateIssue(ctx *context.APIContext) { form.Labels = make([]int64, 0) } - if err := issue_service.NewIssue(ctx.Repo.Repository, issue, form.Labels, nil, assigneeIDs); err != nil { + if err := issue_service.NewIssue(ctx, ctx.Repo.Repository, issue, form.Labels, nil, assigneeIDs); err != nil { if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) { ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err) return @@ -752,7 +752,7 @@ func EditIssue(ctx *context.APIContext) { issue.Content = *form.Body } if form.Ref != nil { - err = issue_service.ChangeIssueRef(issue, ctx.Doer, *form.Ref) + err = issue_service.ChangeIssueRef(ctx, issue, ctx.Doer, *form.Ref) if err != nil { ctx.Error(http.StatusInternalServerError, "UpdateRef", err) return @@ -790,7 +790,7 @@ func EditIssue(ctx *context.APIContext) { oneAssignee = *form.Assignee } - err = issue_service.UpdateAssignees(issue, oneAssignee, form.Assignees, ctx.Doer) + err = issue_service.UpdateAssignees(ctx, issue, oneAssignee, form.Assignees, ctx.Doer) if err != nil { ctx.Error(http.StatusInternalServerError, "UpdateAssignees", err) return @@ -887,7 +887,7 @@ func DeleteIssue(ctx *context.APIContext) { return } - if err = issue_service.DeleteIssue(ctx.Doer, ctx.Repo.GitRepo, issue); err != nil { + if err = issue_service.DeleteIssue(ctx, ctx.Doer, ctx.Repo.GitRepo, issue); err != nil { ctx.Error(http.StatusInternalServerError, "DeleteIssueByID", err) return } diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 9b5ec0b3f..f4e2969d7 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -534,7 +534,7 @@ func EditPullRequest(ctx *context.APIContext) { // Send an empty array ([]) to clear all assignees from the Issue. if ctx.Repo.CanWrite(unit.TypePullRequests) && (form.Assignees != nil || len(form.Assignee) > 0) { - err = issue_service.UpdateAssignees(issue, form.Assignee, form.Assignees, ctx.Doer) + err = issue_service.UpdateAssignees(ctx, issue, form.Assignee, form.Assignees, ctx.Doer) if err != nil { if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err)) diff --git a/routers/api/v1/repo/pull_review.go b/routers/api/v1/repo/pull_review.go index 8f4b9dafe..a568cd565 100644 --- a/routers/api/v1/repo/pull_review.go +++ b/routers/api/v1/repo/pull_review.go @@ -706,7 +706,7 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions } for _, reviewer := range reviewers { - comment, err := issue_service.ReviewRequest(pr.Issue, ctx.Doer, reviewer, isAdd) + comment, err := issue_service.ReviewRequest(ctx, pr.Issue, ctx.Doer, reviewer, isAdd) if err != nil { ctx.Error(http.StatusInternalServerError, "ReviewRequest", err) return @@ -750,7 +750,7 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions } for _, teamReviewer := range teamReviewers { - comment, err := issue_service.TeamReviewRequest(pr.Issue, ctx.Doer, teamReviewer, isAdd) + comment, err := issue_service.TeamReviewRequest(ctx, pr.Issue, ctx.Doer, teamReviewer, isAdd) if err != nil { ctx.ServerError("TeamReviewRequest", err) return diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index d251f2043..fb61ec00d 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -964,7 +964,7 @@ func DeleteIssue(ctx *context.Context) { return } - if err := issue_service.DeleteIssue(ctx.Doer, ctx.Repo.GitRepo, issue); err != nil { + if err := issue_service.DeleteIssue(ctx, ctx.Doer, ctx.Repo.GitRepo, issue); err != nil { ctx.ServerError("DeleteIssueByID", err) return } @@ -1132,7 +1132,7 @@ func NewIssuePost(ctx *context.Context) { Ref: form.Ref, } - if err := issue_service.NewIssue(repo, issue, labelIDs, attachments, assigneeIDs); err != nil { + if err := issue_service.NewIssue(ctx, repo, issue, labelIDs, attachments, assigneeIDs); err != nil { if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) { ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error()) return @@ -2013,7 +2013,7 @@ func UpdateIssueTitle(ctx *context.Context) { return } - if err := issue_service.ChangeTitle(issue, ctx.Doer, title); err != nil { + if err := issue_service.ChangeTitle(ctx, issue, ctx.Doer, title); err != nil { ctx.ServerError("ChangeTitle", err) return } @@ -2037,7 +2037,7 @@ func UpdateIssueRef(ctx *context.Context) { ref := ctx.FormTrim("ref") - if err := issue_service.ChangeIssueRef(issue, ctx.Doer, ref); err != nil { + if err := issue_service.ChangeIssueRef(ctx, issue, ctx.Doer, ref); err != nil { ctx.ServerError("ChangeRef", err) return } @@ -2161,7 +2161,7 @@ func UpdateIssueAssignee(ctx *context.Context) { for _, issue := range issues { switch action { case "clear": - if err := issue_service.DeleteNotPassedAssignee(issue, ctx.Doer, []*user_model.User{}); err != nil { + if err := issue_service.DeleteNotPassedAssignee(ctx, issue, ctx.Doer, []*user_model.User{}); err != nil { ctx.ServerError("ClearAssignees", err) return } @@ -2182,7 +2182,7 @@ func UpdateIssueAssignee(ctx *context.Context) { return } - _, _, err = issue_service.ToggleAssignee(issue, ctx.Doer, assigneeID) + _, _, err = issue_service.ToggleAssignee(ctx, issue, ctx.Doer, assigneeID) if err != nil { ctx.ServerError("ToggleAssignee", err) return @@ -2269,7 +2269,7 @@ func UpdatePullReviewRequest(ctx *context.Context) { return } - _, err = issue_service.TeamReviewRequest(issue, ctx.Doer, team, action == "attach") + _, err = issue_service.TeamReviewRequest(ctx, issue, ctx.Doer, team, action == "attach") if err != nil { ctx.ServerError("TeamReviewRequest", err) return @@ -2307,7 +2307,7 @@ func UpdatePullReviewRequest(ctx *context.Context) { return } - _, err = issue_service.ReviewRequest(issue, ctx.Doer, reviewer, action == "attach") + _, err = issue_service.ReviewRequest(ctx, issue, ctx.Doer, reviewer, action == "attach") if err != nil { ctx.ServerError("ReviewRequest", err) return diff --git a/services/issue/assignee.go b/services/issue/assignee.go index e5e1456c3..4d0224d4b 100644 --- a/services/issue/assignee.go +++ b/services/issue/assignee.go @@ -6,7 +6,6 @@ package issue import ( "context" - "code.gitea.io/gitea/models/db" issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" @@ -18,7 +17,7 @@ import ( ) // DeleteNotPassedAssignee deletes all assignees who aren't passed via the "assignees" array -func DeleteNotPassedAssignee(issue *issues_model.Issue, doer *user_model.User, assignees []*user_model.User) (err error) { +func DeleteNotPassedAssignee(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, assignees []*user_model.User) (err error) { var found bool oriAssignes := make([]*user_model.User, len(issue.Assignees)) _ = copy(oriAssignes, issue.Assignees) @@ -34,7 +33,7 @@ func DeleteNotPassedAssignee(issue *issues_model.Issue, doer *user_model.User, a if !found { // This function also does comments and hooks, which is why we call it separately instead of directly removing the assignees here - if _, _, err := ToggleAssignee(issue, doer, assignee.ID); err != nil { + if _, _, err := ToggleAssignee(ctx, issue, doer, assignee.ID); err != nil { return err } } @@ -44,25 +43,25 @@ func DeleteNotPassedAssignee(issue *issues_model.Issue, doer *user_model.User, a } // ToggleAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it. -func ToggleAssignee(issue *issues_model.Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *issues_model.Comment, err error) { - removed, comment, err = issues_model.ToggleIssueAssignee(issue, doer, assigneeID) +func ToggleAssignee(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *issues_model.Comment, err error) { + removed, comment, err = issues_model.ToggleIssueAssignee(ctx, issue, doer, assigneeID) if err != nil { return } - assignee, err1 := user_model.GetUserByID(db.DefaultContext, assigneeID) + assignee, err1 := user_model.GetUserByID(ctx, assigneeID) if err1 != nil { err = err1 return } - notification.NotifyIssueChangeAssignee(db.DefaultContext, doer, issue, assignee, removed, comment) + notification.NotifyIssueChangeAssignee(ctx, doer, issue, assignee, removed, comment) return removed, comment, err } // ReviewRequest add or remove a review request from a user for this PR, and make comment for it. -func ReviewRequest(issue *issues_model.Issue, doer, reviewer *user_model.User, isAdd bool) (comment *issues_model.Comment, err error) { +func ReviewRequest(ctx context.Context, issue *issues_model.Issue, doer, reviewer *user_model.User, isAdd bool) (comment *issues_model.Comment, err error) { if isAdd { comment, err = issues_model.AddReviewRequest(issue, reviewer, doer) } else { @@ -74,7 +73,7 @@ func ReviewRequest(issue *issues_model.Issue, doer, reviewer *user_model.User, i } if comment != nil { - notification.NotifyPullReviewRequest(db.DefaultContext, doer, issue, reviewer, isAdd, comment) + notification.NotifyPullReviewRequest(ctx, doer, issue, reviewer, isAdd, comment) } return comment, err @@ -229,7 +228,7 @@ func IsValidTeamReviewRequest(ctx context.Context, reviewer *organization.Team, } // TeamReviewRequest add or remove a review request from a team for this PR, and make comment for it. -func TeamReviewRequest(issue *issues_model.Issue, doer *user_model.User, reviewer *organization.Team, isAdd bool) (comment *issues_model.Comment, err error) { +func TeamReviewRequest(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, reviewer *organization.Team, isAdd bool) (comment *issues_model.Comment, err error) { if isAdd { comment, err = issues_model.AddTeamReviewRequest(issue, reviewer, doer) } else { @@ -245,11 +244,11 @@ func TeamReviewRequest(issue *issues_model.Issue, doer *user_model.User, reviewe } // notify all user in this team - if err = comment.LoadIssue(db.DefaultContext); err != nil { + if err = comment.LoadIssue(ctx); err != nil { return } - members, err := organization.GetTeamMembers(db.DefaultContext, &organization.SearchMembersOptions{ + members, err := organization.GetTeamMembers(ctx, &organization.SearchMembersOptions{ TeamID: reviewer.ID, }) if err != nil { @@ -261,7 +260,7 @@ func TeamReviewRequest(issue *issues_model.Issue, doer *user_model.User, reviewe continue } comment.AssigneeID = member.ID - notification.NotifyPullReviewRequest(db.DefaultContext, doer, issue, member, isAdd, comment) + notification.NotifyPullReviewRequest(ctx, doer, issue, member, isAdd, comment) } return comment, err diff --git a/services/issue/assignee_test.go b/services/issue/assignee_test.go index 114ace078..43b24e1d1 100644 --- a/services/issue/assignee_test.go +++ b/services/issue/assignee_test.go @@ -31,7 +31,7 @@ func TestDeleteNotPassedAssignee(t *testing.T) { assert.True(t, isAssigned) // Clean everyone - err = DeleteNotPassedAssignee(issue, user1, []*user_model.User{}) + err = DeleteNotPassedAssignee(db.DefaultContext, issue, user1, []*user_model.User{}) assert.NoError(t, err) assert.EqualValues(t, 0, len(issue.Assignees)) diff --git a/services/issue/comments.go b/services/issue/comments.go index 1323fb47a..4fe07c17b 100644 --- a/services/issue/comments.go +++ b/services/issue/comments.go @@ -16,8 +16,8 @@ import ( ) // CreateComment creates comment of issue or commit. -func CreateComment(opts *issues_model.CreateCommentOptions) (comment *issues_model.Comment, err error) { - ctx, committer, err := db.TxContext(db.DefaultContext) +func CreateComment(ctx context.Context, opts *issues_model.CreateCommentOptions) (comment *issues_model.Comment, err error) { + ctx, committer, err := db.TxContext(ctx) if err != nil { return nil, err } @@ -53,7 +53,7 @@ func CreateRefComment(doer *user_model.User, repo *repo_model.Repository, issue return nil } - _, err = CreateComment(&issues_model.CreateCommentOptions{ + _, err = CreateComment(db.DefaultContext, &issues_model.CreateCommentOptions{ Type: issues_model.CommentTypeCommitRef, Doer: doer, Repo: repo, @@ -66,7 +66,7 @@ func CreateRefComment(doer *user_model.User, repo *repo_model.Repository, issue // CreateIssueComment creates a plain issue comment. func CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content string, attachments []string) (*issues_model.Comment, error) { - comment, err := CreateComment(&issues_model.CreateCommentOptions{ + comment, err := CreateComment(ctx, &issues_model.CreateCommentOptions{ Type: issues_model.CommentTypeComment, Doer: doer, Repo: repo, diff --git a/services/issue/issue.go b/services/issue/issue.go index b91ee4fc1..d4f827e99 100644 --- a/services/issue/issue.go +++ b/services/issue/issue.go @@ -4,6 +4,7 @@ package issue import ( + "context" "fmt" activities_model "code.gitea.io/gitea/models/activities" @@ -20,49 +21,49 @@ import ( ) // NewIssue creates new issue with labels for repository. -func NewIssue(repo *repo_model.Repository, issue *issues_model.Issue, labelIDs []int64, uuids []string, assigneeIDs []int64) error { +func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *issues_model.Issue, labelIDs []int64, uuids []string, assigneeIDs []int64) error { if err := issues_model.NewIssue(repo, issue, labelIDs, uuids); err != nil { return err } for _, assigneeID := range assigneeIDs { - if err := AddAssigneeIfNotAssigned(issue, issue.Poster, assigneeID); err != nil { + if err := AddAssigneeIfNotAssigned(ctx, issue, issue.Poster, assigneeID); err != nil { return err } } - mentions, err := issues_model.FindAndUpdateIssueMentions(db.DefaultContext, issue, issue.Poster, issue.Content) + mentions, err := issues_model.FindAndUpdateIssueMentions(ctx, issue, issue.Poster, issue.Content) if err != nil { return err } - notification.NotifyNewIssue(db.DefaultContext, issue, mentions) + notification.NotifyNewIssue(ctx, issue, mentions) if len(issue.Labels) > 0 { - notification.NotifyIssueChangeLabels(db.DefaultContext, issue.Poster, issue, issue.Labels, nil) + notification.NotifyIssueChangeLabels(ctx, issue.Poster, issue, issue.Labels, nil) } if issue.Milestone != nil { - notification.NotifyIssueChangeMilestone(db.DefaultContext, issue.Poster, issue, 0) + notification.NotifyIssueChangeMilestone(ctx, issue.Poster, issue, 0) } return nil } // ChangeTitle changes the title of this issue, as the given user. -func ChangeTitle(issue *issues_model.Issue, doer *user_model.User, title string) (err error) { +func ChangeTitle(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, title string) (err error) { oldTitle := issue.Title issue.Title = title - if err = issues_model.ChangeIssueTitle(issue, doer, oldTitle); err != nil { + if err = issues_model.ChangeIssueTitle(ctx, issue, doer, oldTitle); err != nil { return } - notification.NotifyIssueChangeTitle(db.DefaultContext, doer, issue, oldTitle) + notification.NotifyIssueChangeTitle(ctx, doer, issue, oldTitle) return nil } // ChangeIssueRef changes the branch of this issue, as the given user. -func ChangeIssueRef(issue *issues_model.Issue, doer *user_model.User, ref string) error { +func ChangeIssueRef(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, ref string) error { oldRef := issue.Ref issue.Ref = ref @@ -70,7 +71,7 @@ func ChangeIssueRef(issue *issues_model.Issue, doer *user_model.User, ref string return err } - notification.NotifyIssueChangeRef(db.DefaultContext, doer, issue, oldRef) + notification.NotifyIssueChangeRef(ctx, doer, issue, oldRef) return nil } @@ -81,7 +82,7 @@ func ChangeIssueRef(issue *issues_model.Issue, doer *user_model.User, ref string // "assignees" (array): Logins for Users to assign to this issue. // Pass one or more user logins to replace the set of assignees on this Issue. // Send an empty array ([]) to clear all assignees from the Issue. -func UpdateAssignees(issue *issues_model.Issue, oneAssignee string, multipleAssignees []string, doer *user_model.User) (err error) { +func UpdateAssignees(ctx context.Context, issue *issues_model.Issue, oneAssignee string, multipleAssignees []string, doer *user_model.User) (err error) { var allNewAssignees []*user_model.User // Keep the old assignee thingy for compatibility reasons @@ -102,7 +103,7 @@ func UpdateAssignees(issue *issues_model.Issue, oneAssignee string, multipleAssi // Loop through all assignees to add them for _, assigneeName := range multipleAssignees { - assignee, err := user_model.GetUserByName(db.DefaultContext, assigneeName) + assignee, err := user_model.GetUserByName(ctx, assigneeName) if err != nil { return err } @@ -111,7 +112,7 @@ func UpdateAssignees(issue *issues_model.Issue, oneAssignee string, multipleAssi } // Delete all old assignees not passed - if err = DeleteNotPassedAssignee(issue, doer, allNewAssignees); err != nil { + if err = DeleteNotPassedAssignee(ctx, issue, doer, allNewAssignees); err != nil { return err } @@ -121,7 +122,7 @@ func UpdateAssignees(issue *issues_model.Issue, oneAssignee string, multipleAssi // has access to the repo. for _, assignee := range allNewAssignees { // Extra method to prevent double adding (which would result in removing) - err = AddAssigneeIfNotAssigned(issue, doer, assignee.ID) + err = AddAssigneeIfNotAssigned(ctx, issue, doer, assignee.ID) if err != nil { return err } @@ -131,42 +132,42 @@ func UpdateAssignees(issue *issues_model.Issue, oneAssignee string, multipleAssi } // DeleteIssue deletes an issue -func DeleteIssue(doer *user_model.User, gitRepo *git.Repository, issue *issues_model.Issue) error { +func DeleteIssue(ctx context.Context, doer *user_model.User, gitRepo *git.Repository, issue *issues_model.Issue) error { // load issue before deleting it - if err := issue.LoadAttributes(gitRepo.Ctx); err != nil { + if err := issue.LoadAttributes(ctx); err != nil { return err } - if err := issue.LoadPullRequest(gitRepo.Ctx); err != nil { + if err := issue.LoadPullRequest(ctx); err != nil { return err } // delete entries in database - if err := deleteIssue(issue); err != nil { + if err := deleteIssue(ctx, issue); err != nil { return err } // delete pull request related git data - if issue.IsPull { + if issue.IsPull && gitRepo != nil { if err := gitRepo.RemoveReference(fmt.Sprintf("%s%d/head", git.PullPrefix, issue.PullRequest.Index)); err != nil { return err } } - notification.NotifyDeleteIssue(gitRepo.Ctx, doer, issue) + notification.NotifyDeleteIssue(ctx, doer, issue) return nil } // AddAssigneeIfNotAssigned adds an assignee only if he isn't already assigned to the issue. // Also checks for access of assigned user -func AddAssigneeIfNotAssigned(issue *issues_model.Issue, doer *user_model.User, assigneeID int64) (err error) { - assignee, err := user_model.GetUserByID(db.DefaultContext, assigneeID) +func AddAssigneeIfNotAssigned(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, assigneeID int64) (err error) { + assignee, err := user_model.GetUserByID(ctx, assigneeID) if err != nil { return err } // Check if the user is already assigned - isAssigned, err := issues_model.IsUserAssignedToIssue(db.DefaultContext, issue, assignee) + isAssigned, err := issues_model.IsUserAssignedToIssue(ctx, issue, assignee) if err != nil { return err } @@ -175,7 +176,7 @@ func AddAssigneeIfNotAssigned(issue *issues_model.Issue, doer *user_model.User, return nil } - valid, err := access_model.CanBeAssigned(db.DefaultContext, assignee, issue.Repo, issue.IsPull) + valid, err := access_model.CanBeAssigned(ctx, assignee, issue.Repo, issue.IsPull) if err != nil { return err } @@ -183,7 +184,7 @@ func AddAssigneeIfNotAssigned(issue *issues_model.Issue, doer *user_model.User, return repo_model.ErrUserDoesNotHaveAccessToRepo{UserID: assigneeID, RepoName: issue.Repo.Name} } - _, _, err = ToggleAssignee(issue, doer, assigneeID) + _, _, err = ToggleAssignee(ctx, issue, doer, assigneeID) if err != nil { return err } @@ -206,8 +207,8 @@ func GetRefEndNamesAndURLs(issues []*issues_model.Issue, repoLink string) (map[i } // deleteIssue deletes the issue -func deleteIssue(issue *issues_model.Issue) error { - ctx, committer, err := db.TxContext(db.DefaultContext) +func deleteIssue(ctx context.Context, issue *issues_model.Issue) error { + ctx, committer, err := db.TxContext(ctx) if err != nil { return err } diff --git a/services/issue/issue_test.go b/services/issue/issue_test.go index b67d2e2e7..da0e97c23 100644 --- a/services/issue/issue_test.go +++ b/services/issue/issue_test.go @@ -44,7 +44,7 @@ func TestIssue_DeleteIssue(t *testing.T) { ID: issueIDs[2], } - err = deleteIssue(issue) + err = deleteIssue(db.DefaultContext, issue) assert.NoError(t, err) issueIDs, err = issues_model.GetIssueIDsByRepoID(db.DefaultContext, 1) assert.NoError(t, err) @@ -55,7 +55,7 @@ func TestIssue_DeleteIssue(t *testing.T) { assert.NoError(t, err) issue, err = issues_model.GetIssueByID(db.DefaultContext, 4) assert.NoError(t, err) - err = deleteIssue(issue) + err = deleteIssue(db.DefaultContext, issue) assert.NoError(t, err) assert.EqualValues(t, 2, len(attachments)) for i := range attachments { @@ -78,7 +78,7 @@ func TestIssue_DeleteIssue(t *testing.T) { assert.NoError(t, err) assert.False(t, left) - err = deleteIssue(issue2) + err = deleteIssue(db.DefaultContext, issue2) assert.NoError(t, err) left, err = issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1) assert.NoError(t, err) diff --git a/services/pull/comment.go b/services/pull/comment.go index 933ad09a8..24dfd8af0 100644 --- a/services/pull/comment.go +++ b/services/pull/comment.go @@ -90,7 +90,7 @@ func CreatePushPullComment(ctx context.Context, pusher *user_model.User, pr *iss ops.Content = string(dataJSON) - comment, err = issue_service.CreateComment(ops) + comment, err = issue_service.CreateComment(ctx, ops) return comment, err } diff --git a/services/pull/pull.go b/services/pull/pull.go index fe2f00201..55dfd3c18 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -52,7 +52,7 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, pull *issu } for _, assigneeID := range assigneeIDs { - if err := issue_service.AddAssigneeIfNotAssigned(pull, pull.Poster, assigneeID); err != nil { + if err := issue_service.AddAssigneeIfNotAssigned(ctx, pull, pull.Poster, assigneeID); err != nil { return err } } @@ -122,7 +122,7 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, pull *issu Content: string(dataJSON), } - _, _ = issue_service.CreateComment(ops) + _, _ = issue_service.CreateComment(ctx, ops) } return nil @@ -221,7 +221,7 @@ func ChangeTargetBranch(ctx context.Context, pr *issues_model.PullRequest, doer OldRef: oldBranch, NewRef: targetBranch, } - if _, err = issue_service.CreateComment(options); err != nil { + if _, err = issue_service.CreateComment(ctx, options); err != nil { return fmt.Errorf("CreateChangeTargetBranchComment: %w", err) } diff --git a/services/pull/review.go b/services/pull/review.go index ba93b5e2f..6feffe4ec 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -248,7 +248,7 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo return nil, err } } - return issue_service.CreateComment(&issues_model.CreateCommentOptions{ + return issue_service.CreateComment(ctx, &issues_model.CreateCommentOptions{ Type: issues_model.CommentTypeCode, Doer: doer, Repo: repo, @@ -368,7 +368,7 @@ func DismissReview(ctx context.Context, reviewID, repoID int64, message string, return } - comment, err = issue_service.CreateComment(&issues_model.CreateCommentOptions{ + comment, err = issue_service.CreateComment(ctx, &issues_model.CreateCommentOptions{ Doer: doer, Content: message, Type: issues_model.CommentTypeDismissReview, diff --git a/tests/integration/api_pull_test.go b/tests/integration/api_pull_test.go index 4427c610b..4b25f97dd 100644 --- a/tests/integration/api_pull_test.go +++ b/tests/integration/api_pull_test.go @@ -67,7 +67,7 @@ func TestAPIMergePullWIP(t *testing.T) { owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{Status: issues_model.PullRequestStatusMergeable}, unittest.Cond("has_merged = ?", false)) pr.LoadIssue(db.DefaultContext) - issue_service.ChangeTitle(pr.Issue, owner, setting.Repository.PullRequest.WorkInProgressPrefixes[0]+" "+pr.Issue.Title) + issue_service.ChangeTitle(db.DefaultContext, pr.Issue, owner, setting.Repository.PullRequest.WorkInProgressPrefixes[0]+" "+pr.Issue.Title) // force reload pr.LoadAttributes(db.DefaultContext)