Fix issues count bug (#21557)

fix #19349 , #19505

Co-authored-by: delvh <dev.lh@web.de>
This commit is contained in:
Lunny Xiao 2022-10-25 20:47:46 +08:00 committed by GitHub
parent 29c00eb1ed
commit 5e8e3ecbeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 47 deletions

View File

@ -886,7 +886,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
} }
} }
case CommentTypeReopen, CommentTypeClose: case CommentTypeReopen, CommentTypeClose:
if err = updateIssueClosedNum(ctx, opts.Issue); err != nil { if err = repo_model.UpdateRepoIssueNumbers(ctx, opts.Issue.RepoID, opts.Issue.IsPull, true); err != nil {
return err return err
} }
} }

View File

@ -725,7 +725,8 @@ func doChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.Use
} }
} }
if err := updateIssueClosedNum(ctx, issue); err != nil { // update repository's issue closed number
if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, true); err != nil {
return nil, err return nil, err
} }
@ -2137,15 +2138,6 @@ func (issue *Issue) BlockingDependencies(ctx context.Context) (issueDeps []*Depe
return issueDeps, err return issueDeps, err
} }
func updateIssueClosedNum(ctx context.Context, issue *Issue) (err error) {
if issue.IsPull {
err = repo_model.StatsCorrectNumClosed(ctx, issue.RepoID, true, "num_closed_pulls")
} else {
err = repo_model.StatsCorrectNumClosed(ctx, issue.RepoID, false, "num_closed_issues")
}
return err
}
// FindAndUpdateIssueMentions finds users mentioned in the given content string, and saves them in the database. // FindAndUpdateIssueMentions finds users mentioned in the given content string, and saves them in the database.
func FindAndUpdateIssueMentions(ctx context.Context, issue *Issue, doer *user_model.User, content string) (mentions []*user_model.User, err error) { func FindAndUpdateIssueMentions(ctx context.Context, issue *Issue, doer *user_model.User, content string) (mentions []*user_model.User, err error) {
rawMentions := references.FindAllMentionsMarkdown(content) rawMentions := references.FindAllMentionsMarkdown(content)

View File

@ -404,24 +404,19 @@ func repoStatsCorrectIssueNumComments(ctx context.Context, id int64) error {
} }
func repoStatsCorrectNumIssues(ctx context.Context, id int64) error { func repoStatsCorrectNumIssues(ctx context.Context, id int64) error {
return repoStatsCorrectNum(ctx, id, false, "num_issues") return repo_model.UpdateRepoIssueNumbers(ctx, id, false, false)
} }
func repoStatsCorrectNumPulls(ctx context.Context, id int64) error { func repoStatsCorrectNumPulls(ctx context.Context, id int64) error {
return repoStatsCorrectNum(ctx, id, true, "num_pulls") return repo_model.UpdateRepoIssueNumbers(ctx, id, true, false)
}
func repoStatsCorrectNum(ctx context.Context, id int64, isPull bool, field string) error {
_, err := db.GetEngine(ctx).Exec("UPDATE `repository` SET "+field+"=(SELECT COUNT(*) FROM `issue` WHERE repo_id=? AND is_pull=?) WHERE id=?", id, isPull, id)
return err
} }
func repoStatsCorrectNumClosedIssues(ctx context.Context, id int64) error { func repoStatsCorrectNumClosedIssues(ctx context.Context, id int64) error {
return repo_model.StatsCorrectNumClosed(ctx, id, false, "num_closed_issues") return repo_model.UpdateRepoIssueNumbers(ctx, id, false, true)
} }
func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error { func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error {
return repo_model.StatsCorrectNumClosed(ctx, id, true, "num_closed_pulls") return repo_model.UpdateRepoIssueNumbers(ctx, id, true, true)
} }
func statsQuery(args ...interface{}) func(context.Context) ([]map[string][]byte, error) { func statsQuery(args ...interface{}) func(context.Context) ([]map[string][]byte, error) {

View File

@ -765,36 +765,31 @@ func CountRepositories(ctx context.Context, opts CountRepositoryOptions) (int64,
return count, nil return count, nil
} }
// StatsCorrectNumClosed update repository's issue related numbers // UpdateRepoIssueNumbers updates one of a repositories amount of (open|closed) (issues|PRs) with the current count
func StatsCorrectNumClosed(ctx context.Context, id int64, isPull bool, field string) error { func UpdateRepoIssueNumbers(ctx context.Context, repoID int64, isPull, isClosed bool) error {
_, err := db.Exec(ctx, "UPDATE `repository` SET "+field+"=(SELECT COUNT(*) FROM `issue` WHERE repo_id=? AND is_closed=? AND is_pull=?) WHERE id=?", id, true, isPull, id) field := "num_"
return err if isClosed {
field += "closed_"
}
if isPull {
field += "pulls"
} else {
field += "issues"
} }
// UpdateRepoIssueNumbers update repository issue numbers subQuery := builder.Select("count(*)").
func UpdateRepoIssueNumbers(ctx context.Context, repoID int64, isPull, isClosed bool) error { From("issue").Where(builder.Eq{
e := db.GetEngine(ctx) "repo_id": repoID,
if isPull { "is_pull": isPull,
if _, err := e.ID(repoID).Decr("num_pulls").Update(new(Repository)); err != nil { }.And(builder.If(isClosed, builder.Eq{"is_closed": isClosed})))
// builder.Update(cond) will generate SQL like UPDATE ... SET cond
query := builder.Update(builder.Eq{field: subQuery}).
From("repository").
Where(builder.Eq{"id": repoID})
_, err := db.Exec(ctx, query)
return err return err
} }
if isClosed {
if _, err := e.ID(repoID).Decr("num_closed_pulls").Update(new(Repository)); err != nil {
return err
}
}
} else {
if _, err := e.ID(repoID).Decr("num_issues").Update(new(Repository)); err != nil {
return err
}
if isClosed {
if _, err := e.ID(repoID).Decr("num_closed_issues").Update(new(Repository)); err != nil {
return err
}
}
}
return nil
}
// CountNullArchivedRepository counts the number of repositories with is_archived is null // CountNullArchivedRepository counts the number of repositories with is_archived is null
func CountNullArchivedRepository() (int64, error) { func CountNullArchivedRepository() (int64, error) {