Skip to content

Commit e917334

Browse files
authored
Add missing database transaction for new issue (#29490)
When creating an issue, inserting issue, assign users and set project should be in the same transaction.
1 parent 7ec4c65 commit e917334

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

routers/api/v1/repo/issue.go

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

712-
if err := issue_service.NewIssue(ctx, ctx.Repo.Repository, issue, form.Labels, nil, assigneeIDs); err != nil {
712+
if err := issue_service.NewIssue(ctx, ctx.Repo.Repository, issue, form.Labels, nil, assigneeIDs, 0); err != nil {
713713
if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) {
714714
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err)
715715
} else if errors.Is(err, user_model.ErrBlockedUser) {

routers/web/repo/issue.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,14 @@ func NewIssuePost(ctx *context.Context) {
12241224
return
12251225
}
12261226

1227+
if projectID > 0 {
1228+
if !ctx.Repo.CanRead(unit.TypeProjects) {
1229+
// User must also be able to see the project.
1230+
ctx.Error(http.StatusBadRequest, "user hasn't permissions to read projects")
1231+
return
1232+
}
1233+
}
1234+
12271235
if setting.Attachment.Enabled {
12281236
attachments = form.Files
12291237
}
@@ -1256,7 +1264,7 @@ func NewIssuePost(ctx *context.Context) {
12561264
Ref: form.Ref,
12571265
}
12581266

1259-
if err := issue_service.NewIssue(ctx, repo, issue, labelIDs, attachments, assigneeIDs); err != nil {
1267+
if err := issue_service.NewIssue(ctx, repo, issue, labelIDs, attachments, assigneeIDs, projectID); err != nil {
12601268
if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) {
12611269
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error())
12621270
} else if errors.Is(err, user_model.ErrBlockedUser) {
@@ -1267,18 +1275,6 @@ func NewIssuePost(ctx *context.Context) {
12671275
return
12681276
}
12691277

1270-
if projectID > 0 {
1271-
if !ctx.Repo.CanRead(unit.TypeProjects) {
1272-
// User must also be able to see the project.
1273-
ctx.Error(http.StatusBadRequest, "user hasn't permissions to read projects")
1274-
return
1275-
}
1276-
if err := issues_model.ChangeProjectAssign(ctx, issue, ctx.Doer, projectID); err != nil {
1277-
ctx.ServerError("ChangeProjectAssign", err)
1278-
return
1279-
}
1280-
}
1281-
12821278
log.Trace("Issue created: %d/%d", repo.ID, issue.ID)
12831279
if ctx.FormString("redirect_after_creation") == "project" && projectID > 0 {
12841280
ctx.JSONRedirect(ctx.Repo.RepoLink + "/projects/" + strconv.FormatInt(projectID, 10))

services/issue/issue.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
)
2323

2424
// NewIssue creates new issue with labels for repository.
25-
func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *issues_model.Issue, labelIDs []int64, uuids []string, assigneeIDs []int64) error {
25+
func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *issues_model.Issue, labelIDs []int64, uuids []string, assigneeIDs []int64, projectID int64) error {
2626
if err := issue.LoadPoster(ctx); err != nil {
2727
return err
2828
}
@@ -31,14 +31,23 @@ func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *issues_mo
3131
return user_model.ErrBlockedUser
3232
}
3333

34-
if err := issues_model.NewIssue(ctx, repo, issue, labelIDs, uuids); err != nil {
35-
return err
36-
}
37-
38-
for _, assigneeID := range assigneeIDs {
39-
if _, err := AddAssigneeIfNotAssigned(ctx, issue, issue.Poster, assigneeID, true); err != nil {
34+
if err := db.WithTx(ctx, func(ctx context.Context) error {
35+
if err := issues_model.NewIssue(ctx, repo, issue, labelIDs, uuids); err != nil {
4036
return err
4137
}
38+
for _, assigneeID := range assigneeIDs {
39+
if _, err := AddAssigneeIfNotAssigned(ctx, issue, issue.Poster, assigneeID, true); err != nil {
40+
return err
41+
}
42+
}
43+
if projectID > 0 {
44+
if err := issues_model.ChangeProjectAssign(ctx, issue, issue.Poster, projectID); err != nil {
45+
return err
46+
}
47+
}
48+
return nil
49+
}); err != nil {
50+
return err
4251
}
4352

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

0 commit comments

Comments
 (0)