summaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-04-15 02:18:28 +0800
committerGitHub <noreply@github.com>2023-04-14 14:18:28 -0400
commitcfe3d6e9b507a9331328e55ff98a1f582abae185 (patch)
tree94b035bd2859291a16edda27822b570bdd611a35 /services
parentb667634b32e240f3f3260ab7e304f72f4ff75659 (diff)
downloadgitea-cfe3d6e9b507a9331328e55ff98a1f582abae185.tar.gz
gitea-cfe3d6e9b507a9331328e55ff98a1f582abae185.zip
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.
Diffstat (limited to 'services')
-rw-r--r--services/issue/assignee.go25
-rw-r--r--services/issue/assignee_test.go2
-rw-r--r--services/issue/comments.go8
-rw-r--r--services/issue/issue.go57
-rw-r--r--services/issue/issue_test.go6
-rw-r--r--services/pull/comment.go2
-rw-r--r--services/pull/pull.go6
-rw-r--r--services/pull/review.go4
8 files changed, 55 insertions, 55 deletions
diff --git a/services/issue/assignee.go b/services/issue/assignee.go
index e5e1456c3f..4d0224d4bf 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 114ace078e..43b24e1d1f 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 1323fb47aa..4fe07c17b9 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 b91ee4fc18..d4f827e99a 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 b67d2e2e79..da0e97c23c 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 933ad09a85..24dfd8af0c 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 fe2f002010..55dfd3c180 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 ba93b5e2f5..6feffe4ec4 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,