summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-07-19 05:51:33 +0800
committerzeripath <art27@cantab.net>2019-07-18 22:51:33 +0100
commite0d6d2f97816f972d408308c740bacc450584e00 (patch)
treee68026c89e9a2c3bded17f45e24a459bf6b266f0 /models
parentf9d6e35a8ad0a3bbf1a9f9732f192a3588f95c3b (diff)
downloadgitea-e0d6d2f97816f972d408308c740bacc450584e00.tar.gz
gitea-e0d6d2f97816f972d408308c740bacc450584e00.zip
Fix repository's pull request count error (#7518)
* fix pr count error * fix tests
Diffstat (limited to 'models')
-rw-r--r--models/issue.go19
-rw-r--r--models/issue_comment.go14
-rw-r--r--models/repo.go17
3 files changed, 38 insertions, 12 deletions
diff --git a/models/issue.go b/models/issue.go
index bde5758b02..7561083e0f 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -1849,3 +1849,22 @@ func (issue *Issue) BlockedByDependencies() ([]*Issue, error) {
func (issue *Issue) BlockingDependencies() ([]*Issue, error) {
return issue.getBlockingDependencies(x)
}
+
+func (issue *Issue) updateClosedNum(e Engine) (err error) {
+ if issue.IsPull {
+ _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=(SELECT count(*) FROM issue WHERE repo_id=? AND is_pull=? AND is_closed=?) WHERE id=?",
+ issue.RepoID,
+ true,
+ true,
+ issue.RepoID,
+ )
+ } else {
+ _, err = e.Exec("UPDATE `repository` SET num_closed_issues=(SELECT count(*) FROM issue WHERE repo_id=? AND is_pull=? AND is_closed=?) WHERE id=?",
+ issue.RepoID,
+ false,
+ true,
+ issue.RepoID,
+ )
+ }
+ return
+}
diff --git a/models/issue_comment.go b/models/issue_comment.go
index b930b0b12a..5e7ebc0cf3 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -634,12 +634,7 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen
act.OpType = ActionReopenPullRequest
}
- if opts.Issue.IsPull {
- _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls-1 WHERE id=?", opts.Repo.ID)
- } else {
- _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues-1 WHERE id=?", opts.Repo.ID)
- }
- if err != nil {
+ if err = opts.Issue.updateClosedNum(e); err != nil {
return err
}
@@ -649,12 +644,7 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen
act.OpType = ActionClosePullRequest
}
- if opts.Issue.IsPull {
- _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls+1 WHERE id=?", opts.Repo.ID)
- } else {
- _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues+1 WHERE id=?", opts.Repo.ID)
- }
- if err != nil {
+ if err = opts.Issue.updateClosedNum(e); err != nil {
return err
}
}
diff --git a/models/repo.go b/models/repo.go
index c60488844c..ba14155395 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -2311,6 +2311,23 @@ func CheckRepoStats() {
}
// ***** END: Repository.NumClosedIssues *****
+ // ***** START: Repository.NumClosedPulls *****
+ desc = "repository count 'num_closed_pulls'"
+ 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)
+ if err != nil {
+ log.Error("Select %s: %v", desc, err)
+ } else {
+ for _, result := range results {
+ id := com.StrTo(result["id"]).MustInt64()
+ log.Trace("Updating %s: %d", desc, id)
+ _, 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)
+ if err != nil {
+ log.Error("Update %s[%d]: %v", desc, id, err)
+ }
+ }
+ }
+ // ***** END: Repository.NumClosedPulls *****
+
// FIXME: use checker when stop supporting old fork repo format.
// ***** START: Repository.NumForks *****
results, err = x.Query("SELECT repo.id FROM `repository` repo WHERE repo.num_forks!=(SELECT COUNT(*) FROM `repository` WHERE fork_id=repo.id)")