Skip to content

Commit 1cb1101

Browse files
Gustedwxiaoguangzeripath
authored
backport(1.15): Fix stats upon searching issues (#17578)
- Backport of #17566 Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent 653dff4 commit 1cb1101

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

models/issue.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,12 +1517,12 @@ func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) {
15171517
func getIssueStatsChunk(opts *IssueStatsOptions, issueIDs []int64) (*IssueStats, error) {
15181518
stats := &IssueStats{}
15191519

1520-
countSession := func(opts *IssueStatsOptions) *xorm.Session {
1520+
countSession := func(opts *IssueStatsOptions, issueIDs []int64) *xorm.Session {
15211521
sess := x.
15221522
Where("issue.repo_id = ?", opts.RepoID)
15231523

1524-
if len(opts.IssueIDs) > 0 {
1525-
sess.In("issue.id", opts.IssueIDs)
1524+
if len(issueIDs) > 0 {
1525+
sess.In("issue.id", issueIDs)
15261526
}
15271527

15281528
if len(opts.Labels) > 0 && opts.Labels != "0" {
@@ -1572,13 +1572,13 @@ func getIssueStatsChunk(opts *IssueStatsOptions, issueIDs []int64) (*IssueStats,
15721572
}
15731573

15741574
var err error
1575-
stats.OpenCount, err = countSession(opts).
1575+
stats.OpenCount, err = countSession(opts, issueIDs).
15761576
And("issue.is_closed = ?", false).
15771577
Count(new(Issue))
15781578
if err != nil {
15791579
return stats, err
15801580
}
1581-
stats.ClosedCount, err = countSession(opts).
1581+
stats.ClosedCount, err = countSession(opts, issueIDs).
15821582
And("issue.is_closed = ?", true).
15831583
Count(new(Issue))
15841584
return stats, err

models/issue_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
package models
66

77
import (
8+
"fmt"
89
"sort"
10+
"sync"
911
"testing"
1012
"time"
1113

@@ -417,3 +419,43 @@ func TestIssue_ResolveMentions(t *testing.T) {
417419
// Private repo, whole team
418420
testSuccess("user17", "big_test_private_4", "user15", []string{"user17/owners"}, []int64{18})
419421
}
422+
423+
func TestCorrectIssueStats(t *testing.T) {
424+
assert.NoError(t, PrepareTestDatabase())
425+
426+
// Because the condition is to have chunked database look-ups,
427+
// We have to more issues than `maxQueryParameters`, we will insert.
428+
// maxQueryParameters + 10 issues into the testDatabase.
429+
// Each new issues will have a constant description "Bugs are nasty"
430+
// Which will be used later on.
431+
432+
issueAmount := maxQueryParameters + 10
433+
434+
var wg sync.WaitGroup
435+
for i := 0; i < issueAmount; i++ {
436+
wg.Add(1)
437+
go func(i int) {
438+
testInsertIssue(t, fmt.Sprintf("Issue %d", i+1), "Bugs are nasty", 0)
439+
wg.Done()
440+
}(i)
441+
}
442+
wg.Wait()
443+
444+
// Now we will get all issueID's that match the "Bugs are nasty" query.
445+
total, ids, err := SearchIssueIDsByKeyword("Bugs are nasty", []int64{1}, issueAmount, 0)
446+
447+
// Just to be sure.
448+
assert.NoError(t, err)
449+
assert.EqualValues(t, issueAmount, total)
450+
451+
// Now we will call the GetIssueStats with these IDs and if working,
452+
// get the correct stats back.
453+
issueStats, err := GetIssueStats(&IssueStatsOptions{
454+
RepoID: 1,
455+
IssueIDs: ids,
456+
})
457+
458+
// Now check the values.
459+
assert.NoError(t, err)
460+
assert.EqualValues(t, issueStats.OpenCount, issueAmount)
461+
}

0 commit comments

Comments
 (0)