From 1cb1101d44f797732d98abdcf8ef6ef2cc0ecd6a Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 8 Nov 2021 22:14:57 +0100 Subject: [PATCH] backport(1.15): Fix stats upon searching issues (#17578) - Backport of https://github.com/go-gitea/gitea/pull/17566 Co-authored-by: wxiaoguang Co-authored-by: zeripath --- models/issue.go | 10 +++++----- models/issue_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/models/issue.go b/models/issue.go index 6e63620b7f..6e5c144785 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1517,12 +1517,12 @@ func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) { func getIssueStatsChunk(opts *IssueStatsOptions, issueIDs []int64) (*IssueStats, error) { stats := &IssueStats{} - countSession := func(opts *IssueStatsOptions) *xorm.Session { + countSession := func(opts *IssueStatsOptions, issueIDs []int64) *xorm.Session { sess := x. Where("issue.repo_id = ?", opts.RepoID) - if len(opts.IssueIDs) > 0 { - sess.In("issue.id", opts.IssueIDs) + if len(issueIDs) > 0 { + sess.In("issue.id", issueIDs) } if len(opts.Labels) > 0 && opts.Labels != "0" { @@ -1572,13 +1572,13 @@ func getIssueStatsChunk(opts *IssueStatsOptions, issueIDs []int64) (*IssueStats, } var err error - stats.OpenCount, err = countSession(opts). + stats.OpenCount, err = countSession(opts, issueIDs). And("issue.is_closed = ?", false). Count(new(Issue)) if err != nil { return stats, err } - stats.ClosedCount, err = countSession(opts). + stats.ClosedCount, err = countSession(opts, issueIDs). And("issue.is_closed = ?", true). Count(new(Issue)) return stats, err diff --git a/models/issue_test.go b/models/issue_test.go index f2c9b7a68f..ec41c44ed3 100644 --- a/models/issue_test.go +++ b/models/issue_test.go @@ -5,7 +5,9 @@ package models import ( + "fmt" "sort" + "sync" "testing" "time" @@ -417,3 +419,43 @@ func TestIssue_ResolveMentions(t *testing.T) { // Private repo, whole team testSuccess("user17", "big_test_private_4", "user15", []string{"user17/owners"}, []int64{18}) } + +func TestCorrectIssueStats(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + // Because the condition is to have chunked database look-ups, + // We have to more issues than `maxQueryParameters`, we will insert. + // maxQueryParameters + 10 issues into the testDatabase. + // Each new issues will have a constant description "Bugs are nasty" + // Which will be used later on. + + issueAmount := maxQueryParameters + 10 + + var wg sync.WaitGroup + for i := 0; i < issueAmount; i++ { + wg.Add(1) + go func(i int) { + testInsertIssue(t, fmt.Sprintf("Issue %d", i+1), "Bugs are nasty", 0) + wg.Done() + }(i) + } + wg.Wait() + + // Now we will get all issueID's that match the "Bugs are nasty" query. + total, ids, err := SearchIssueIDsByKeyword("Bugs are nasty", []int64{1}, issueAmount, 0) + + // Just to be sure. + assert.NoError(t, err) + assert.EqualValues(t, issueAmount, total) + + // Now we will call the GetIssueStats with these IDs and if working, + // get the correct stats back. + issueStats, err := GetIssueStats(&IssueStatsOptions{ + RepoID: 1, + IssueIDs: ids, + }) + + // Now check the values. + assert.NoError(t, err) + assert.EqualValues(t, issueStats.OpenCount, issueAmount) +}