Skip to content

Commit 5667ef9

Browse files
authored
Add missing database transaction for new issue (#29490) (#29607)
When creating an issue, inserting issue, assign users and set project should be in the same transaction. Backport #29490
1 parent 02df269 commit 5667ef9

File tree

7 files changed

+31
-26
lines changed

7 files changed

+31
-26
lines changed

models/issues/issue_project.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ func LoadIssuesFromBoardList(ctx context.Context, bs project_model.BoardList) (m
100100
}
101101

102102
// ChangeProjectAssign changes the project associated with an issue
103-
func ChangeProjectAssign(issue *Issue, doer *user_model.User, newProjectID int64) error {
104-
ctx, committer, err := db.TxContext(db.DefaultContext)
103+
func ChangeProjectAssign(ctx context.Context, issue *Issue, doer *user_model.User, newProjectID int64) error {
104+
ctx, committer, err := db.TxContext(ctx)
105105
if err != nil {
106106
return err
107107
}

routers/api/v1/repo/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ func CreateIssue(ctx *context.APIContext) {
707707
form.Labels = make([]int64, 0)
708708
}
709709

710-
if err := issue_service.NewIssue(ctx, ctx.Repo.Repository, issue, form.Labels, nil, assigneeIDs); err != nil {
710+
if err := issue_service.NewIssue(ctx, ctx.Repo.Repository, issue, form.Labels, nil, assigneeIDs, 0); err != nil {
711711
if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) {
712712
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err)
713713
return

routers/web/org/projects.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ func UpdateIssueProject(ctx *context.Context) {
468468
}
469469
}
470470

471-
if err := issues_model.ChangeProjectAssign(issue, ctx.Doer, projectID); err != nil {
471+
if err := issues_model.ChangeProjectAssign(ctx, issue, ctx.Doer, projectID); err != nil {
472472
ctx.ServerError("ChangeProjectAssign", err)
473473
return
474474
}

routers/web/repo/issue.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,14 @@ func NewIssuePost(ctx *context.Context) {
11821182
return
11831183
}
11841184

1185+
if projectID > 0 {
1186+
if !ctx.Repo.CanRead(unit.TypeProjects) {
1187+
// User must also be able to see the project.
1188+
ctx.Error(http.StatusBadRequest, "user hasn't permissions to read projects")
1189+
return
1190+
}
1191+
}
1192+
11851193
if setting.Attachment.Enabled {
11861194
attachments = form.Files
11871195
}
@@ -1214,7 +1222,7 @@ func NewIssuePost(ctx *context.Context) {
12141222
Ref: form.Ref,
12151223
}
12161224

1217-
if err := issue_service.NewIssue(ctx, repo, issue, labelIDs, attachments, assigneeIDs); err != nil {
1225+
if err := issue_service.NewIssue(ctx, repo, issue, labelIDs, attachments, assigneeIDs, projectID); err != nil {
12181226
if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) {
12191227
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error())
12201228
return
@@ -1223,18 +1231,6 @@ func NewIssuePost(ctx *context.Context) {
12231231
return
12241232
}
12251233

1226-
if projectID > 0 {
1227-
if !ctx.Repo.CanRead(unit.TypeProjects) {
1228-
// User must also be able to see the project.
1229-
ctx.Error(http.StatusBadRequest, "user hasn't permissions to read projects")
1230-
return
1231-
}
1232-
if err := issues_model.ChangeProjectAssign(issue, ctx.Doer, projectID); err != nil {
1233-
ctx.ServerError("ChangeProjectAssign", err)
1234-
return
1235-
}
1236-
}
1237-
12381234
log.Trace("Issue created: %d/%d", repo.ID, issue.ID)
12391235
if ctx.FormString("redirect_after_creation") == "project" && projectID > 0 {
12401236
ctx.JSONRedirect(ctx.Repo.RepoLink + "/projects/" + strconv.FormatInt(projectID, 10))

routers/web/repo/projects.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func UpdateIssueProject(ctx *context.Context) {
396396
}
397397
}
398398

399-
if err := issues_model.ChangeProjectAssign(issue, ctx.Doer, projectID); err != nil {
399+
if err := issues_model.ChangeProjectAssign(ctx, issue, ctx.Doer, projectID); err != nil {
400400
ctx.ServerError("ChangeProjectAssign", err)
401401
return
402402
}

routers/web/repo/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@ func CompareAndPullRequestPost(ctx *context.Context) {
14811481
ctx.Error(http.StatusBadRequest, "user hasn't the permission to write to projects")
14821482
return
14831483
}
1484-
if err := issues_model.ChangeProjectAssign(pullIssue, ctx.Doer, projectID); err != nil {
1484+
if err := issues_model.ChangeProjectAssign(ctx, pullIssue, ctx.Doer, projectID); err != nil {
14851485
ctx.ServerError("ChangeProjectAssign", err)
14861486
return
14871487
}

services/issue/issue.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,24 @@ import (
2121
)
2222

2323
// NewIssue creates new issue with labels for repository.
24-
func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *issues_model.Issue, labelIDs []int64, uuids []string, assigneeIDs []int64) error {
25-
if err := issues_model.NewIssue(ctx, repo, issue, labelIDs, uuids); err != nil {
26-
return err
27-
}
28-
29-
for _, assigneeID := range assigneeIDs {
30-
if _, err := AddAssigneeIfNotAssigned(ctx, issue, issue.Poster, assigneeID, true); err != nil {
24+
func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *issues_model.Issue, labelIDs []int64, uuids []string, assigneeIDs []int64, projectID int64) error {
25+
if err := db.WithTx(ctx, func(ctx context.Context) error {
26+
if err := issues_model.NewIssue(ctx, repo, issue, labelIDs, uuids); err != nil {
3127
return err
3228
}
29+
for _, assigneeID := range assigneeIDs {
30+
if _, err := AddAssigneeIfNotAssigned(ctx, issue, issue.Poster, assigneeID, true); err != nil {
31+
return err
32+
}
33+
}
34+
if projectID > 0 {
35+
if err := issues_model.ChangeProjectAssign(ctx, issue, issue.Poster, projectID); err != nil {
36+
return err
37+
}
38+
}
39+
return nil
40+
}); err != nil {
41+
return err
3342
}
3443

3544
mentions, err := issues_model.FindAndUpdateIssueMentions(ctx, issue, issue.Poster, issue.Content)

0 commit comments

Comments
 (0)