Skip to content

support search issues pagination #22704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions modules/indexer/issues/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,22 +381,22 @@ func DeleteRepoIssueIndexer(ctx context.Context, repo *repo_model.Repository) {

// SearchIssuesByKeyword search issue ids by keywords and repo id
// WARNNING: You have to ensure user have permission to visit repoIDs' issues
func SearchIssuesByKeyword(ctx context.Context, repoIDs []int64, keyword string) ([]int64, error) {
func SearchIssuesByKeyword(ctx context.Context, repoIDs []int64, keyword string, start, limit int) (int64, []int64, error) {
var issueIDs []int64
indexer := holder.get()

if indexer == nil {
log.Error("SearchIssuesByKeyword(): unable to get indexer!")
return nil, fmt.Errorf("unable to get issue indexer")
return 0, nil, fmt.Errorf("unable to get issue indexer")
}
res, err := indexer.Search(ctx, keyword, repoIDs, 50, 0)
res, err := indexer.Search(ctx, keyword, repoIDs, limit, start)
if err != nil {
return nil, err
return 0, nil, err
}
for _, r := range res.Hits {
issueIDs = append(issueIDs, r.ID)
}
return issueIDs, nil
return res.Total, issueIDs, nil
}

// IsAvailable checks if issue indexer is available
Expand Down
16 changes: 8 additions & 8 deletions modules/indexer/issues/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ func TestBleveSearchIssues(t *testing.T) {

time.Sleep(5 * time.Second)

ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2")
_, ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2", 0, 50)
assert.NoError(t, err)
assert.EqualValues(t, []int64{2}, ids)

ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first")
_, ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first", 0, 50)
assert.NoError(t, err)
assert.EqualValues(t, []int64{1}, ids)

ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for")
_, ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for", 0, 50)
assert.NoError(t, err)
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)

ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good")
_, ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good", 0, 50)
assert.NoError(t, err)
assert.EqualValues(t, []int64{1}, ids)
}
Expand All @@ -74,19 +74,19 @@ func TestDBSearchIssues(t *testing.T) {
setting.Indexer.IssueType = "db"
InitIssueIndexer(true)

ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2")
_, ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2", 0, 50)
assert.NoError(t, err)
assert.EqualValues(t, []int64{2}, ids)

ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first")
_, ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first", 0, 50)
assert.NoError(t, err)
assert.EqualValues(t, []int64{1}, ids)

ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for")
_, ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for", 0, 50)
assert.NoError(t, err)
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)

ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good")
_, ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good", 0, 50)
assert.NoError(t, err)
assert.EqualValues(t, []int64{1}, ids)
}
53 changes: 28 additions & 25 deletions routers/api/v1/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,18 @@ func SearchIssues(ctx *context.APIContext) {
if strings.IndexByte(keyword, 0) >= 0 {
keyword = ""
}
// this api is also used in UI,
// so the default limit is set to fit UI needs
limit := ctx.FormInt("limit")
if limit == 0 {
limit = setting.UI.IssuePagingNum
} else if limit > setting.API.MaxResponseItems {
limit = setting.API.MaxResponseItems
}
Comment on lines +192 to +199
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤨
Sounds like a bug report waiting to happen.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines just moved from line 222 - 230


var issueIDs []int64
if len(keyword) > 0 && len(repoIDs) > 0 {
if issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, repoIDs, keyword); err != nil {
if filteredCount, issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, repoIDs, keyword, limit*ctx.FormInt("page"), limit); err != nil {
ctx.Error(http.StatusInternalServerError, "SearchIssuesByKeyword", err)
return
}
Expand Down Expand Up @@ -219,15 +228,6 @@ func SearchIssues(ctx *context.APIContext) {
includedMilestones = strings.Split(milestones, ",")
}

// this api is also used in UI,
// so the default limit is set to fit UI needs
limit := ctx.FormInt("limit")
if limit == 0 {
limit = setting.UI.IssuePagingNum
} else if limit > setting.API.MaxResponseItems {
limit = setting.API.MaxResponseItems
}

// Only fetch the issues if we either don't have a keyword or the search returned issues
// This would otherwise return all issues if no issues were found by the search.
if len(keyword) == 0 || len(issueIDs) > 0 || len(includedLabelNames) > 0 || len(includedMilestones) > 0 {
Expand Down Expand Up @@ -272,12 +272,14 @@ func SearchIssues(ctx *context.APIContext) {
return
}

issuesOpt.ListOptions = db.ListOptions{
Page: -1,
}
if filteredCount, err = issues_model.CountIssues(ctx, issuesOpt); err != nil {
ctx.Error(http.StatusInternalServerError, "CountIssues", err)
return
if filteredCount == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what circumstances is filteredCount 0 and why should we return the total number of issues then?
Can't we just delete this whole branch now?

issuesOpt.ListOptions = db.ListOptions{
Page: -1,
}
if filteredCount, err = issues_model.CountIssues(ctx, issuesOpt); err != nil {
ctx.Error(http.StatusInternalServerError, "CountIssues", err)
return
}
}
}

Expand Down Expand Up @@ -386,8 +388,9 @@ func ListIssues(ctx *context.APIContext) {
}
var issueIDs []int64
var labelIDs []int64
listOptions := utils.GetListOptions(ctx)
if len(keyword) > 0 {
issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, []int64{ctx.Repo.Repository.ID}, keyword)
filteredCount, issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, []int64{ctx.Repo.Repository.ID}, keyword, listOptions.Page*listOptions.PageSize, listOptions.PageSize)
if err != nil {
ctx.Error(http.StatusInternalServerError, "SearchIssuesByKeyword", err)
return
Expand Down Expand Up @@ -432,8 +435,6 @@ func ListIssues(ctx *context.APIContext) {
}
}

