Skip to content

Commit 74874d0

Browse files
authored
Fix issue/PR numbers (#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 77f5035 commit 74874d0

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
@@ -997,12 +997,7 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
997997
}
998998
}
999999

1000-
if opts.IsPull {
1001-
_, err = e.Exec("UPDATE `repository` SET num_pulls = num_pulls + 1 WHERE id = ?", opts.Issue.RepoID)
1002-
} else {
1003-
_, err = e.Exec("UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?", opts.Issue.RepoID)
1004-
}
1005-
if err != nil {
1000+
if err := repo_model.UpdateRepoIssueNumbers(ctx, opts.Issue.RepoID, opts.IsPull, false); err != nil {
10061001
return err
10071002
}
10081003

models/repo.go

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

services/issue/issue.go

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

221-
if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, issue.IsClosed); err != nil {
221+
// update the total issue numbers
222+
if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, false); err != nil {
222223
return err
223224
}
225+
// if the issue is closed, update the closed issue numbers
226+
if issue.IsClosed {
227+
if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, true); err != nil {
228+
return err
229+
}
230+
}
224231

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

0 commit comments

Comments
 (0)