Skip to content

Commit dcb3ddb

Browse files
committed
Revert #7898
1 parent 188e164 commit dcb3ddb

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

models/issue.go

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package models
66

77
import (
8+
"errors"
89
"fmt"
910
"path"
1011
"regexp"
@@ -73,7 +74,6 @@ var (
7374

7475
const issueTasksRegexpStr = `(^\s*[-*]\s\[[\sx]\]\s.)|(\n\s*[-*]\s\[[\sx]\]\s.)`
7576
const issueTasksDoneRegexpStr = `(^\s*[-*]\s\[[x]\]\s.)|(\n\s*[-*]\s\[[x]\]\s.)`
76-
const issueMaxDupIndexAttempts = 3
7777

7878
func init() {
7979
issueTasksPat = regexp.MustCompile(issueTasksRegexpStr)
@@ -1053,9 +1053,36 @@ type NewIssueOptions struct {
10531053
IsPull bool
10541054
}
10551055

1056+
// GetMaxIndexOfIssue returns the max index on issue
1057+
func GetMaxIndexOfIssue(repoID int64) (int64, error) {
1058+
return getMaxIndexOfIssue(x, repoID)
1059+
}
1060+
1061+
func getMaxIndexOfIssue(e Engine, repoID int64) (int64, error) {
1062+
var (
1063+
maxIndex int64
1064+
has bool
1065+
err error
1066+
)
1067+
1068+
has, err = e.SQL("SELECT COALESCE((SELECT MAX(`index`) FROM issue WHERE repo_id = ?),0)", repoID).Get(&maxIndex)
1069+
if err != nil {
1070+
return 0, err
1071+
} else if !has {
1072+
return 0, errors.New("Retrieve Max index from issue failed")
1073+
}
1074+
return maxIndex, nil
1075+
}
1076+
10561077
func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
10571078
opts.Issue.Title = strings.TrimSpace(opts.Issue.Title)
10581079

1080+
maxIndex, err := getMaxIndexOfIssue(e, opts.Issue.RepoID)
1081+
if err != nil {
1082+
return err
1083+
}
1084+
opts.Issue.Index = maxIndex + 1
1085+
10591086
if opts.Issue.MilestoneID > 0 {
10601087
milestone, err := getMilestoneByRepoID(e, opts.Issue.RepoID, opts.Issue.MilestoneID)
10611088
if err != nil && !IsErrMilestoneNotExist(err) {
@@ -1104,31 +1131,10 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
11041131
}
11051132

11061133
// Milestone and assignee validation should happen before insert actual object.
1107-
1108-
// There's no good way to identify a duplicate key error in database/sql; brute force some retries
1109-
dupIndexAttempts := issueMaxDupIndexAttempts
1110-
for {
1111-
_, err := e.SetExpr("`index`", "coalesce(MAX(`index`),0)+1").
1112-
Where("repo_id=?", opts.Issue.RepoID).
1113-
Insert(opts.Issue)
1114-
if err == nil {
1115-
break
1116-
}
1117-
1118-
dupIndexAttempts--
1119-
if dupIndexAttempts <= 0 {
1120-
return err
1121-
}
1122-
}
1123-
1124-
inserted, err := getIssueByID(e, opts.Issue.ID)
1125-
if err != nil {
1134+
if _, err = e.Insert(opts.Issue); err != nil {
11261135
return err
11271136
}
11281137

1129-
// Patch Index with the value calculated by the database
1130-
opts.Issue.Index = inserted.Index
1131-
11321138
if opts.Issue.MilestoneID > 0 {
11331139
if err = changeMilestoneAssign(e, doer, opts.Issue, -1); err != nil {
11341140
return err

routers/api/v1/repo/pull.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,15 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
254254
deadlineUnix = timeutil.TimeStamp(form.Deadline.Unix())
255255
}
256256

257+
maxIndex, err := models.GetMaxIndexOfIssue(repo.ID)
258+
if err != nil {
259+
ctx.ServerError("GetPatch", err)
260+
return
261+
}
262+
257263
prIssue := &models.Issue{
258264
RepoID: repo.ID,
265+
Index: maxIndex + 1,
259266
Title: form.Title,
260267
PosterID: ctx.User.ID,
261268
Poster: ctx.User,

routers/repo/pull.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,8 +766,15 @@ func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm)
766766
return
767767
}
768768

769+
maxIndex, err := models.GetMaxIndexOfIssue(repo.ID)
770+
if err != nil {
771+
ctx.ServerError("GetPatch", err)
772+
return
773+
}
774+
769775
pullIssue := &models.Issue{
770776
RepoID: repo.ID,
777+
Index: maxIndex + 1,
771778
Title: form.Title,
772779
PosterID: ctx.User.ID,
773780
Poster: ctx.User,

0 commit comments

Comments
 (0)