Skip to content

Commit 5e8e3ec

Browse files
lunnydelvh
andauthored
Fix issues count bug (#21557)
fix #19349 , #19505 Co-authored-by: delvh <[email protected]>
1 parent 29c00eb commit 5e8e3ec

File tree

4 files changed

+27
-45
lines changed

4 files changed

+27
-45
lines changed

models/issues/comment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
886886
}
887887
}
888888
case CommentTypeReopen, CommentTypeClose:
889-
if err = updateIssueClosedNum(ctx, opts.Issue); err != nil {
889+
if err = repo_model.UpdateRepoIssueNumbers(ctx, opts.Issue.RepoID, opts.Issue.IsPull, true); err != nil {
890890
return err
891891
}
892892
}

models/issues/issue.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,8 @@ func doChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.Use
725725
}
726726
}
727727

728-
if err := updateIssueClosedNum(ctx, issue); err != nil {
728+
// update repository's issue closed number
729+
if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, true); err != nil {
729730
return nil, err
730731
}
731732

@@ -2137,15 +2138,6 @@ func (issue *Issue) BlockingDependencies(ctx context.Context) (issueDeps []*Depe
21372138
return issueDeps, err
21382139
}
21392140

2140-
func updateIssueClosedNum(ctx context.Context, issue *Issue) (err error) {
2141-
if issue.IsPull {
2142-
err = repo_model.StatsCorrectNumClosed(ctx, issue.RepoID, true, "num_closed_pulls")
2143-
} else {
2144-
err = repo_model.StatsCorrectNumClosed(ctx, issue.RepoID, false, "num_closed_issues")
2145-
}
2146-
return err
2147-
}
2148-
21492141
// FindAndUpdateIssueMentions finds users mentioned in the given content string, and saves them in the database.
21502142
func FindAndUpdateIssueMentions(ctx context.Context, issue *Issue, doer *user_model.User, content string) (mentions []*user_model.User, err error) {
21512143
rawMentions := references.FindAllMentionsMarkdown(content)

models/repo.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,24 +404,19 @@ func repoStatsCorrectIssueNumComments(ctx context.Context, id int64) error {
404404
}
405405

406406
func repoStatsCorrectNumIssues(ctx context.Context, id int64) error {
407-
return repoStatsCorrectNum(ctx, id, false, "num_issues")
407+
return repo_model.UpdateRepoIssueNumbers(ctx, id, false, false)
408408
}
409409

410410
func repoStatsCorrectNumPulls(ctx context.Context, id int64) error {
411-
return repoStatsCorrectNum(ctx, id, true, "num_pulls")
412-
}
413-
414-
func repoStatsCorrectNum(ctx context.Context, id int64, isPull bool, field string) error {
415-
_, err := db.GetEngine(ctx).Exec("UPDATE `repository` SET "+field+"=(SELECT COUNT(*) FROM `issue` WHERE repo_id=? AND is_pull=?) WHERE id=?", id, isPull, id)
416-
return err
411+
return repo_model.UpdateRepoIssueNumbers(ctx, id, true, false)
417412
}
418413

419414
func repoStatsCorrectNumClosedIssues(ctx context.Context, id int64) error {
420-
return repo_model.StatsCorrectNumClosed(ctx, id, false, "num_closed_issues")
415+
return repo_model.UpdateRepoIssueNumbers(ctx, id, false, true)
421416
}
422417

423418
func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error {
424-
return repo_model.StatsCorrectNumClosed(ctx, id, true, "num_closed_pulls")
419+
return repo_model.UpdateRepoIssueNumbers(ctx, id, true, true)
425420
}
426421

427422
func statsQuery(args ...interface{}) func(context.Context) ([]map[string][]byte, error) {

models/repo/repo.go

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -765,35 +765,30 @@ func CountRepositories(ctx context.Context, opts CountRepositoryOptions) (int64,
765765
return count, nil
766766
}
767767

768-
// StatsCorrectNumClosed update repository's issue related numbers
769-
func StatsCorrectNumClosed(ctx context.Context, id int64, isPull bool, field string) error {
770-
_, 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)
771-
return err
772-
}
773-
774-
// UpdateRepoIssueNumbers update repository issue numbers
768+
// UpdateRepoIssueNumbers updates one of a repositories amount of (open|closed) (issues|PRs) with the current count
775769
func UpdateRepoIssueNumbers(ctx context.Context, repoID int64, isPull, isClosed bool) error {
776-
e := db.GetEngine(ctx)
770+
field := "num_"
771+
if isClosed {
772+
field += "closed_"
773+
}
777774
if isPull {
778-
if _, err := e.ID(repoID).Decr("num_pulls").Update(new(Repository)); err != nil {
779-
return err
780-
}
781-
if isClosed {
782-
if _, err := e.ID(repoID).Decr("num_closed_pulls").Update(new(Repository)); err != nil {
783-
return err
784-
}
785-
}
775+
field += "pulls"
786776
} else {
787-
if _, err := e.ID(repoID).Decr("num_issues").Update(new(Repository)); err != nil {
788-
return err
789-
}
790-
if isClosed {
791-
if _, err := e.ID(repoID).Decr("num_closed_issues").Update(new(Repository)); err != nil {
792-
return err
793-
}
794-
}
777+
field += "issues"
795778
}
796-
return nil
779+
780+
subQuery := builder.Select("count(*)").
781+
From("issue").Where(builder.Eq{
782+
"repo_id": repoID,
783+
"is_pull": isPull,
784+
}.And(builder.If(isClosed, builder.Eq{"is_closed": isClosed})))
785+
786+
// builder.Update(cond) will generate SQL like UPDATE ... SET cond
787+
query := builder.Update(builder.Eq{field: subQuery}).
788+
From("repository").
789+
Where(builder.Eq{"id": repoID})
790+
_, err := db.Exec(ctx, query)
791+
return err
797792
}
798793

799794
// CountNullArchivedRepository counts the number of repositories with is_archived is null

0 commit comments

Comments
 (0)