Skip to content
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

[API] Add "before" query to ListIssueComments and ListRepoIssueComments #9685

Merged
merged 9 commits into from
Jan 13, 2020
Merged
27 changes: 25 additions & 2 deletions integrations/api_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package integrations
import (
"fmt"
"net/http"
"net/url"
"testing"

"code.gitea.io/gitea/models"
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion models/fixtures/comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
issue_id: 1 # in repo_id 1
content: "good work!"
created_unix: 946684811
updated_unix: 946684811
-
id: 3
type: 0 # comment
poster_id: 5 # user not watching (see watch.yml)
issue_id: 1 # in repo_id 1
content: "meh..."
created_unix: 946684812
updated_unix: 946684812
-
id: 4
type: 21 # code comment
Expand Down Expand Up @@ -63,4 +65,4 @@
review_id: 10
tree_path: "README.md"
created_unix: 946684812
invalidated: true
invalidated: true
4 changes: 4 additions & 0 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ type FindCommentsOptions struct {
IssueID int64
ReviewID int64
Since int64
Before int64
Type CommentType
}

Expand All @@ -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})
}
Expand Down
36 changes: 25 additions & 11 deletions routers/api/v1/repo/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down