Skip to content

Commit 6fa19a8

Browse files
hinoshiba6543zeripathtechknowlogick
authored
Fixed count of filtered issues when api request. (#12275)
* Improved total count of issue when filtered. * Fixed size of slice when selected 1 repository. * Improved function of error check. * improved comment * Added parameter of return header. Co-authored-by: 6543 <[email protected]> * Updated corresponded to the current vendored of "xorm.io/xorm". * Dedublicated it by store the Options Struct into a variable. * format code * Update routers/api/v1/repo/issue.go Co-authored-by: 6543 <[email protected]> * Update routers/api/v1/repo/issue.go Co-authored-by: 6543 <[email protected]> * Updated number of range. Co-authored-by: 6543 <[email protected]> * Updated number of range. Co-authored-by: 6543 <[email protected]> * Removed total value. * make fmt * Improved value of sql. Co-authored-by: zeripath <[email protected]> * Improved value of sql. * improved message * improved message * improved message * fixed message Co-authored-by: 6543 <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent b510927 commit 6fa19a8

File tree

2 files changed

+64
-29
lines changed

2 files changed

+64
-29
lines changed

models/issue.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
12661266
opts.setupSession(sess)
12671267
sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID)
12681268

1269-
issues := make([]*Issue, 0, setting.UI.IssuePagingNum)
1269+
issues := make([]*Issue, 0, opts.ListOptions.PageSize)
12701270
if err := sess.Find(&issues); err != nil {
12711271
return nil, fmt.Errorf("Find: %v", err)
12721272
}
@@ -1279,6 +1279,27 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
12791279
return issues, nil
12801280
}
12811281

1282+
// CountIssues number return of issues by given conditions.
1283+
func CountIssues(opts *IssuesOptions) (int64, error) {
1284+
sess := x.NewSession()
1285+
defer sess.Close()
1286+
1287+
countsSlice := make([]*struct {
1288+
RepoID int64
1289+
Count int64
1290+
}, 0, 1)
1291+
1292+
sess.Select("COUNT(issue.id) AS count").Table("issue")
1293+
opts.setupSession(sess)
1294+
if err := sess.Find(&countsSlice); err != nil {
1295+
return 0, fmt.Errorf("Find: %v", err)
1296+
}
1297+
if len(countsSlice) < 1 {
1298+
return 0, fmt.Errorf("there is less than one result sql record")
1299+
}
1300+
return countsSlice[0].Count, nil
1301+
}
1302+
12821303
// GetParticipantsIDsByIssueID returns the IDs of all users who participated in comments of an issue,
12831304
// but skips joining with `user` for performance reasons.
12841305
// User permissions must be verified elsewhere if required.

routers/api/v1/repo/issue.go

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ func SearchIssues(ctx *context.APIContext) {
9393
opts.AllLimited = true
9494
}
9595

96-
issueCount := 0
9796
for page := 1; ; page++ {
9897
opts.Page = page
9998
repos, count, err := models.SearchRepositoryByName(opts)
@@ -107,19 +106,12 @@ func SearchIssues(ctx *context.APIContext) {
107106
}
108107
log.Trace("Processing next %d repos of %d", len(repos), count)
109108
for _, repo := range repos {
110-
switch isClosed {
111-
case util.OptionalBoolTrue:
112-
issueCount += repo.NumClosedIssues
113-
case util.OptionalBoolFalse:
114-
issueCount += repo.NumOpenIssues
115-
case util.OptionalBoolNone:
116-
issueCount += repo.NumIssues
117-
}
118109
repoIDs = append(repoIDs, repo.ID)
119110
}
120111
}
121112

122113
var issues []*models.Issue
114+
var filteredCount int64
123115

