summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2020-01-13 17:02:24 +0100
committerAntoine GIRARD <sapk@users.noreply.github.com>2020-01-13 17:02:24 +0100
commit0b3aaa61964faa85b8008b04487388cc362ab436 (patch)
treecc6a3eda4780b7c8aa2d9e108a8a0ec393fbbb83
parentb7ffc6a096bdb231d95ecbb5db878e3d8b2b63c9 (diff)
downloadgitea-0b3aaa61964faa85b8008b04487388cc362ab436.tar.gz
gitea-0b3aaa61964faa85b8008b04487388cc362ab436.zip
[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>
-rw-r--r--integrations/api_comment_test.go27
-rw-r--r--models/fixtures/comment.yml4
-rw-r--r--models/issue_comment.go4
-rw-r--r--routers/api/v1/repo/issue_comment.go36
-rw-r--r--templates/swagger/v1_json.tmpl16
5 files changed, 73 insertions, 14 deletions
diff --git a/integrations/api_comment_test.go b/integrations/api_comment_test.go
index d21f56aaf8..2c754272e5 100644
--- a/integrations/api_comment_test.go
+++ b/integrations/api_comment_test.go
@@ -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) {
diff --git a/models/fixtures/comment.yml b/models/fixtures/comment.yml
index 864313cac3..bd64680c8c 100644
--- a/models/fixtures/comment.yml
+++ b/models/fixtures/comment.yml
@@ -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
@@ -63,4 +65,4 @@
review_id: 10
tree_path: "README.md"
created_unix: 946684812
- invalidated: true \ No newline at end of file
+ invalidated: true
diff --git a/models/issue_comment.go b/models/issue_comment.go
index 9caab1dc45..8f54d9656a 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -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})
}
diff --git a/routers/api/v1/repo/issue_comment.go b/routers/api/v1/repo/issue_comment.go
index 3085c2e51f..8c97936204 100644
--- a/routers/api/v1/repo/issue_comment.go
+++ b/routers/api/v1/repo/issue_comment.go
@@ -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 {
diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl
index de774de9fb..a2baac1364 100644
--- a/templates/swagger/v1_json.tmpl
+++ b/templates/swagger/v1_json.tmpl
@@ -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": {