diff options
author | 6543 <6543@obermui.de> | 2021-06-17 00:33:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-16 18:33:37 -0400 |
commit | 0e081ff0ce61227d5f34f1d7f8213d9f407f1f3d (patch) | |
tree | ff3bf587cf5af0cea1226b2fa219b27a3336594c /routers/api | |
parent | ffbf35b7e9e2ea65525229f2c7d5a076c98e66ba (diff) | |
download | gitea-0e081ff0ce61227d5f34f1d7f8213d9f407f1f3d.tar.gz gitea-0e081ff0ce61227d5f34f1d7f8213d9f407f1f3d.zip |
[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>
Diffstat (limited to 'routers/api')
-rw-r--r-- | routers/api/v1/repo/issue.go | 83 |
1 files changed, 75 insertions, 8 deletions
diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index 6b46dc0fef..5932765ab8 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -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 |