mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Fix index produces problem when issues/pulls deleted (#6973)
* fix index produces problem when issues/pulls deleted * fix tests * fix tests * fix tests
This commit is contained in:
		@@ -5,6 +5,7 @@
 | 
				
			|||||||
package models
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"path"
 | 
						"path"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
@@ -1015,9 +1016,35 @@ type NewIssueOptions struct {
 | 
				
			|||||||
	IsPull      bool
 | 
						IsPull      bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// GetMaxIndexOfIssue returns the max index on issue
 | 
				
			||||||
 | 
					func GetMaxIndexOfIssue(repoID int64) (int64, error) {
 | 
				
			||||||
 | 
						return getMaxIndexOfIssue(x, repoID)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func getMaxIndexOfIssue(e Engine, repoID int64) (int64, error) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							maxIndex int64
 | 
				
			||||||
 | 
							has      bool
 | 
				
			||||||
 | 
							err      error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						has, err = e.SQL("SELECT COALESCE((SELECT MAX(`index`) FROM issue WHERE repo_id = ?),0)", repoID).Get(&maxIndex)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return 0, err
 | 
				
			||||||
 | 
						} else if !has {
 | 
				
			||||||
 | 
							return 0, errors.New("Retrieve Max index from issue failed")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return maxIndex, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
 | 
					func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
 | 
				
			||||||
	opts.Issue.Title = strings.TrimSpace(opts.Issue.Title)
 | 
						opts.Issue.Title = strings.TrimSpace(opts.Issue.Title)
 | 
				
			||||||
	opts.Issue.Index = opts.Repo.NextIssueIndex()
 | 
					
 | 
				
			||||||
 | 
						maxIndex, err := getMaxIndexOfIssue(e, opts.Issue.RepoID)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						opts.Issue.Index = maxIndex + 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if opts.Issue.MilestoneID > 0 {
 | 
						if opts.Issue.MilestoneID > 0 {
 | 
				
			||||||
		milestone, err := getMilestoneByRepoID(e, opts.Issue.RepoID, opts.Issue.MilestoneID)
 | 
							milestone, err := getMilestoneByRepoID(e, opts.Issue.RepoID, opts.Issue.MilestoneID)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -687,13 +687,6 @@ func (repo *Repository) getUsersWithAccessMode(e Engine, mode AccessMode) (_ []*
 | 
				
			|||||||
	return users, nil
 | 
						return users, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NextIssueIndex returns the next issue index
 | 
					 | 
				
			||||||
// FIXME: should have a mutex to prevent producing same index for two issues that are created
 | 
					 | 
				
			||||||
// closely enough.
 | 
					 | 
				
			||||||
func (repo *Repository) NextIssueIndex() int64 {
 | 
					 | 
				
			||||||
	return int64(repo.NumIssues+repo.NumPulls) + 1
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
	descPattern = regexp.MustCompile(`https?://\S+`)
 | 
						descPattern = regexp.MustCompile(`https?://\S+`)
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -251,9 +251,15 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
 | 
				
			|||||||
		deadlineUnix = util.TimeStamp(form.Deadline.Unix())
 | 
							deadlineUnix = util.TimeStamp(form.Deadline.Unix())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						maxIndex, err := models.GetMaxIndexOfIssue(repo.ID)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							ctx.ServerError("GetPatch", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	prIssue := &models.Issue{
 | 
						prIssue := &models.Issue{
 | 
				
			||||||
		RepoID:       repo.ID,
 | 
							RepoID:       repo.ID,
 | 
				
			||||||
		Index:        repo.NextIssueIndex(),
 | 
							Index:        maxIndex + 1,
 | 
				
			||||||
		Title:        form.Title,
 | 
							Title:        form.Title,
 | 
				
			||||||
		PosterID:     ctx.User.ID,
 | 
							PosterID:     ctx.User.ID,
 | 
				
			||||||
		Poster:       ctx.User,
 | 
							Poster:       ctx.User,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -946,9 +946,15 @@ func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm)
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						maxIndex, err := models.GetMaxIndexOfIssue(repo.ID)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							ctx.ServerError("GetPatch", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pullIssue := &models.Issue{
 | 
						pullIssue := &models.Issue{
 | 
				
			||||||
		RepoID:      repo.ID,
 | 
							RepoID:      repo.ID,
 | 
				
			||||||
		Index:       repo.NextIssueIndex(),
 | 
							Index:       maxIndex + 1,
 | 
				
			||||||
		Title:       form.Title,
 | 
							Title:       form.Title,
 | 
				
			||||||
		PosterID:    ctx.User.ID,
 | 
							PosterID:    ctx.User.ID,
 | 
				
			||||||
		Poster:      ctx.User,
 | 
							Poster:      ctx.User,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user