Skip to content

Commit 291787a

Browse files
authored
Fix issues count bug (#21600)
backport #21557
1 parent e504410 commit 291787a

File tree

4 files changed

+30
-52
lines changed

4 files changed

+30
-52
lines changed

models/issues/comment.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
881881
}
882882
}
883883
case CommentTypeReopen, CommentTypeClose:
884-
if err = updateIssueClosedNum(ctx, opts.Issue); err != nil {
884+
if err = repo_model.UpdateRepoIssueNumbers(ctx, opts.Issue.RepoID, opts.Issue.IsPull, true); err != nil {
885885
return err
886886
}
887887
}

models/issues/issue.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,8 @@ func doChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.Use
722722
}
723723
}
724724

725-
if err := updateIssueClosedNum(ctx, issue); err != nil {
725+
// update repository's issue closed number
726+
if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, true); err != nil {
726727
return nil, err
727728
}
728729

@@ -2104,15 +2105,6 @@ func (issue *Issue) BlockingDependencies(ctx context.Context) (issueDeps []*Depe
21042105
return issueDeps, err
21052106
}
21062107

2107-
func updateIssueClosedNum(ctx context.Context, issue *Issue) (err error) {
2108-
if issue.IsPull {
2109-
err = repo_model.StatsCorrectNumClosed(ctx, issue.RepoID, true, "num_closed_pulls")
2110-
} else {
2111-
err = repo_model.StatsCorrectNumClosed(ctx, issue.RepoID, false, "num_closed_issues")
2112-
}
2113-
return
2114-
}
2115-
21162108
// FindAndUpdateIssueMentions finds users mentioned in the given content string, and saves them in the database.
21172109
func FindAndUpdateIssueMentions(ctx context.Context, issue *Issue, doer *user_model.User, content string) (mentions []*user_model.User, err error) {
21182110
rawMentions := references.FindAllMentionsMarkdown(content)

models/repo.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -562,24 +562,19 @@ func repoStatsCorrectIssueNumComments(ctx context.Context, id int64) error {
562562
}
563563

564564
func repoStatsCorrectNumIssues(ctx context.Context, id int64) error {
565-
return repoStatsCorrectNum(ctx, id, false, "num_issues")
565+
return repo_model.UpdateRepoIssueNumbers(ctx, id, false, false)
566566
}
567567

568568
func repoStatsCorrectNumPulls(ctx context.Context, id int64) error {
569-
return repoStatsCorrectNum(ctx, id, true, "num_pulls")
570-
}
571-
572-
func repoStatsCorrectNum(ctx context.Context, id int64, isPull bool, field string) error {
573-
_, err := db.GetEngine(ctx).Exec("UPDATE `repository` SET "+field+"=(SELECT COUNT(*) FROM `issue` WHERE repo_id=? AND is_pull=?) WHERE id=?", id, isPull, id)
574-
return err
569+
return repo_model.UpdateRepoIssueNumbers(ctx, id, true, false)
575570
}
576571

577572
func repoStatsCorrectNumClosedIssues(ctx context.Context, id int64) error {
578-
return repo_model.StatsCorrectNumClosed(ctx, id, false, "num_closed_issues")
573+
return repo_model.UpdateRepoIssueNumbers(ctx, id, false, true)
579574
}
580575

581576
func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error {
582-
return repo_model.StatsCorrectNumClosed(ctx, id, true, "num_closed_pulls")
577+
return repo_model.UpdateRepoIssueNumbers(ctx, id, true, true)
583578
}
584579

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

models/repo/repo.go

+23-32
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
api "code.gitea.io/gitea/modules/structs"
2424
"code.gitea.io/gitea/modules/timeutil"
2525
"code.gitea.io/gitea/modules/util"
26+
27+
"xorm.io/builder"
2628
)
2729

2830
// ErrUserDoesNotHaveAccessToRepo represets an error where the user doesn't has access to a given repo.
@@ -319,13 +321,7 @@ func (repo *Repository) LoadUnits(ctx context.Context) (err error) {
319321

320322
// UnitEnabled if this repository has the given unit enabled
321323
func (repo *Repository) UnitEnabled(tp unit.Type) (result bool) {
322-
if err := db.WithContext(func(ctx *db.Context) error {
323-
result = repo.UnitEnabledCtx(ctx, tp)
324-
return nil
325-
}); err != nil {
326-
log.Error("repo.UnitEnabled: %v", err)
327-
}
328-
return
324+
return repo.UnitEnabledCtx(db.DefaultContext, tp)
329325
}
330326

331327
// UnitEnabled if this repository has the given unit enabled
@@ -760,33 +756,28 @@ func CountRepositories(ctx context.Context, opts CountRepositoryOptions) (int64,
760756
return count, nil
761757
}
762758

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

0 commit comments

Comments
 (0)