You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_comment.go 7.0 kB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "time"
  7. api "code.gitea.io/sdk/gitea"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. )
  11. // ListIssueComments list all the comments of an issue
  12. func ListIssueComments(ctx *context.APIContext) {
  13. // swagger:operation GET /repos/{owner}/{repo}/issue/{index}/comments issue issueGetComments
  14. // ---
  15. // summary: List all comments on an issue
  16. // produces:
  17. // - application/json
  18. // parameters:
  19. // - name: owner
  20. // in: path
  21. // description: owner of the repo
  22. // type: string
  23. // required: true
  24. // - name: repo
  25. // in: path
  26. // description: name of the repo
  27. // type: string
  28. // required: true
  29. // - name: id
  30. // in: path
  31. // description: index of the issue
  32. // type: integer
  33. // required: true
  34. // - name: string
  35. // in: query
  36. // description: if provided, only comments updated since the specified time are returned.
  37. // type: string
  38. // responses:
  39. // "200":
  40. // "$ref": "#/responses/CommentList"
  41. var since time.Time
  42. if len(ctx.Query("since")) > 0 {
  43. since, _ = time.Parse(time.RFC3339, ctx.Query("since"))
  44. }
  45. // comments,err:=models.GetCommentsByIssueIDSince(, since)
  46. issue, err := models.GetRawIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  47. if err != nil {
  48. ctx.Error(500, "GetRawIssueByIndex", err)
  49. return
  50. }
  51. comments, err := models.FindComments(models.FindCommentsOptions{
  52. IssueID: issue.ID,
  53. Since: since.Unix(),
  54. Type: models.CommentTypeComment,
  55. })
  56. if err != nil {
  57. ctx.Error(500, "GetCommentsByIssueIDSince", err)
  58. return
  59. }
  60. apiComments := make([]*api.Comment, len(comments))
  61. for i := range comments {
  62. apiComments[i] = comments[i].APIFormat()
  63. }
  64. ctx.JSON(200, &apiComments)
  65. }
  66. // ListRepoIssueComments returns all issue-comments for a repo
  67. func ListRepoIssueComments(ctx *context.APIContext) {
  68. // swagger:operation GET /repos/{owner}/{repo}/issues/comments issue issueGetRepoComments
  69. // ---
  70. // summary: List all comments in a repository
  71. // produces:
  72. // - application/json
  73. // parameters:
  74. // - name: owner
  75. // in: path
  76. // description: owner of the repo
  77. // type: string
  78. // required: true
  79. // - name: repo
  80. // in: path
  81. // description: name of the repo
  82. // type: string
  83. // required: true
  84. // - name: string
  85. // in: query
  86. // description: if provided, only comments updated since the provided time are returned.
  87. // type: string
  88. // responses:
  89. // "200":
  90. // "$ref": "#/responses/CommentList"
  91. var since time.Time
  92. if len(ctx.Query("since")) > 0 {
  93. since, _ = time.Parse(time.RFC3339, ctx.Query("since"))
  94. }
  95. comments, err := models.FindComments(models.FindCommentsOptions{
  96. RepoID: ctx.Repo.Repository.ID,
  97. Since: since.Unix(),
  98. Type: models.CommentTypeComment,
  99. })
  100. if err != nil {
  101. ctx.Error(500, "GetCommentsByRepoIDSince", err)
  102. return
  103. }
  104. apiComments := make([]*api.Comment, len(comments))
  105. for i := range comments {
  106. apiComments[i] = comments[i].APIFormat()
  107. }
  108. ctx.JSON(200, &apiComments)
  109. }
  110. // CreateIssueComment create a comment for an issue
  111. func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOption) {
  112. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/comments issue issueCreateComment
  113. // ---
  114. // summary: Add a comment to an issue
  115. // consumes:
  116. // - application/json
  117. // produces:
  118. // - application/json
  119. // parameters:
  120. // - name: owner
  121. // in: path
  122. // description: owner of the repo
  123. // type: string
  124. // required: true
  125. // - name: repo
  126. // in: path
  127. // description: name of the repo
  128. // type: string
  129. // required: true
  130. // - name: id
  131. // in: path
  132. // description: index of the issue
  133. // type: integer
  134. // required: true
  135. // - name: body
  136. // in: body
  137. // schema:
  138. // "$ref": "#/definitions/CreateIssueOption"
  139. // responses:
  140. // "201":
  141. // "$ref": "#/responses/Comment"
  142. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  143. if err != nil {
  144. ctx.Error(500, "GetIssueByIndex", err)
  145. return
  146. }
  147. comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Body, nil)
  148. if err != nil {
  149. ctx.Error(500, "CreateIssueComment", err)
  150. return
  151. }
  152. ctx.JSON(201, comment.APIFormat())
  153. }
  154. // EditIssueComment modify a comment of an issue
  155. func EditIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) {
  156. // swagger:operation PATCH /repos/{owner}/{repo}/comments/{id} issue issueEditComment
  157. // ---
  158. // summary: Edit a comment
  159. // consumes:
  160. // - application/json
  161. // produces:
  162. // - application/json
  163. // parameters:
  164. // - name: owner
  165. // in: path
  166. // description: owner of the repo
  167. // type: string
  168. // required: true
  169. // - name: repo
  170. // in: path
  171. // description: name of the repo
  172. // type: string
  173. // required: true
  174. // - name: id
  175. // in: path
  176. // description: id of the comment to edit
  177. // type: integer
  178. // required: true
  179. // - name: body
  180. // in: body
  181. // schema:
  182. // "$ref": "#/definitions/EditIssueCommentOption"
  183. // responses:
  184. // "200":
  185. // "$ref": "#/responses/Comment"
  186. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  187. if err != nil {
  188. if models.IsErrCommentNotExist(err) {
  189. ctx.Error(404, "GetCommentByID", err)
  190. } else {
  191. ctx.Error(500, "GetCommentByID", err)
  192. }
  193. return
  194. }
  195. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  196. ctx.Status(403)
  197. return
  198. } else if comment.Type != models.CommentTypeComment {
  199. ctx.Status(204)
  200. return
  201. }
  202. comment.Content = form.Body
  203. if err := models.UpdateComment(comment); err != nil {
  204. ctx.Error(500, "UpdateComment", err)
  205. return
  206. }
  207. ctx.JSON(200, comment.APIFormat())
  208. }
  209. // DeleteIssueComment delete a comment from an issue
  210. func DeleteIssueComment(ctx *context.APIContext) {
  211. // swagger:operation DELETE /repos/{owner}/{repo}/comments/{id} issue issueDeleteComment
  212. // ---
  213. // summary: Delete a comment
  214. // parameters:
  215. // - name: owner
  216. // in: path
  217. // description: owner of the repo
  218. // type: string
  219. // required: true
  220. // - name: repo
  221. // in: path
  222. // description: name of the repo
  223. // type: string
  224. // required: true
  225. // - name: id
  226. // in: path
  227. // description: id of comment to delete
  228. // type: integer
  229. // required: true
  230. // responses:
  231. // "204":
  232. // "$ref": "#/responses/empty"
  233. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  234. if err != nil {
  235. if models.IsErrCommentNotExist(err) {
  236. ctx.Error(404, "GetCommentByID", err)
  237. } else {
  238. ctx.Error(500, "GetCommentByID", err)
  239. }
  240. return
  241. }
  242. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  243. ctx.Status(403)
  244. return
  245. } else if comment.Type != models.CommentTypeComment {
  246. ctx.Status(204)
  247. return
  248. }
  249. if err = models.DeleteComment(comment); err != nil {
  250. ctx.Error(500, "DeleteCommentByID", err)
  251. return
  252. }
  253. ctx.Status(204)
  254. }