[API] Add "before" query to ListIssueComments and ListRepoIssue… (#9685)

* add "before" query to ListIssueComments and ListRepoIssueComments

* Add TEST

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com>
release/v1.15
6543 2020-01-13 17:02:24 +01:00 committed by Antoine GIRARD
parent b7ffc6a096
commit 0b3aaa6196
5 changed files with 73 additions and 14 deletions

View File

@ -7,6 +7,7 @@ package integrations
import (
"fmt"
"net/http"
"net/url"
"testing"
"code.gitea.io/gitea/models"
@ -25,18 +26,40 @@ func TestAPIListRepoComments(t *testing.T) {
repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
session := loginUser(t, repoOwner.Name)
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments",
repoOwner.Name, repo.Name)
link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments", repoOwner.Name, repo.Name))
req := NewRequest(t, "GET", link.String())
resp := session.MakeRequest(t, req, http.StatusOK)
var apiComments []*api.Comment
DecodeJSON(t, resp, &apiComments)
assert.Len(t, apiComments, 2)
for _, apiComment := range apiComments {
c := &models.Comment{ID: apiComment.ID}
models.AssertExistsAndLoadBean(t, c,
models.Cond("type = ?", models.CommentTypeComment))
models.AssertExistsAndLoadBean(t, &models.Issue{ID: c.IssueID, RepoID: repo.ID})
}
//test before and since filters
query := url.Values{}
before := "2000-01-01T00:00:11+00:00" //unix: 946684811
since := "2000-01-01T00:00:12+00:00" //unix: 946684812
query.Add("before", before)
link.RawQuery = query.Encode()
req = NewRequest(t, "GET", link.String())
resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiComments)
assert.Len(t, apiComments, 1)
assert.EqualValues(t, 2, apiComments[0].ID)
query.Del("before")
query.Add("since", since)
link.RawQuery = query.Encode()
req = NewRequest(t, "GET", link.String())
resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiComments)
assert.Len(t, apiComments, 1)
assert.EqualValues(t, 3, apiComments[0].ID)
}
func TestAPIListIssueComments(t *testing.T) {

View File

@ -13,6 +13,7 @@
issue_id: 1 # in repo_id 1
content: "good work!"
created_unix: 946684811
updated_unix: 946684811
-
id: 3
type: 0 # comment
@ -20,6 +21,7 @@
issue_id: 1 # in repo_id 1
content: "meh..."
created_unix: 946684812
updated_unix: 946684812
-
id: 4
type: 21 # code comment

View File

@ -782,6 +782,7 @@ type FindCommentsOptions struct {
IssueID int64
ReviewID int64
Since int64
Before int64
Type CommentType
}
@ -799,6 +800,9 @@ func (opts *FindCommentsOptions) toConds() builder.Cond {
if opts.Since > 0 {
cond = cond.And(builder.Gte{"comment.updated_unix": opts.Since})
}
if opts.Before > 0 {
cond = cond.And(builder.Lte{"comment.updated_unix": opts.Before})
}
if opts.Type != CommentTypeUnknown {
cond = cond.And(builder.Eq{"comment.type": opts.Type})
}

View File

@ -7,11 +7,11 @@ package repo
import (
"errors"
"net/http"
"time"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
comment_service "code.gitea.io/gitea/services/comments"
)
@ -43,16 +43,21 @@ func ListIssueComments(ctx *context.APIContext) {
// in: query
// description: if provided, only comments updated since the specified time are returned.
// type: string
// format: date-time
// - name: before
// in: query
// description: if provided, only comments updated before the provided time are returned.
// type: string
// format: date-time
// responses:
// "200":
// "$ref": "#/responses/CommentList"
var since time.Time
if len(ctx.Query("since")) > 0 {
since, _ = time.Parse(time.RFC3339, ctx.Query("since"))
before, since, err := utils.GetQueryBeforeSince(ctx)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetQueryBeforeSince", err)
return
}
// comments,err:=models.GetCommentsByIssueIDSince(, since)
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetRawIssueByIndex", err)
@ -62,7 +67,8 @@ func ListIssueComments(ctx *context.APIContext) {
comments, err := models.FindComments(models.FindCommentsOptions{
IssueID: issue.ID,
Since: since.Unix(),
Since: since,
Before: before,
Type: models.CommentTypeComment,
})
if err != nil {
@ -105,18 +111,26 @@ func ListRepoIssueComments(ctx *context.APIContext) {
// in: query
// description: if provided, only comments updated since the provided time are returned.
// type: string
// format: date-time
// - name: before
// in: query
// description: if provided, only comments updated before the provided time are returned.
// type: string
// format: date-time
// responses:
// "200":
// "$ref": "#/responses/CommentList"
var since time.Time
if len(ctx.Query("since")) > 0 {
since, _ = time.Parse(time.RFC3339, ctx.Query("since"))
before, since, err := utils.GetQueryBeforeSince(ctx)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetQueryBeforeSince", err)
return
}
comments, err := models.FindComments(models.FindCommentsOptions{
RepoID: ctx.Repo.Repository.ID,
Since: since.Unix(),
Since: since,
Before: before,
Type: models.CommentTypeComment,
})
if err != nil {

View File

@ -3196,9 +3196,17 @@
},
{
"type": "string",
"format": "date-time",
"description": "if provided, only comments updated since the provided time are returned.",
"name": "since",
"in": "query"
},
{
"type": "string",
"format": "date-time",
"description": "if provided, only comments updated before the provided time are returned.",
"name": "before",
"in": "query"
}
],
"responses": {
@ -3652,9 +3660,17 @@
},
{
"type": "string",
"format": "date-time",
"description": "if provided, only comments updated since the specified time are returned.",
"name": "since",
"in": "query"
},
{
"type": "string",
"format": "date-time",
"description": "if provided, only comments updated before the provided time are returned.",
"name": "before",
"in": "query"
}
],
"responses": {