]> source.dussan.org Git - gitea.git/commitdiff
Fix stats upon searching issues (#17566)
authorGusted <williamzijl7@hotmail.com>
Mon, 8 Nov 2021 21:14:46 +0000 (22:14 +0100)
committerGitHub <noreply@github.com>
Mon, 8 Nov 2021 21:14:46 +0000 (23:14 +0200)
* Fix stat chunks searching

- Fixes a issue whereby the given chunk of issueIDs wasn't respected and
thus the returned results where not the correct results.

* Add tests

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: zeripath <art27@cantab.net>
models/issue.go
models/issue_test.go

index 0bc1ebd209d29373571d1af252936485a0fb3e84..75aafe51b7be043f6232c0054dfe0c048223b142 100644 (file)
@@ -1527,12 +1527,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 := db.GetEngine(db.DefaultContext).
                        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" {
@@ -1582,13 +1582,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
index 8894d8020176cfbd830c1f269f9796c1b80774c0..9df91aeb9944bf3ef4056007cd29b3fb38a961eb 100644 (file)
@@ -435,3 +435,43 @@ func TestResourceIndex(t *testing.T) {
        }
        wg.Wait()
 }
+
+func TestCorrectIssueStats(t *testing.T) {
+       assert.NoError(t, db.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)
+}