diff --git a/integrations/api_comment_test.go b/integrations/api_comment_test.go index d21f56aaf..2c754272e 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 864313cac..bd64680c8 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 9caab1dc4..8f54d9656 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 3085c2e51..8c9793620 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 de774de9f..a2baac136 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": {