Skip to content

Commit

Permalink
Added issue search via api (#3612)
Browse files Browse the repository at this point in the history
* Started implementing issue api search

* Implemented issue search via api

* Added search to swagger.json

* Removed todo

* fmt

* Added comment to generate swagger json via "generate-swagger"

* Simplified search

* fmt

* Removed unessecary comment

* Removed unessecary declaration of the issues-variable

* Removed unessecary comment

* Removed unessecary comment

* Added explanation keyword

* Simplified check for empty keyword

* corrected check if keyword is empty
  • Loading branch information
kolaente authored and lafriks committed Mar 7, 2018
1 parent d71f510 commit 1a83581
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
6 changes: 6 additions & 0 deletions public/swagger.v1.json
Expand Up @@ -1830,6 +1830,12 @@
"description": "page number of requested issues",
"name": "page",
"in": "query"
},
{
"type": "string",
"description": "search string",
"name": "q",
"in": "query"
}
],
"responses": {
Expand Down
35 changes: 29 additions & 6 deletions routers/api/v1/repo/issue.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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"
Expand All @@ -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 {
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
Expand Down

0 comments on commit 1a83581

Please sign in to comment.