124116
keyword := strings.Trim(ctx.Query("q"), " ")
125117
if strings.IndexByte(keyword, 0) >= 0 {
@@ -129,7 +121,10 @@ func SearchIssues(ctx *context.APIContext) {
129121
var labelIDs []int64
130122
var err error
131123
if len(keyword) > 0 && len(repoIDs) > 0 {
132-
issueIDs, err = issue_indexer.SearchIssuesByKeyword(repoIDs, keyword)
124+
if issueIDs, err = issue_indexer.SearchIssuesByKeyword(repoIDs, keyword); err != nil {
125+
ctx.Error(http.StatusInternalServerError, "SearchIssuesByKeyword", err)
126+
return
127+
}
133128
}
134129

135130
var isPull util.OptionalBool
@@ -151,29 +146,36 @@ func SearchIssues(ctx *context.APIContext) {
151146
// Only fetch the issues if we either don't have a keyword or the search returned issues
152147
// This would otherwise return all issues if no issues were found by the search.
153148
if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
154-
issues, err = models.Issues(&models.IssuesOptions{
149+
issuesOpt := &models.IssuesOptions{
155150
ListOptions: models.ListOptions{
156151
Page: ctx.QueryInt("page"),
157152
PageSize: setting.UI.IssuePagingNum,
158153
},
159-
160154
RepoIDs: repoIDs,
161155
IsClosed: isClosed,
162156
IssueIDs: issueIDs,
163157
IncludedLabelNames: includedLabelNames,
164158
SortType: "priorityrepo",
165159
PriorityRepoID: ctx.QueryInt64("priority_repo_id"),
166160
IsPull: isPull,
167-
})
168-
}
161+
}
169162

170-
if err != nil {
171-
ctx.Error(http.StatusInternalServerError, "Issues", err)
172-
return
163+
if issues, err = models.Issues(issuesOpt); err != nil {
164+
ctx.Error(http.StatusInternalServerError, "Issues", err)
165+
return
166+
}
167+
168+
issuesOpt.ListOptions = models.ListOptions{
169+
Page: -1,
170+
}
171+
if filteredCount, err = models.CountIssues(issuesOpt); err != nil {
172+
ctx.Error(http.StatusInternalServerError, "CountIssues", err)
173+
return
174+
}
173175
}
174176

175-
ctx.SetLinkHeader(issueCount, setting.UI.IssuePagingNum)
176-
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", issueCount))
177+
ctx.SetLinkHeader(int(filteredCount), setting.UI.IssuePagingNum)
178+
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", filteredCount))
177179
ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link")
178180
ctx.JSON(http.StatusOK, convert.ToAPIIssueList(issues))
179181
}
@@ -241,6 +243,7 @@ func ListIssues(ctx *context.APIContext) {
241243
}
242244

243245
var issues []*models.Issue
246+
var filteredCount int64
244247

245248
keyword := strings.Trim(ctx.Query("q"), " ")
246249
if strings.IndexByte(keyword, 0) >= 0 {
@@ -251,6 +254,10 @@ func ListIssues(ctx *context.APIContext) {
251254
var err error
252255
if len(keyword) > 0 {
253256
issueIDs, err = issue_indexer.SearchIssuesByKeyword([]int64{ctx.Repo.Repository.ID}, keyword)
257+
if err != nil {
258+
ctx.Error(http.StatusInternalServerError, "SearchIssuesByKeyword", err)
259+
return
260+
}
254261
}
255262

256263
if splitted := strings.Split(ctx.Query("labels"), ","); len(splitted) > 0 {
@@ -306,26 +313,33 @@ func ListIssues(ctx *context.APIContext) {
306313
// Only fetch the issues if we either don't have a keyword or the search returned issues
307314
// This would otherwise return all issues if no issues were found by the search.
308315
if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
309-
issues, err = models.Issues(&models.IssuesOptions{
316+
issuesOpt := &models.IssuesOptions{
310317
ListOptions: listOptions,
311318
RepoIDs: []int64{ctx.Repo.Repository.ID},
312319
IsClosed: isClosed,
313320
IssueIDs: issueIDs,
314321
LabelIDs: labelIDs,
315322
MilestoneIDs: mileIDs,
316323
IsPull: isPull,
317-
})
318-
}
324+
}
319325

320-
if err != nil {
321-
ctx.Error(http.StatusInternalServerError, "Issues", err)
322-
return
326+
if issues, err = models.Issues(issuesOpt); err != nil {
327+
ctx.Error(http.StatusInternalServerError, "Issues", err)
328+
return
329+
}
330+
331+
issuesOpt.ListOptions = models.ListOptions{
332+
Page: -1,
333+
}
334+
if filteredCount, err = models.CountIssues(issuesOpt); err != nil {
335+
ctx.Error(http.StatusInternalServerError, "CountIssues", err)
336+
return
337+
}
323338
}
324339

325-
ctx.SetLinkHeader(ctx.Repo.Repository.NumIssues, listOptions.PageSize)
326-
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", ctx.Repo.Repository.NumIssues))
340+
ctx.SetLinkHeader(int(filteredCount), listOptions.PageSize)
341+
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", filteredCount))
327342
ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link")
328-
329343
ctx.JSON(http.StatusOK, convert.ToAPIIssueList(issues))
330344
}
331345

0 commit comments

Comments
 (0)