[API] ListIssues add more filters (#16174)

* [API] ListIssues add more filters:
optional filter repo issues by:
 - since
 - before
 - created_by
 - assigned_by
 - mentioned_by

* Add Tests

* Update routers/api/v1/repo/issue.go

Co-authored-by: Lanre Adelowo <adelowomailbox@gmail.com>

* Apply suggestions from code review

Co-authored-by: Lanre Adelowo <adelowomailbox@gmail.com>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
6543 2021-06-17 00:33:37 +02:00 committed by GitHub
parent ffbf35b7e9
commit 0e081ff0ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 134 additions and 15 deletions

View file

@ -266,6 +266,30 @@ func ListIssues(ctx *context.APIContext) {
// in: query
// description: comma separated list of milestone names or ids. It uses names and fall back to ids. Fetch only issues that have any of this milestones. Non existent milestones are discarded
// type: string
// - name: since
// in: query
// description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
// type: string
// format: date-time
// required: false
// - name: before
// in: query
// description: Only show notifications updated before the given time. This is a timestamp in RFC 3339 format
// type: string
// format: date-time
// required: false
// - name: created_by
// in: query
// description: filter (issues / pulls) created to
// type: string
// - name: assigned_by
// in: query
// description: filter (issues / pulls) assigned to
// type: string
// - name: mentioned_by
// in: query
// description: filter (issues / pulls) mentioning to
// type: string
// - name: page
// in: query
// description: page number of results to return (1-based)
@ -277,6 +301,11 @@ func ListIssues(ctx *context.APIContext) {
// responses:
// "200":
// "$ref": "#/responses/IssueList"
before, since, err := utils.GetQueryBeforeSince(ctx)
if err != nil {
ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
return
}
var isClosed util.OptionalBool
switch ctx.Query("state") {
@ -297,7 +326,6 @@ func ListIssues(ctx *context.APIContext) {
}
var issueIDs []int64
var labelIDs []int64
var err error
if len(keyword) > 0 {
issueIDs, err = issue_indexer.SearchIssuesByKeyword([]int64{ctx.Repo.Repository.ID}, keyword)
if err != nil {
@ -356,17 +384,36 @@ func ListIssues(ctx *context.APIContext) {
isPull = util.OptionalBoolNone
}
// FIXME: we should be more efficient here
createdByID := getUserIDForFilter(ctx, "created_by")
if ctx.Written() {
return
}
assignedByID := getUserIDForFilter(ctx, "assigned_by")
if ctx.Written() {
return
}
mentionedByID := getUserIDForFilter(ctx, "mentioned_by")
if ctx.Written() {
return
}
// 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(labelIDs) > 0 {
issuesOpt := &models.IssuesOptions{
ListOptions: listOptions,
RepoIDs: []int64{ctx.Repo.Repository.ID},
IsClosed: isClosed,
IssueIDs: issueIDs,
LabelIDs: labelIDs,
MilestoneIDs: mileIDs,
IsPull: isPull,
ListOptions: listOptions,
RepoIDs: []int64{ctx.Repo.Repository.ID},
IsClosed: isClosed,
IssueIDs: issueIDs,
LabelIDs: labelIDs,
MilestoneIDs: mileIDs,
IsPull: isPull,
UpdatedBeforeUnix: before,
UpdatedAfterUnix: since,
PosterID: createdByID,
AssigneeID: assignedByID,
MentionedID: mentionedByID,
}
if issues, err = models.Issues(issuesOpt); err != nil {
@ -389,6 +436,26 @@ func ListIssues(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, convert.ToAPIIssueList(issues))
}
func getUserIDForFilter(ctx *context.APIContext, queryName string) int64 {
userName := ctx.Query(queryName)
if len(userName) == 0 {
return 0
}
user, err := models.GetUserByName(userName)
if models.IsErrUserNotExist(err) {
ctx.NotFound(err)
return 0
}
if err != nil {
ctx.InternalServerError(err)
return 0
}
return user.ID
}
// GetIssue get an issue of a repository
func GetIssue(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/issues/{index} issue issueGetIssue