mirror of
https://github.com/go-gitea/gitea.git
synced 2024-11-04 08:17:24 -05:00
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.
This commit is contained in:
parent
b667634b32
commit
cfe3d6e9b5
@ -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.
|
// 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) {
|
func ToggleIssueAssignee(ctx context.Context, issue *Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *Comment, err error) {
|
||||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
ctx, committer, err := db.TxContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, nil, err
|
return false, nil, err
|
||||||
}
|
}
|
||||||
|
@ -24,17 +24,17 @@ func TestUpdateAssignee(t *testing.T) {
|
|||||||
// Assign multiple users
|
// Assign multiple users
|
||||||
user2, err := user_model.GetUserByID(db.DefaultContext, 2)
|
user2, err := user_model.GetUserByID(db.DefaultContext, 2)
|
||||||
assert.NoError(t, err)
|
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)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
user3, err := user_model.GetUserByID(db.DefaultContext, 3)
|
user3, err := user_model.GetUserByID(db.DefaultContext, 3)
|
||||||
assert.NoError(t, err)
|
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)
|
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
|
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)
|
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)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// Check if he got removed
|
// Check if he got removed
|
||||||
|
@ -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.
|
// ChangeIssueTitle changes the title of this issue, as the given user.
|
||||||
func ChangeIssueTitle(issue *Issue, doer *user_model.User, oldTitle string) (err error) {
|
func ChangeIssueTitle(ctx context.Context, issue *Issue, doer *user_model.User, oldTitle string) (err error) {
|
||||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
ctx, committer, err := db.TxContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,7 @@ func TestXRef_NeuterCrossReferences(t *testing.T) {
|
|||||||
|
|
||||||
d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||||
i.Title = "title2, no mentions"
|
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})
|
ref = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0})
|
||||||
assert.Equal(t, issues_model.CommentTypeIssueRef, ref.Type)
|
assert.Equal(t, issues_model.CommentTypeIssueRef, ref.Type)
|
||||||
|
@ -651,7 +651,7 @@ func CreateIssue(ctx *context.APIContext) {
|
|||||||
form.Labels = make([]int64, 0)
|
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) {
|
if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) {
|
||||||
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err)
|
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err)
|
||||||
return
|
return
|
||||||
@ -752,7 +752,7 @@ func EditIssue(ctx *context.APIContext) {
|
|||||||
issue.Content = *form.Body
|
issue.Content = *form.Body
|
||||||
}
|
}
|
||||||
if form.Ref != nil {
|
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 {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError, "UpdateRef", err)
|
ctx.Error(http.StatusInternalServerError, "UpdateRef", err)
|
||||||
return
|
return
|
||||||
@ -790,7 +790,7 @@ func EditIssue(ctx *context.APIContext) {
|
|||||||
oneAssignee = *form.Assignee
|
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 {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError, "UpdateAssignees", err)
|
ctx.Error(http.StatusInternalServerError, "UpdateAssignees", err)
|
||||||
return
|
return
|
||||||
@ -887,7 +887,7 @@ func DeleteIssue(ctx *context.APIContext) {
|
|||||||
return
|
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)
|
ctx.Error(http.StatusInternalServerError, "DeleteIssueByID", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -534,7 +534,7 @@ func EditPullRequest(ctx *context.APIContext) {
|
|||||||
// Send an empty array ([]) to clear all assignees from the Issue.
|
// Send an empty array ([]) to clear all assignees from the Issue.
|
||||||
|
|
||||||
if ctx.Repo.CanWrite(unit.TypePullRequests) && (form.Assignees != nil || len(form.Assignee) > 0) {
|
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 err != nil {
|
||||||
if user_model.IsErrUserNotExist(err) {
|
if user_model.IsErrUserNotExist(err) {
|
||||||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
|
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
|
||||||
|
@ -706,7 +706,7 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, reviewer := range reviewers {
|
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 {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError, "ReviewRequest", err)
|
ctx.Error(http.StatusInternalServerError, "ReviewRequest", err)
|
||||||
return
|
return
|
||||||
@ -750,7 +750,7 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, teamReviewer := range teamReviewers {
|
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 {
|
if err != nil {
|
||||||
ctx.ServerError("TeamReviewRequest", err)
|
ctx.ServerError("TeamReviewRequest", err)
|
||||||
return
|
return
|
||||||
|
@ -964,7 +964,7 @@ func DeleteIssue(ctx *context.Context) {
|
|||||||
return
|
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)
|
ctx.ServerError("DeleteIssueByID", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1132,7 +1132,7 @@ func NewIssuePost(ctx *context.Context) {
|
|||||||
Ref: form.Ref,
|
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) {
|
if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) {
|
||||||
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error())
|
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error())
|
||||||
return
|
return
|
||||||
@ -2013,7 +2013,7 @@ func UpdateIssueTitle(ctx *context.Context) {
|
|||||||
return
|
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)
|
ctx.ServerError("ChangeTitle", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -2037,7 +2037,7 @@ func UpdateIssueRef(ctx *context.Context) {
|
|||||||
|
|
||||||
ref := ctx.FormTrim("ref")
|
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)
|
ctx.ServerError("ChangeRef", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -2161,7 +2161,7 @@ func UpdateIssueAssignee(ctx *context.Context) {
|
|||||||
for _, issue := range issues {
|
for _, issue := range issues {
|
||||||
switch action {
|
switch action {
|
||||||
case "clear":
|
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)
|
ctx.ServerError("ClearAssignees", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -2182,7 +2182,7 @@ func UpdateIssueAssignee(ctx *context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = issue_service.ToggleAssignee(issue, ctx.Doer, assigneeID)
|
_, _, err = issue_service.ToggleAssignee(ctx, issue, ctx.Doer, assigneeID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError("ToggleAssignee", err)
|
ctx.ServerError("ToggleAssignee", err)
|
||||||
return
|
return
|
||||||
@ -2269,7 +2269,7 @@ func UpdatePullReviewRequest(ctx *context.Context) {
|
|||||||
return
|
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 {
|
if err != nil {
|
||||||
ctx.ServerError("TeamReviewRequest", err)
|
ctx.ServerError("TeamReviewRequest", err)
|
||||||
return
|
return
|
||||||
@ -2307,7 +2307,7 @@ func UpdatePullReviewRequest(ctx *context.Context) {
|
|||||||
return
|
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 {
|
if err != nil {
|
||||||
ctx.ServerError("ReviewRequest", err)
|
ctx.ServerError("ReviewRequest", err)
|
||||||
return
|
return
|
||||||
|
@ -6,7 +6,6 @@ package issue
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
|
||||||
issues_model "code.gitea.io/gitea/models/issues"
|
issues_model "code.gitea.io/gitea/models/issues"
|
||||||
"code.gitea.io/gitea/models/organization"
|
"code.gitea.io/gitea/models/organization"
|
||||||
"code.gitea.io/gitea/models/perm"
|
"code.gitea.io/gitea/models/perm"
|
||||||
@ -18,7 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// DeleteNotPassedAssignee deletes all assignees who aren't passed via the "assignees" array
|
// 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
|
var found bool
|
||||||
oriAssignes := make([]*user_model.User, len(issue.Assignees))
|
oriAssignes := make([]*user_model.User, len(issue.Assignees))
|
||||||
_ = copy(oriAssignes, issue.Assignees)
|
_ = copy(oriAssignes, issue.Assignees)
|
||||||
@ -34,7 +33,7 @@ func DeleteNotPassedAssignee(issue *issues_model.Issue, doer *user_model.User, a
|
|||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
// This function also does comments and hooks, which is why we call it separately instead of directly removing the assignees here
|
// 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
|
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.
|
// 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) {
|
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(issue, doer, assigneeID)
|
removed, comment, err = issues_model.ToggleIssueAssignee(ctx, issue, doer, assigneeID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
assignee, err1 := user_model.GetUserByID(db.DefaultContext, assigneeID)
|
assignee, err1 := user_model.GetUserByID(ctx, assigneeID)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
err = err1
|
err = err1
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
notification.NotifyIssueChangeAssignee(db.DefaultContext, doer, issue, assignee, removed, comment)
|
notification.NotifyIssueChangeAssignee(ctx, doer, issue, assignee, removed, comment)
|
||||||
|
|
||||||
return removed, comment, err
|
return removed, comment, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReviewRequest add or remove a review request from a user for this PR, and make comment for it.
|
// 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 {
|
if isAdd {
|
||||||
comment, err = issues_model.AddReviewRequest(issue, reviewer, doer)
|
comment, err = issues_model.AddReviewRequest(issue, reviewer, doer)
|
||||||
} else {
|
} else {
|
||||||
@ -74,7 +73,7 @@ func ReviewRequest(issue *issues_model.Issue, doer, reviewer *user_model.User, i
|
|||||||
}
|
}
|
||||||
|
|
||||||
if comment != nil {
|
if comment != nil {
|
||||||
notification.NotifyPullReviewRequest(db.DefaultContext, doer, issue, reviewer, isAdd, comment)
|
notification.NotifyPullReviewRequest(ctx, doer, issue, reviewer, isAdd, comment)
|
||||||
}
|
}
|
||||||
|
|
||||||
return comment, err
|
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.
|
// 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 {
|
if isAdd {
|
||||||
comment, err = issues_model.AddTeamReviewRequest(issue, reviewer, doer)
|
comment, err = issues_model.AddTeamReviewRequest(issue, reviewer, doer)
|
||||||
} else {
|
} else {
|
||||||
@ -245,11 +244,11 @@ func TeamReviewRequest(issue *issues_model.Issue, doer *user_model.User, reviewe
|
|||||||
}
|
}
|
||||||
|
|
||||||
// notify all user in this team
|
// notify all user in this team
|
||||||
if err = comment.LoadIssue(db.DefaultContext); err != nil {
|
if err = comment.LoadIssue(ctx); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
members, err := organization.GetTeamMembers(db.DefaultContext, &organization.SearchMembersOptions{
|
members, err := organization.GetTeamMembers(ctx, &organization.SearchMembersOptions{
|
||||||
TeamID: reviewer.ID,
|
TeamID: reviewer.ID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -261,7 +260,7 @@ func TeamReviewRequest(issue *issues_model.Issue, doer *user_model.User, reviewe
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
comment.AssigneeID = member.ID
|
comment.AssigneeID = member.ID
|
||||||
notification.NotifyPullReviewRequest(db.DefaultContext, doer, issue, member, isAdd, comment)
|
notification.NotifyPullReviewRequest(ctx, doer, issue, member, isAdd, comment)
|
||||||
}
|
}
|
||||||
|
|
||||||
return comment, err
|
return comment, err
|
||||||
|
@ -31,7 +31,7 @@ func TestDeleteNotPassedAssignee(t *testing.T) {
|
|||||||
assert.True(t, isAssigned)
|
assert.True(t, isAssigned)
|
||||||
|
|
||||||
// Clean everyone
|
// Clean everyone
|
||||||
err = DeleteNotPassedAssignee(issue, user1, []*user_model.User{})
|
err = DeleteNotPassedAssignee(db.DefaultContext, issue, user1, []*user_model.User{})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, 0, len(issue.Assignees))
|
assert.EqualValues(t, 0, len(issue.Assignees))
|
||||||
|
|
||||||
|
@ -16,8 +16,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// CreateComment creates comment of issue or commit.
|
// CreateComment creates comment of issue or commit.
|
||||||
func CreateComment(opts *issues_model.CreateCommentOptions) (comment *issues_model.Comment, err error) {
|
func CreateComment(ctx context.Context, opts *issues_model.CreateCommentOptions) (comment *issues_model.Comment, err error) {
|
||||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
ctx, committer, err := db.TxContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -53,7 +53,7 @@ func CreateRefComment(doer *user_model.User, repo *repo_model.Repository, issue
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = CreateComment(&issues_model.CreateCommentOptions{
|
_, err = CreateComment(db.DefaultContext, &issues_model.CreateCommentOptions{
|
||||||
Type: issues_model.CommentTypeCommitRef,
|
Type: issues_model.CommentTypeCommitRef,
|
||||||
Doer: doer,
|
Doer: doer,
|
||||||
Repo: repo,
|
Repo: repo,
|
||||||
@ -66,7 +66,7 @@ func CreateRefComment(doer *user_model.User, repo *repo_model.Repository, issue
|
|||||||
|
|
||||||
// CreateIssueComment creates a plain issue comment.
|
// 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) {
|
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,
|
Type: issues_model.CommentTypeComment,
|
||||||
Doer: doer,
|
Doer: doer,
|
||||||
Repo: repo,
|
Repo: repo,
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
package issue
|
package issue
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
activities_model "code.gitea.io/gitea/models/activities"
|
activities_model "code.gitea.io/gitea/models/activities"
|
||||||
@ -20,49 +21,49 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// NewIssue creates new issue with labels for repository.
|
// 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 {
|
if err := issues_model.NewIssue(repo, issue, labelIDs, uuids); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, assigneeID := range assigneeIDs {
|
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
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
notification.NotifyNewIssue(db.DefaultContext, issue, mentions)
|
notification.NotifyNewIssue(ctx, issue, mentions)
|
||||||
if len(issue.Labels) > 0 {
|
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 {
|
if issue.Milestone != nil {
|
||||||
notification.NotifyIssueChangeMilestone(db.DefaultContext, issue.Poster, issue, 0)
|
notification.NotifyIssueChangeMilestone(ctx, issue.Poster, issue, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChangeTitle changes the title of this issue, as the given user.
|
// 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
|
oldTitle := issue.Title
|
||||||
issue.Title = 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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
notification.NotifyIssueChangeTitle(db.DefaultContext, doer, issue, oldTitle)
|
notification.NotifyIssueChangeTitle(ctx, doer, issue, oldTitle)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChangeIssueRef changes the branch of this issue, as the given user.
|
// 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
|
oldRef := issue.Ref
|
||||||
issue.Ref = ref
|
issue.Ref = ref
|
||||||
|
|
||||||
@ -70,7 +71,7 @@ func ChangeIssueRef(issue *issues_model.Issue, doer *user_model.User, ref string
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
notification.NotifyIssueChangeRef(db.DefaultContext, doer, issue, oldRef)
|
notification.NotifyIssueChangeRef(ctx, doer, issue, oldRef)
|
||||||
|
|
||||||
return nil
|
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.
|
// "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.
|
// 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.
|
// 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
|
var allNewAssignees []*user_model.User
|
||||||
|
|
||||||
// Keep the old assignee thingy for compatibility reasons
|
// 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
|
// Loop through all assignees to add them
|
||||||
for _, assigneeName := range multipleAssignees {
|
for _, assigneeName := range multipleAssignees {
|
||||||
assignee, err := user_model.GetUserByName(db.DefaultContext, assigneeName)
|
assignee, err := user_model.GetUserByName(ctx, assigneeName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -111,7 +112,7 @@ func UpdateAssignees(issue *issues_model.Issue, oneAssignee string, multipleAssi
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Delete all old assignees not passed
|
// 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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,7 +122,7 @@ func UpdateAssignees(issue *issues_model.Issue, oneAssignee string, multipleAssi
|
|||||||
// has access to the repo.
|
// has access to the repo.
|
||||||
for _, assignee := range allNewAssignees {
|
for _, assignee := range allNewAssignees {
|
||||||
// Extra method to prevent double adding (which would result in removing)
|
// 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -131,42 +132,42 @@ func UpdateAssignees(issue *issues_model.Issue, oneAssignee string, multipleAssi
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DeleteIssue deletes an issue
|
// 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
|
// load issue before deleting it
|
||||||
if err := issue.LoadAttributes(gitRepo.Ctx); err != nil {
|
if err := issue.LoadAttributes(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := issue.LoadPullRequest(gitRepo.Ctx); err != nil {
|
if err := issue.LoadPullRequest(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete entries in database
|
// delete entries in database
|
||||||
if err := deleteIssue(issue); err != nil {
|
if err := deleteIssue(ctx, issue); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete pull request related git data
|
// 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 {
|
if err := gitRepo.RemoveReference(fmt.Sprintf("%s%d/head", git.PullPrefix, issue.PullRequest.Index)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
notification.NotifyDeleteIssue(gitRepo.Ctx, doer, issue)
|
notification.NotifyDeleteIssue(ctx, doer, issue)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddAssigneeIfNotAssigned adds an assignee only if he isn't already assigned to the issue.
|
// AddAssigneeIfNotAssigned adds an assignee only if he isn't already assigned to the issue.
|
||||||
// Also checks for access of assigned user
|
// Also checks for access of assigned user
|
||||||
func AddAssigneeIfNotAssigned(issue *issues_model.Issue, doer *user_model.User, assigneeID int64) (err error) {
|
func AddAssigneeIfNotAssigned(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, assigneeID int64) (err error) {
|
||||||
assignee, err := user_model.GetUserByID(db.DefaultContext, assigneeID)
|
assignee, err := user_model.GetUserByID(ctx, assigneeID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the user is already assigned
|
// 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -175,7 +176,7 @@ func AddAssigneeIfNotAssigned(issue *issues_model.Issue, doer *user_model.User,
|
|||||||
return nil
|
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 {
|
if err != nil {
|
||||||
return err
|
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}
|
return repo_model.ErrUserDoesNotHaveAccessToRepo{UserID: assigneeID, RepoName: issue.Repo.Name}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = ToggleAssignee(issue, doer, assigneeID)
|
_, _, err = ToggleAssignee(ctx, issue, doer, assigneeID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -206,8 +207,8 @@ func GetRefEndNamesAndURLs(issues []*issues_model.Issue, repoLink string) (map[i
|
|||||||
}
|
}
|
||||||
|
|
||||||
// deleteIssue deletes the issue
|
// deleteIssue deletes the issue
|
||||||
func deleteIssue(issue *issues_model.Issue) error {
|
func deleteIssue(ctx context.Context, issue *issues_model.Issue) error {
|
||||||
ctx, committer, err := db.TxContext(db.DefaultContext)
|
ctx, committer, err := db.TxContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ func TestIssue_DeleteIssue(t *testing.T) {
|
|||||||
ID: issueIDs[2],
|
ID: issueIDs[2],
|
||||||
}
|
}
|
||||||
|
|
||||||
err = deleteIssue(issue)
|
err = deleteIssue(db.DefaultContext, issue)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
issueIDs, err = issues_model.GetIssueIDsByRepoID(db.DefaultContext, 1)
|
issueIDs, err = issues_model.GetIssueIDsByRepoID(db.DefaultContext, 1)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
@ -55,7 +55,7 @@ func TestIssue_DeleteIssue(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
issue, err = issues_model.GetIssueByID(db.DefaultContext, 4)
|
issue, err = issues_model.GetIssueByID(db.DefaultContext, 4)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
err = deleteIssue(issue)
|
err = deleteIssue(db.DefaultContext, issue)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, 2, len(attachments))
|
assert.EqualValues(t, 2, len(attachments))
|
||||||
for i := range attachments {
|
for i := range attachments {
|
||||||
@ -78,7 +78,7 @@ func TestIssue_DeleteIssue(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.False(t, left)
|
assert.False(t, left)
|
||||||
|
|
||||||
err = deleteIssue(issue2)
|
err = deleteIssue(db.DefaultContext, issue2)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
left, err = issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1)
|
left, err = issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
@ -90,7 +90,7 @@ func CreatePushPullComment(ctx context.Context, pusher *user_model.User, pr *iss
|
|||||||
|
|
||||||
ops.Content = string(dataJSON)
|
ops.Content = string(dataJSON)
|
||||||
|
|
||||||
comment, err = issue_service.CreateComment(ops)
|
comment, err = issue_service.CreateComment(ctx, ops)
|
||||||
|
|
||||||
return comment, err
|
return comment, err
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, pull *issu
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, assigneeID := range assigneeIDs {
|
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
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -122,7 +122,7 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, pull *issu
|
|||||||
Content: string(dataJSON),
|
Content: string(dataJSON),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _ = issue_service.CreateComment(ops)
|
_, _ = issue_service.CreateComment(ctx, ops)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -221,7 +221,7 @@ func ChangeTargetBranch(ctx context.Context, pr *issues_model.PullRequest, doer
|
|||||||
OldRef: oldBranch,
|
OldRef: oldBranch,
|
||||||
NewRef: targetBranch,
|
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)
|
return fmt.Errorf("CreateChangeTargetBranchComment: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,7 +248,7 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return issue_service.CreateComment(&issues_model.CreateCommentOptions{
|
return issue_service.CreateComment(ctx, &issues_model.CreateCommentOptions{
|
||||||
Type: issues_model.CommentTypeCode,
|
Type: issues_model.CommentTypeCode,
|
||||||
Doer: doer,
|
Doer: doer,
|
||||||
Repo: repo,
|
Repo: repo,
|
||||||
@ -368,7 +368,7 @@ func DismissReview(ctx context.Context, reviewID, repoID int64, message string,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
comment, err = issue_service.CreateComment(&issues_model.CreateCommentOptions{
|
comment, err = issue_service.CreateComment(ctx, &issues_model.CreateCommentOptions{
|
||||||
Doer: doer,
|
Doer: doer,
|
||||||
Content: message,
|
Content: message,
|
||||||
Type: issues_model.CommentTypeDismissReview,
|
Type: issues_model.CommentTypeDismissReview,
|
||||||
|
@ -67,7 +67,7 @@ func TestAPIMergePullWIP(t *testing.T) {
|
|||||||
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
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 := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{Status: issues_model.PullRequestStatusMergeable}, unittest.Cond("has_merged = ?", false))
|
||||||
pr.LoadIssue(db.DefaultContext)
|
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
|
// force reload
|
||||||
pr.LoadAttributes(db.DefaultContext)
|
pr.LoadAttributes(db.DefaultContext)
|
||||||
|
Loading…
Reference in New Issue
Block a user