[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>
tokarchuk/v1.17
6543 5 years ago committed by Antoine GIRARD
parent b7ffc6a096
commit 0b3aaa6196
  1. 27
      integrations/api_comment_test.go
  2. 4
      models/fixtures/comment.yml
  3. 4
      models/issue_comment.go
  4. 36
      routers/api/v1/repo/issue_comment.go
  5. 16
      templates/swagger/v1_json.tmpl

@ -7,6 +7,7 @@ package integrations
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"testing" "testing"
"code.gitea.io/gitea/models" "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) repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
session := loginUser(t, repoOwner.Name) session := loginUser(t, repoOwner.Name)
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments", link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments", repoOwner.Name, repo.Name))
repoOwner.Name, repo.Name) req := NewRequest(t, "GET", link.String())
resp := session.MakeRequest(t, req, http.StatusOK) resp := session.MakeRequest(t, req, http.StatusOK)
var apiComments []*api.Comment var apiComments []*api.Comment
DecodeJSON(t, resp, &apiComments) DecodeJSON(t, resp, &apiComments)
assert.Len(t, apiComments, 2)
for _, apiComment := range apiComments { for _, apiComment := range apiComments {
c := &models.Comment{ID: apiComment.ID} c := &models.Comment{ID: apiComment.ID}
models.AssertExistsAndLoadBean(t, c, models.AssertExistsAndLoadBean(t, c,
models.Cond("type = ?", models.CommentTypeComment)) models.Cond("type = ?", models.CommentTypeComment))
models.AssertExistsAndLoadBean(t, &models.Issue{ID: c.IssueID, RepoID: repo.ID}) 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) { func TestAPIListIssueComments(t *testing.T) {

@ -13,6 +13,7 @@
issue_id: 1 # in repo_id 1 issue_id: 1 # in repo_id 1
content: "good work!" content: "good work!"
created_unix: 946684811 created_unix: 946684811
updated_unix: 946684811
- -
id: 3 id: 3
type: 0 # comment type: 0 # comment
@ -20,6 +21,7 @@
issue_id: 1 # in repo_id 1 issue_id: 1 # in repo_id 1
content: "meh..." content: "meh..."
created_unix: 946684812 created_unix: 946684812
updated_unix: 946684812
- -
id: 4 id: 4
type: 21 # code comment type: 21 # code comment
@ -63,4 +65,4 @@
review_id: 10 review_id: 10
tree_path: "README.md" tree_path: "README.md"
created_unix: 946684812 created_unix: 946684812
invalidated: true invalidated: true

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

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

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

Loading…
Cancel
Save