Skip to content

Commit a3f9e92

Browse files
Gustedwxiaoguangzeripath
authored
Fix stats upon searching issues (#17566)
* 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 <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent 640f0e1 commit a3f9e92

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

models/issue.go

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

1530-
countSession := func(opts *IssueStatsOptions) *xorm.Session {
1530+
countSession := func(opts *IssueStatsOptions, issueIDs []int64) *xorm.Session {
15311531
sess := db.GetEngine(db.DefaultContext).
15321532
Where("issue.repo_id = ?", opts.RepoID)
15331533

1534-
if len(opts.IssueIDs) > 0 {
1535-
sess.In("issue.id", opts.IssueIDs)
1534+
if len(issueIDs) > 0 {
1535+
sess.In("issue.id", issueIDs)
15361536
}
15371537

15381538
if len(opts.Labels) > 0 && opts.Labels != "0" {
@@ -1582,13 +1582,13 @@ func getIssueStatsChunk(opts *IssueStatsOptions, issueIDs []int64) (*IssueStats,
15821582
}
15831583

15841584
var err error
1585-
stats.OpenCount, err = countSession(opts).
1585+
stats.OpenCount, err = countSession(opts, issueIDs).
15861586
And("issue.is_closed = ?", false).
15871587
Count(new(Issue))
15881588
if err != nil {
15891589
return stats, err
15901590
}
1591-
stats.ClosedCount, err = countSession(opts).
1591+
stats.ClosedCount, err = countSession(opts, issueIDs).
15921592
And("issue.is_closed = ?", true).
15931593
Count(new(Issue))
15941594
return stats, err

models/issue_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,43 @@ func TestResourceIndex(t *testing.T) {
435435
}
436436
wg.Wait()
437437
}
438+
439+
func TestCorrectIssueStats(t *testing.T) {
440+
assert.NoError(t, db.PrepareTestDatabase())
441+
442+
// Because the condition is to have chunked database look-ups,
443+
// We have to more issues than `maxQueryParameters`, we will insert.
444+
// maxQueryParameters + 10 issues into the testDatabase.
445+
// Each new issues will have a constant description "Bugs are nasty"
446+
// Which will be used later on.
447+
448+
issueAmount := maxQueryParameters + 10
449+
450+
var wg sync.WaitGroup
451+
for i := 0; i < issueAmount; i++ {
452+
wg.Add(1)
453+
go func(i int) {
454+
testInsertIssue(t, fmt.Sprintf("Issue %d", i+1), "Bugs are nasty", 0)
455+
wg.Done()
456+
}(i)
457+
}
458+
wg.Wait()
459+
460+
// Now we will get all issueID's that match the "Bugs are nasty" query.
461+
total, ids, err := SearchIssueIDsByKeyword("Bugs are nasty", []int64{1}, issueAmount, 0)
462+
463+
// Just to be sure.
464+
assert.NoError(t, err)
465+
assert.EqualValues(t, issueAmount, total)
466+
467+
// Now we will call the GetIssueStats with these IDs and if working,
468+
// get the correct stats back.
469+
issueStats, err := GetIssueStats(&IssueStatsOptions{
470+
RepoID: 1,
471+
IssueIDs: ids,
472+
})
473+
474+
// Now check the values.
475+
assert.NoError(t, err)
476+
assert.EqualValues(t, issueStats.OpenCount, issueAmount)
477+
}

0 commit comments

Comments
 (0)