Skip to content

Commit 783c11c

Browse files
committed
Fix incorrect open issue count after commit close
This commit fixes the bug that a repositories open issue value is not updated when an issue is closed through a commit that references such issue. More specifically, creating an issue on a repository with currently 0 open bugs lead to the repository showing one open issue. Closing such issue through a commit onto the branch this issue was targetting would close the issue correctly but failed to update the issue counter in the repositories header. Hence the repository would show one open issue while the open issue list remains empty. As this commit is a response to #10536, more information about the bug can be aquired there. Fixes #10536.
1 parent 421e7b7 commit 783c11c

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

models/repo.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,27 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
18721872
}
18731873
}
18741874

1875+
// FetchClosedIssueNum fetches the currently stored repositories NumClosedIssues from the database.
1876+
// To further clarify, this method does NOT:
1877+
// - Recalculate the number of closed issues in the database
1878+
// - Return the repositories value of the locally stored NumClosedIssues
1879+
// The method will only return an error if an actual SQL error occurred.
1880+
// If no value was found, a -1 and a nil error will be returned.
1881+
func (repo *Repository) FetchClosedIssueNum() (int, error) {
1882+
var value int
1883+
found, err := x.Table("repository").
1884+
Select("num_closed_issues").
1885+
Where("id = ?", repo.ID).
1886+
Get(&value)
1887+
if err != nil {
1888+
return 0, err
1889+
}
1890+
if !found {
1891+
return -1, nil
1892+
}
1893+
return value, nil
1894+
}
1895+
18751896
// CheckRepoStats checks the repository stats
18761897
func CheckRepoStats(ctx context.Context) {
18771898
log.Trace("Doing: CheckRepoStats")

modules/repofiles/action.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func changeIssueStatus(repo *models.Repository, issue *models.Issue, doer *model
6060

6161
// UpdateIssuesCommit checks if issues are manipulated by commit message.
6262
func UpdateIssuesCommit(doer *models.User, repo *models.Repository, commits []*repository.PushCommit, branchName string) error {
63+
// Track if a single commit changed an issue, as we need to update the repo instance in that case.
64+
commitChangedIssue := false
65+
6366
// Commits are appended in the reverse order.
6467
for i := len(commits) - 1; i >= 0; i-- {
6568
c := commits[i]
@@ -142,9 +145,22 @@ func UpdateIssuesCommit(doer *models.User, repo *models.Repository, commits []*r
142145
if err := changeIssueStatus(refRepo, refIssue, doer, close); err != nil {
143146
return err
144147
}
148+
// Set the issue changed flag to true, to later on update the local repo instance
149+
commitChangedIssue = true
145150
}
146151
}
147152
}
153+
154+
if commitChangedIssue {
155+
if fetchedValue, err := repo.FetchClosedIssueNum(); err == nil {
156+
if fetchedValue > -1 {
157+
repo.NumClosedIssues = fetchedValue
158+
}
159+
} else {
160+
return err
161+
}
162+
}
163+
148164
return nil
149165
}
150166

0 commit comments

Comments
 (0)