Skip to content

Commit c0ca9c6

Browse files
wolfogrelunny
andauthored
Fix issue/PR numbers (#22037) (#22044)
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`. Co-authored-by: Lunny Xiao <[email protected]>
1 parent e39bb2d commit c0ca9c6

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
@@ -1010,12 +1010,7 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
10101010
}
10111011
}
10121012

1013-
if opts.IsPull {
1014-
_, err = e.Exec("UPDATE `repository` SET num_pulls = num_pulls + 1 WHERE id = ?", opts.Issue.RepoID)
1015-
} else {
1016-
_, err = e.Exec("UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?", opts.Issue.RepoID)
1017-
}
1018-
if err != nil {
1013+
if err := repo_model.UpdateRepoIssueNumbers(ctx, opts.Issue.RepoID, opts.IsPull, false); err != nil {
10191014
return err
10201015
}
10211016

models/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ func CheckRepoStats(ctx context.Context) error {
444444
},
445445
// Repository.NumIssues
446446
{
447-
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),
447+
statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_issues!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_pull=?)", false),
448448
repoStatsCorrectNumIssues,
449449
"repository count 'num_issues'",
450450
},
@@ -456,7 +456,7 @@ func CheckRepoStats(ctx context.Context) error {
456456
},
457457
// Repository.NumPulls
458458
{
459-
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),
459+
statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_pulls!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_pull=?)", true),
460460
repoStatsCorrectNumPulls,
461461
"repository count 'num_pulls'",
462462
},

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)