listOptions := utils.GetListOptions(ctx)

var isPull util.OptionalBool
switch ctx.FormString("type") {
case "pulls":
Expand Down Expand Up @@ -481,12 +482,14 @@ func ListIssues(ctx *context.APIContext) {
return
}

issuesOpt.ListOptions = db.ListOptions{
Page: -1,
}
if filteredCount, err = issues_model.CountIssues(ctx, issuesOpt); err != nil {
ctx.Error(http.StatusInternalServerError, "CountIssues", err)
return
if filteredCount == 0 {
issuesOpt.ListOptions = db.ListOptions{
Page: -1,
}
if filteredCount, err = issues_model.CountIssues(ctx, issuesOpt); err != nil {
ctx.Error(http.StatusInternalServerError, "CountIssues", err)
return
}
Comment on lines +485 to +492
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

}
}

Expand Down
63 changes: 34 additions & 29 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti

var issueIDs []int64
if len(keyword) > 0 {
issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, []int64{repo.ID}, keyword)
// FIXME: we don't know how many opened/closed
_, issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, []int64{repo.ID}, keyword, 0, 50)
if err != nil {
if issue_indexer.IsAvailable() {
ctx.ServerError("issueIndexer.Search", err)
Expand Down Expand Up @@ -2341,13 +2342,22 @@ func SearchIssues(ctx *context.Context) {
var issues []*issues_model.Issue
var filteredCount int64

// this api is also used in UI,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is no longer an API

// so the default limit is set to fit UI needs
limit := ctx.FormInt("limit")
if limit == 0 {
limit = setting.UI.IssuePagingNum
} else if limit > setting.API.MaxResponseItems {
limit = setting.API.MaxResponseItems
}

keyword := ctx.FormTrim("q")
if strings.IndexByte(keyword, 0) >= 0 {
keyword = ""
}
var issueIDs []int64
if len(keyword) > 0 && len(repoIDs) > 0 {
if issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, repoIDs, keyword); err != nil {
if filteredCount, issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, repoIDs, keyword, ctx.FormInt("page")*limit, limit); err != nil {
ctx.Error(http.StatusInternalServerError, "SearchIssuesByKeyword", err.Error())
return
}
Expand Down Expand Up @@ -2377,15 +2387,6 @@ func SearchIssues(ctx *context.Context) {

projectID := ctx.FormInt64("project")

// this api is also used in UI,
// so the default limit is set to fit UI needs
limit := ctx.FormInt("limit")
if limit == 0 {
limit = setting.UI.IssuePagingNum
} else if limit > setting.API.MaxResponseItems {
limit = setting.API.MaxResponseItems
}

// Only fetch the issues if we either don't have a keyword or the search returned issues
// This would otherwise return all issues if no issues were found by the search.
if len(keyword) == 0 || len(issueIDs) > 0 || len(includedLabelNames) > 0 || len(includedMilestones) > 0 {
Expand Down Expand Up @@ -2431,12 +2432,14 @@ func SearchIssues(ctx *context.Context) {
return
}

issuesOpt.ListOptions = db.ListOptions{
Page: -1,
}
if filteredCount, err = issues_model.CountIssues(ctx, issuesOpt); err != nil {
ctx.Error(http.StatusInternalServerError, "CountIssues", err.Error())
return
if filteredCount == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

issuesOpt.ListOptions = db.ListOptions{
Page: -1,
}
if filteredCount, err = issues_model.CountIssues(ctx, issuesOpt); err != nil {
ctx.Error(http.StatusInternalServerError, "CountIssues", err.Error())
return
}
}
}

Expand Down Expand Up @@ -2489,10 +2492,15 @@ func ListIssues(ctx *context.Context) {
if strings.IndexByte(keyword, 0) >= 0 {
keyword = ""
}
listOptions := db.ListOptions{
Page: ctx.FormInt("page"),
PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")),
}

var issueIDs []int64
var labelIDs []int64
if len(keyword) > 0 {
issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, []int64{ctx.Repo.Repository.ID}, keyword)
filteredCount, issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, []int64{ctx.Repo.Repository.ID}, keyword, listOptions.Page*listOptions.PageSize, listOptions.PageSize)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -2539,11 +2547,6 @@ func ListIssues(ctx *context.Context) {

projectID := ctx.FormInt64("project")

listOptions := db.ListOptions{
Page: ctx.FormInt("page"),
PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")),
}

var isPull util.OptionalBool
switch ctx.FormString("type") {
case "pulls":
Expand Down Expand Up @@ -2592,12 +2595,14 @@ func ListIssues(ctx *context.Context) {
return
}

issuesOpt.ListOptions = db.ListOptions{
Page: -1,
}
if filteredCount, err = issues_model.CountIssues(ctx, issuesOpt); err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
if filteredCount == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

issuesOpt.ListOptions = db.ListOptions{
Page: -1,
}
if filteredCount, err = issues_model.CountIssues(ctx, issuesOpt); err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion routers/web/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ func issueIDsFromSearch(ctx *context.Context, ctxUser *user_model.User, keyword
if err != nil {
return nil, fmt.Errorf("GetRepoIDsForIssuesOptions: %w", err)
}
issueIDsFromSearch, err := issue_indexer.SearchIssuesByKeyword(ctx, searchRepoIDs, keyword)
_, issueIDsFromSearch, err := issue_indexer.SearchIssuesByKeyword(ctx, searchRepoIDs, keyword, 0, 50)
if err != nil {
return nil, fmt.Errorf("SearchIssuesByKeyword: %w", err)
}
Expand Down