Skip to content

Commit e93a4a0

Browse files
authored
Fix issue/PR numbers (#22037) (#22045)
Backport #22037. When deleting a closed issue, we should update both `NumIssues`and `NumClosedIssues`, or `NumOpenIssues`(`= NumIssues -NumClosedIssues`) will be wrong. It's the same for pull requests. Releated to #21557. Alse fixed two harmless problems: - The SQL to check issue/PR total numbers is wrong, that means it will update the numbers even if they are correct. - Replace legacy `num_issues = num_issues + 1` operations with `UpdateRepoIssueNumbers`.
1 parent 601766d commit e93a4a0

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

models/issues/issue.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,12 +1007,7 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
10071007
}
10081008
}
10091009

1010-
if opts.IsPull {
1011-
_, err = e.Exec("UPDATE `repository` SET num_pulls = num_pulls + 1 WHERE id = ?", opts.Issue.RepoID)
1012-
} else {
1013-
_, err = e.Exec("UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?", opts.Issue.RepoID)
1014-
}
1015-
if err != nil {
1010+
if err := repo_model.UpdateRepoIssueNumbers(ctx, opts.Issue.RepoID, opts.IsPull, false); err != nil {
10161011
return err
10171012
}
10181013

models/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ func CheckRepoStats(ctx context.Context) error {
602602
},
603603
// Repository.NumIssues
604604
{
605-
statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_issues!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_closed=? AND is_pull=?)", false, false),
605+
statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_issues!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_pull=?)", false),
606606
repoStatsCorrectNumIssues,
607607
"repository count 'num_issues'",
608608
},
@@ -614,7 +614,7 @@ func CheckRepoStats(ctx context.Context) error {
614614
},
615615
// Repository.NumPulls
616616
{
617-
statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_pulls!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_closed=? AND is_pull=?)", false, true),
617+
statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_pulls!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_pull=?)", true),
618618
repoStatsCorrectNumPulls,
619619
"repository count 'num_pulls'",
620620
},

services/issue/issue.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,16 @@ func deleteIssue(issue *issues_model.Issue) error {
220220
return err
221221
}
222222

223-
if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, issue.IsClosed); err != nil {
223+
// update the total issue numbers
224+
if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, false); err != nil {
224225
return err
225226
}
227+
// if the issue is closed, update the closed issue numbers
228+
if issue.IsClosed {
229+
if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, true); err != nil {
230+
return err
231+
}
232+
}
226233

227234
if err := issues_model.UpdateMilestoneCounters(ctx, issue.MilestoneID); err != nil {
228235
return fmt.Errorf("error updating counters for milestone id %d: %w",

0 commit comments

Comments
 (0)