Skip to content

Commit 0b3aaa6

Browse files
6543lunnysapk
committed
[API] Add "before" query to ListIssueComments and ListRepoIssue… (#9685)
* add "before" query to ListIssueComments and ListRepoIssueComments * Add TEST Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: Antoine GIRARD <[email protected]>
1 parent b7ffc6a commit 0b3aaa6

File tree

5 files changed

+73
-14
lines changed

5 files changed

+73
-14
lines changed

integrations/api_comment_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package integrations
77
import (
88
"fmt"
99
"net/http"
10+
"net/url"
1011
"testing"
1112

1213
"code.gitea.io/gitea/models"
@@ -25,18 +26,40 @@ func TestAPIListRepoComments(t *testing.T) {
2526
repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
2627

2728
session := loginUser(t, repoOwner.Name)
28-
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments",
29-
repoOwner.Name, repo.Name)
29+
link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments", repoOwner.Name, repo.Name))
30+
req := NewRequest(t, "GET", link.String())
3031
resp := session.MakeRequest(t, req, http.StatusOK)
3132

3233
var apiComments []*api.Comment
3334
DecodeJSON(t, resp, &apiComments)
35+
assert.Len(t, apiComments, 2)
3436
for _, apiComment := range apiComments {
3537
c := &models.Comment{ID: apiComment.ID}
3638
models.AssertExistsAndLoadBean(t, c,
3739
models.Cond("type = ?", models.CommentTypeComment))
3840
models.AssertExistsAndLoadBean(t, &models.Issue{ID: c.IssueID, RepoID: repo.ID})
3941
}
42+
43+
//test before and since filters
44+
query := url.Values{}
45+
before := "2000-01-01T00:00:11+00:00" //unix: 946684811
46+
since := "2000-01-01T00:00:12+00:00" //unix: 946684812
47+
query.Add("before", before)
48+
link.RawQuery = query.Encode()
49+
req = NewRequest(t, "GET", link.String())
50+
resp = session.MakeRequest(t, req, http.StatusOK)
51+
DecodeJSON(t, resp, &apiComments)
52+
assert.Len(t, apiComments, 1)
53+
assert.EqualValues(t, 2, apiComments[0].ID)
54+
55+
query.Del("before")
56+
query.Add("since", since)
57+
link.RawQuery = query.Encode()
58+
req = NewRequest(t, "GET", link.String())
59+
resp = session.MakeRequest(t, req, http.StatusOK)
60+
DecodeJSON(t, resp, &apiComments)
61+
assert.Len(t, apiComments, 1)
62+
assert.EqualValues(t, 3, apiComments[0].ID)
4063
}
4164

4265
func TestAPIListIssueComments(t *testing.T) {

models/fixtures/comment.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
issue_id: 1 # in repo_id 1
1414
content: "good work!"
1515
created_unix: 946684811
16+
updated_unix: 946684811
1617
-
1718
id: 3
1819
type: 0 # comment
1920
poster_id: 5 # user not watching (see watch.yml)
2021
issue_id: 1 # in repo_id 1
2122
content: "meh..."
2223
created_unix: 946684812
24+
updated_unix: 946684812
2325
-
2426
id: 4
2527
type: 21 # code comment
@@ -63,4 +65,4 @@
6365
review_id: 10
6466
tree_path: "README.md"
6567
created_unix: 946684812
66-
invalidated: true
68+
invalidated: true

models/issue_comment.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ type FindCommentsOptions struct {
782782
IssueID int64
783783
ReviewID int64
784784
Since int64
785+
Before int64
785786
Type CommentType
786787
}
787788

@@ -799,6 +800,9 @@ func (opts *FindCommentsOptions) toConds() builder.Cond {
799800
if opts.Since > 0 {
800801
cond = cond.And(builder.Gte{"comment.updated_unix": opts.Since})
801802
}
803+
if opts.Before > 0 {
804+
cond = cond.And(builder.Lte{"comment.updated_unix": opts.Before})
805+
}
802806
if opts.Type != CommentTypeUnknown {
803807
cond = cond.And(builder.Eq{"comment.type": opts.Type})
804808
}

routers/api/v1/repo/issue_comment.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ package repo
77
import (
88
"errors"
99
"net/http"
10-
"time"
1110

1211
"code.gitea.io/gitea/models"
1312
"code.gitea.io/gitea/modules/context"
1413
api "code.gitea.io/gitea/modules/structs"
14+
"code.gitea.io/gitea/routers/api/v1/utils"
1515
comment_service "code.gitea.io/gitea/services/comments"
1616
)
1717

@@ -43,16 +43,21 @@ func ListIssueComments(ctx *context.APIContext) {
4343
// in: query
4444
// description: if provided, only comments updated since the specified time are returned.
4545
// type: string
46+
// format: date-time
47+
// - name: before
48+
// in: query
49+
// description: if provided, only comments updated before the provided time are returned.
50+
// type: string
51+
// format: date-time
4652
// responses:
4753
// "200":
4854
// "$ref": "#/responses/CommentList"
4955

50-
var since time.Time
51-
if len(ctx.Query("since")) > 0 {
52-
since, _ = time.Parse(time.RFC3339, ctx.Query("since"))
56+
before, since, err := utils.GetQueryBeforeSince(ctx)
57+
if err != nil {
58+
ctx.Error(http.StatusInternalServerError, "GetQueryBeforeSince", err)
59+
return
5360
}
54-
55-
// comments,err:=models.GetCommentsByIssueIDSince(, since)
5661
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
5762
if err != nil {
5863
ctx.Error(http.StatusInternalServerError, "GetRawIssueByIndex", err)
@@ -62,7 +67,8 @@ func ListIssueComments(ctx *context.APIContext) {
6267

6368
comments, err := models.FindComments(models.FindCommentsOptions{
6469
IssueID: issue.ID,
65-
Since: since.Unix(),
70+
Since: since,
71+
Before: before,
6672
Type: models.CommentTypeComment,
6773
})
6874
if err != nil {
@@ -105,18 +111,26 @@ func ListRepoIssueComments(ctx *context.APIContext) {
105111
// in: query
106112
// description: if provided, only comments updated since the provided time are returned.
107113
// type: string
114+
// format: date-time
115+
// - name: before
116+
// in: query
117+
// description: if provided, only comments updated before the provided time are returned.
118+
// type: string
119+
// format: date-time
108120
// responses:
109121
// "200":
110122
// "$ref": "#/responses/CommentList"
111123

112-
var since time.Time
113-
if len(ctx.Query("since")) > 0 {
114-
since, _ = time.Parse(time.RFC3339, ctx.Query("since"))
124+
before, since, err := utils.GetQueryBeforeSince(ctx)
125+
if err != nil {
126+
ctx.Error(http.StatusInternalServerError, "GetQueryBeforeSince", err)
127+
return
115128
}
116129

117130
comments, err := models.FindComments(models.FindCommentsOptions{
118131
RepoID: ctx.Repo.Repository.ID,
119-
Since: since.Unix(),
132+
Since: since,
133+
Before: before,
120134
Type: models.CommentTypeComment,
121135
})
122136
if err != nil {

templates/swagger/v1_json.tmpl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3196,9 +3196,17 @@
31963196
},
31973197
{
31983198
"type": "string",
3199+
"format": "date-time",
31993200
"description": "if provided, only comments updated since the provided time are returned.",
32003201
"name": "since",
32013202
"in": "query"
3203+
},
3204+
{
3205+
"type": "string",
3206+
"format": "date-time",
3207+
"description": "if provided, only comments updated before the provided time are returned.",
3208+
"name": "before",
3209+
"in": "query"
32023210
}
32033211
],
32043212
"responses": {
@@ -3652,9 +3660,17 @@
36523660
},
36533661
{
36543662
"type": "string",
3663+
"format": "date-time",
36553664
"description": "if provided, only comments updated since the specified time are returned.",
36563665
"name": "since",
36573666
"in": "query"
3667+
},
3668+
{
3669+
"type": "string",
3670+
"format": "date-time",
3671+
"description": "if provided, only comments updated before the provided time are returned.",
3672+
"name": "before",
3673+
"in": "query"
36583674
}
36593675
],
36603676
"responses": {

0 commit comments

Comments
 (0)