Skip to content

Commit e0d6d2f

Browse files
lunnyzeripath
authored andcommitted
Fix repository's pull request count error (#7518)
* fix pr count error * fix tests
1 parent f9d6e35 commit e0d6d2f

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

models/issue.go

+19
Original file line numberDiff line numberDiff line change
@@ -1849,3 +1849,22 @@ func (issue *Issue) BlockedByDependencies() ([]*Issue, error) {
18491849
func (issue *Issue) BlockingDependencies() ([]*Issue, error) {
18501850
return issue.getBlockingDependencies(x)
18511851
}
1852+
1853+
func (issue *Issue) updateClosedNum(e Engine) (err error) {
1854+
if issue.IsPull {
1855+
_, err = e.Exec("UPDATE `repository` SET num_closed_pulls=(SELECT count(*) FROM issue WHERE repo_id=? AND is_pull=? AND is_closed=?) WHERE id=?",
1856+
issue.RepoID,
1857+
true,
1858+
true,
1859+
issue.RepoID,
1860+
)
1861+
} else {
1862+
_, err = e.Exec("UPDATE `repository` SET num_closed_issues=(SELECT count(*) FROM issue WHERE repo_id=? AND is_pull=? AND is_closed=?) WHERE id=?",
1863+
issue.RepoID,
1864+
false,
1865+
true,
1866+
issue.RepoID,
1867+
)
1868+
}
1869+
return
1870+
}

models/issue_comment.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -634,12 +634,7 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen
634634
act.OpType = ActionReopenPullRequest
635635
}
636636

637-
if opts.Issue.IsPull {
638-
_, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls-1 WHERE id=?", opts.Repo.ID)
639-
} else {
640-
_, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues-1 WHERE id=?", opts.Repo.ID)
641-
}
642-
if err != nil {
637+
if err = opts.Issue.updateClosedNum(e); err != nil {
643638
return err
644639
}
645640

@@ -649,12 +644,7 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen
649644
act.OpType = ActionClosePullRequest
650645
}
651646

652-
if opts.Issue.IsPull {
653-
_, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls+1 WHERE id=?", opts.Repo.ID)
654-
} else {
655-
_, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues+1 WHERE id=?", opts.Repo.ID)
656-
}
657-
if err != nil {
647+
if err = opts.Issue.updateClosedNum(e); err != nil {
658648
return err
659649
}
660650
}

models/repo.go

+17
Original file line numberDiff line numberDiff line change
@@ -2311,6 +2311,23 @@ func CheckRepoStats() {
23112311
}
23122312
// ***** END: Repository.NumClosedIssues *****
23132313

2314+
// ***** START: Repository.NumClosedPulls *****
2315+
desc = "repository count 'num_closed_pulls'"
2316+
results, err = x.Query("SELECT repo.id FROM `repository` repo WHERE repo.num_closed_pulls!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_closed=? AND is_pull=?)", true, true)
2317+
if err != nil {
2318+
log.Error("Select %s: %v", desc, err)
2319+
} else {
2320+
for _, result := range results {
2321+
id := com.StrTo(result["id"]).MustInt64()
2322+
log.Trace("Updating %s: %d", desc, id)
2323+
_, err = x.Exec("UPDATE `repository` SET num_closed_pulls=(SELECT COUNT(*) FROM `issue` WHERE repo_id=? AND is_closed=? AND is_pull=?) WHERE id=?", id, true, true, id)
2324+
if err != nil {
2325+
log.Error("Update %s[%d]: %v", desc, id, err)
2326+
}
2327+
}
2328+
}
2329+
// ***** END: Repository.NumClosedPulls *****
2330+
23142331
// FIXME: use checker when stop supporting old fork repo format.
23152332
// ***** START: Repository.NumForks *****
23162333
results, err = x.Query("SELECT repo.id FROM `repository` repo WHERE repo.num_forks!=(SELECT COUNT(*) FROM `repository` WHERE fork_id=repo.id)")

0 commit comments

Comments
 (0)