-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Added issue search via api #3612
Changes from 21 commits
7a54205
5bc4f3f
f3870bf
bfd61f9
752d5b5
b01f5a7
c2542eb
a1211c0
3755d09
7818adc
1bfcb04
38cbfd9
7b08c92
533e1aa
b598149
be9ddba
c5d9b4c
5df8c13
5255550
5f2405d
1db0418
1f209aa
34918c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/indexer" | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/util" | ||
) | ||
|
@@ -42,6 +43,10 @@ func ListIssues(ctx *context.APIContext) { | |
// in: query | ||
// description: page number of requested issues | ||
// type: integer | ||
// - name: q | ||
// in: query | ||
// description: search string | ||
// type: string | ||
// responses: | ||
// "200": | ||
// "$ref": "#/responses/IssueList" | ||
|
@@ -55,12 +60,30 @@ func ListIssues(ctx *context.APIContext) { | |
isClosed = util.OptionalBoolFalse | ||
} | ||
|
||
issues, err := models.Issues(&models.IssuesOptions{ | ||
RepoIDs: []int64{ctx.Repo.Repository.ID}, | ||
Page: ctx.QueryInt("page"), | ||
PageSize: setting.UI.IssuePagingNum, | ||
IsClosed: isClosed, | ||
}) | ||
var issues []*models.Issue | ||
|
||
keyword := strings.Trim(ctx.Query("q"), " ") | ||
if strings.IndexByte(keyword, 0) >= 0 { | ||
keyword = "" | ||
} | ||
var issueIDs []int64 | ||
var err error | ||
if len(keyword) > 0 { | ||
issueIDs, err = indexer.SearchIssuesByKeyword(ctx.Repo.Repository.ID, keyword) | ||
} | ||
|
||
// 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems this line is unnecessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it's necessary, but a comment should be added to explain it. At this point, three outcomes are possible:
This condition guards against condition 2, because otherwise models.Issues would just be called and would return all the issues as if no keyword was passed. So, add comment: // We don't fetch any issue if we have keywords, but SearchIssuesByKeyword returned no issue IDs.
// (In other words, we don't fetch issues if no issue matches our keywords) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add a comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thehowl you are right, but we should avoid using another function names (it can be changed and you can not track changes in comments) in comment, just explain "why" that code is needed. IMO your last part is enough:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Morlinest I've added a comment explaining it. |
||
issues, err = models.Issues(&models.IssuesOptions{ | ||
RepoIDs: []int64{ctx.Repo.Repository.ID}, | ||
Page: ctx.QueryInt("page"), | ||
PageSize: setting.UI.IssuePagingNum, | ||
IsClosed: isClosed, | ||
IssueIDs: issueIDs, | ||
}) | ||
} | ||
|
||
if err != nil { | ||
ctx.Error(500, "Issues", err) | ||
return | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can do something like this:
(simple search function call where you can immediately return empty array instead of doing "if" would be cleaner...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it