Skip to content

Commit e88d67b

Browse files
6543lunny
authored andcommitted
[API] add comments endpoint for single comment (#9494)
* add GET /repos/{owner}/{repo}/issues/comments/{id} and complete error list for swagger in other func * add repo check
1 parent c884735 commit e88d67b

File tree

4 files changed

+181
-3
lines changed

4 files changed

+181
-3
lines changed

integrations/api_comment_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,33 @@ func TestAPICreateComment(t *testing.T) {
8383
models.AssertExistsAndLoadBean(t, &models.Comment{ID: updatedComment.ID, IssueID: issue.ID, Content: commentBody})
8484
}
8585

86+
func TestAPIGetComment(t *testing.T) {
87+
defer prepareTestEnv(t)()
88+
89+
comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2}).(*models.Comment)
90+
assert.NoError(t, comment.LoadIssue())
91+
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: comment.Issue.RepoID}).(*models.Repository)
92+
repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
93+
94+
session := loginUser(t, repoOwner.Name)
95+
token := getTokenForLoggedInUser(t, session)
96+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID)
97+
resp := session.MakeRequest(t, req, http.StatusOK)
98+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d?token=%s", repoOwner.Name, repo.Name, comment.ID, token)
99+
resp = session.MakeRequest(t, req, http.StatusOK)
100+
101+
var apiComment api.Comment
102+
DecodeJSON(t, resp, &apiComment)
103+
104+
assert.NoError(t, comment.LoadPoster())
105+
expect := comment.APIFormat()
106+
107+
assert.Equal(t, expect.ID, apiComment.ID)
108+
assert.Equal(t, expect.Poster.FullName, apiComment.Poster.FullName)
109+
assert.Equal(t, expect.Body, apiComment.Body)
110+
assert.Equal(t, expect.Created.Unix(), apiComment.Created.Unix())
111+
}
112+
86113
func TestAPIEditComment(t *testing.T) {
87114
defer prepareTestEnv(t)()
88115
const newCommentBody = "This is the new comment body"

routers/api/v1/api.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,10 @@ func RegisterRoutes(m *macaron.Macaron) {
661661
m.Group("/comments", func() {
662662
m.Get("", repo.ListRepoIssueComments)
663663
m.Group("/:id", func() {
664-
m.Combo("", reqToken()).
665-
Patch(mustNotBeArchived, bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
666-
Delete(repo.DeleteIssueComment)
664+
m.Combo("").
665+
Get(repo.GetIssueComment).
666+
Patch(mustNotBeArchived, reqToken(), bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
667+
Delete(reqToken(), repo.DeleteIssueComment)
667668
m.Combo("/reactions").
668669
Get(repo.GetIssueCommentReactions).
669670
Post(bind(api.EditReactionOption{}), reqToken(), repo.PostIssueCommentReaction).

routers/api/v1/repo/issue_comment.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,74 @@ func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOpti
204204
ctx.JSON(http.StatusCreated, comment.APIFormat())
205205
}
206206

207+
// GetIssueComment Get a comment by ID
208+
func GetIssueComment(ctx *context.APIContext) {
209+
// swagger:operation GET /repos/{owner}/{repo}/issues/comments/{id} issue issueGetComment
210+
// ---
211+
// summary: Get a comment
212+
// consumes:
213+
// - application/json
214+
// produces:
215+
// - application/json
216+
// parameters:
217+
// - name: owner
218+
// in: path
219+
// description: owner of the repo
220+
// type: string
221+
// required: true
222+
// - name: repo
223+
// in: path
224+
// description: name of the repo
225+
// type: string
226+
// required: true
227+
// - name: id
228+
// in: path
229+
// description: id of the comment
230+
// type: integer
231+
// format: int64
232+
// required: true
233+
// responses:
234+
// "200":
235+
// "$ref": "#/responses/Comment"
236+
// "204":
237+
// "$ref": "#/responses/empty"
238+
// "403":
239+
// "$ref": "#/responses/forbidden"
240+
// "404":
241+
// "$ref": "#/responses/notFound"
242+
243+
comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
244+
if err != nil {
245+
if models.IsErrCommentNotExist(err) {
246+
ctx.NotFound(err)
247+
} else {
248+
ctx.Error(http.StatusInternalServerError, "GetCommentByID", err)
249+
}
250+
return
251+
}
252+
253+
if err = comment.LoadIssue(); err != nil {
254+
ctx.InternalServerError(err)
255+
return
256+
}
257+
if comment.Issue.RepoID != ctx.Repo.Repository.ID {
258+
ctx.Status(http.StatusNotFound)
259+
return
260+
}
261+
262+
if comment.Type != models.CommentTypeComment {
263+
ctx.Status(http.StatusNoContent)
264+
return
265+
}
266+
267+
if err := comment.LoadPoster(); err != nil {
268+
ctx.Error(http.StatusInternalServerError, "comment.LoadPoster", err)
269+
return
270+
}
271+
272+
ctx.JSON(http.StatusOK, comment.APIFormat())
273+
}
274+
207275
// EditIssueComment modify a comment of an issue
208276
func EditIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) {
209277
// swagger:operation PATCH /repos/{owner}/{repo}/issues/comments/{id} issue issueEditComment
@@ -237,6 +305,13 @@ func EditIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
237305
// responses:
238306
// "200":
239307
// "$ref": "#/responses/Comment"
308+
// "204":
309+
// "$ref": "#/responses/empty"
310+
// "403":
311+
// "$ref": "#/responses/forbidden"
312+
// "404":
313+
// "$ref": "#/responses/notFound"
314+
240315
editIssueComment(ctx, form)
241316
}
242317

@@ -283,6 +358,8 @@ func EditIssueCommentDeprecated(ctx *context.APIContext, form api.EditIssueComme
283358
// "$ref": "#/responses/empty"
284359
// "403":
285360
// "$ref": "#/responses/forbidden"
361+
// "404":
362+
// "$ref": "#/responses/notFound"
286363

287364
editIssueComment(ctx, form)
288365
}
@@ -343,6 +420,8 @@ func DeleteIssueComment(ctx *context.APIContext) {
343420
// "$ref": "#/responses/empty"
344421
// "403":
345422
// "$ref": "#/responses/forbidden"
423+
// "404":
424+
// "$ref": "#/responses/notFound"
346425

347426
deleteIssueComment(ctx)
348427
}
@@ -380,6 +459,8 @@ func DeleteIssueCommentDeprecated(ctx *context.APIContext) {
380459
// "$ref": "#/responses/empty"
381460
// "403":
382461
// "$ref": "#/responses/forbidden"
462+
// "404":
463+
// "$ref": "#/responses/notFound"
383464

384465
deleteIssueComment(ctx)
385466
}

templates/swagger/v1_json.tmpl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,6 +3002,57 @@
30023002
}
30033003
},
30043004
"/repos/{owner}/{repo}/issues/comments/{id}": {
3005+
"get": {
3006+
"consumes": [
3007+
"application/json"
3008+
],
3009+
"produces": [
3010+
"application/json"
3011+
],
3012+
"tags": [
3013+
"issue"
3014+
],
3015+
"summary": "Get a comment",
3016+
"operationId": "issueGetComment",
3017+
"parameters": [
3018+
{
3019+
"type": "string",
3020+
"description": "owner of the repo",
3021+
"name": "owner",
3022+
"in": "path",
3023+
"required": true
3024+
},
3025+
{
3026+
"type": "string",
3027+
"description": "name of the repo",
3028+
"name": "repo",
3029+
"in": "path",
3030+
"required": true
3031+
},
3032+
{
3033+
"type": "integer",
3034+
"format": "int64",
3035+
"description": "id of the comment",
3036+
"name": "id",
3037+
"in": "path",
3038+
"required": true
3039+
}
3040+
],
3041+
"responses": {
3042+
"200": {
3043+
"$ref": "#/responses/Comment"
3044+
},
3045+
"204": {
3046+
"$ref": "#/responses/empty"
3047+
},
3048+
"403": {
3049+
"$ref": "#/responses/forbidden"
3050+
},
3051+
"404": {
3052+
"$ref": "#/responses/notFound"
3053+
}
3054+
}
3055+
},
30053056
"delete": {
30063057
"tags": [
30073058
"issue"
@@ -3038,6 +3089,9 @@
30383089
},
30393090
"403": {
30403091
"$ref": "#/responses/forbidden"
3092+
},
3093+
"404": {
3094+
"$ref": "#/responses/notFound"
30413095
}
30423096
}
30433097
},
@@ -3087,6 +3141,15 @@
30873141
"responses": {
30883142
"200": {
30893143
"$ref": "#/responses/Comment"
3144+
},
3145+
"204": {
3146+
"$ref": "#/responses/empty"
3147+
},
3148+
"403": {
3149+
"$ref": "#/responses/forbidden"
3150+
},
3151+
"404": {
3152+
"$ref": "#/responses/notFound"
30903153
}
30913154
}
30923155
}
@@ -3491,6 +3554,9 @@
34913554
},
34923555
"403": {
34933556
"$ref": "#/responses/forbidden"
3557+
},
3558+
"404": {
3559+
"$ref": "#/responses/notFound"
34943560
}
34953561
}
34963562
},
@@ -3554,6 +3620,9 @@
35543620
},
35553621
"403": {
35563622
"$ref": "#/responses/forbidden"
3623+
},
3624+
"404": {
3625+
"$ref": "#/responses/notFound"
35573626
}
35583627
}
35593628
}

0 commit comments

Comments
 (0)