Skip to content

Commit c385dcc

Browse files
authored
Fix index produces problem when issues/pulls deleted (#6973)
* fix index produces problem when issues/pulls deleted * fix tests * fix tests * fix tests
1 parent 96b412b commit c385dcc

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

models/issue.go

+28-1
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"
@@ -1015,9 +1016,35 @@ type NewIssueOptions struct {
10151016
IsPull bool
10161017
}
10171018

1019+
// GetMaxIndexOfIssue returns the max index on issue
1020+
func GetMaxIndexOfIssue(repoID int64) (int64, error) {
1021+
return getMaxIndexOfIssue(x, repoID)
1022+
}
1023+
1024+
func getMaxIndexOfIssue(e Engine, repoID int64) (int64, error) {
1025+
var (
1026+
maxIndex int64
1027+
has bool
1028+
err error
1029+
)
1030+
1031+
has, err = e.SQL("SELECT COALESCE((SELECT MAX(`index`) FROM issue WHERE repo_id = ?),0)", repoID).Get(&maxIndex)
1032+
if err != nil {
1033+
return 0, err
1034+
} else if !has {
1035+
return 0, errors.New("Retrieve Max index from issue failed")
1036+
}
1037+
return maxIndex, nil
1038+
}
1039+
10181040
func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
10191041
opts.Issue.Title = strings.TrimSpace(opts.Issue.Title)
1020-
opts.Issue.Index = opts.Repo.NextIssueIndex()
1042+
1043+
maxIndex, err := getMaxIndexOfIssue(e, opts.Issue.RepoID)
1044+
if err != nil {
1045+
return err
1046+
}
1047+
opts.Issue.Index = maxIndex + 1
10211048

10221049
if opts.Issue.MilestoneID > 0 {
10231050
milestone, err := getMilestoneByRepoID(e, opts.Issue.RepoID, opts.Issue.MilestoneID)

models/repo.go

-7
Original file line numberDiff line numberDiff line change
@@ -687,13 +687,6 @@ func (repo *Repository) getUsersWithAccessMode(e Engine, mode AccessMode) (_ []*
687687
return users, nil
688688
}
689689

690-
// NextIssueIndex returns the next issue index
691-
// FIXME: should have a mutex to prevent producing same index for two issues that are created
692-
// closely enough.
693-
func (repo *Repository) NextIssueIndex() int64 {
694-
return int64(repo.NumIssues+repo.NumPulls) + 1
695-
}
696-
697690
var (
698691
descPattern = regexp.MustCompile(`https?://\S+`)
699692
)

routers/api/v1/repo/pull.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,15 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
251251
deadlineUnix = util.TimeStamp(form.Deadline.Unix())
252252
}
253253

254+
maxIndex, err := models.GetMaxIndexOfIssue(repo.ID)
255+
if err != nil {
256+
ctx.ServerError("GetPatch", err)
257+
return
258+
}
259+
254260
prIssue := &models.Issue{
255261
RepoID: repo.ID,
256-
Index: repo.NextIssueIndex(),
262+
Index: maxIndex + 1,
257263
Title: form.Title,
258264
PosterID: ctx.User.ID,
259265
Poster: ctx.User,

routers/repo/pull.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -946,9 +946,15 @@ func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm)
946946
return
947947
}
948948

949+
maxIndex, err := models.GetMaxIndexOfIssue(repo.ID)
950+
if err != nil {
951+
ctx.ServerError("GetPatch", err)
952+
return
953+
}
954+
949955
pullIssue := &models.Issue{
950956
RepoID: repo.ID,
951-
Index: repo.NextIssueIndex(),
957+
Index: maxIndex + 1,
952958
Title: form.Title,
953959
PosterID: ctx.User.ID,
954960
Poster: ctx.User,

0 commit comments

Comments
 (0)