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

issue_comment.go 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. api "code.gitea.io/sdk/gitea"
  10. )
  11. // ListIssueComments list all the comments of an issue
  12. func ListIssueComments(ctx *context.APIContext) {
  13. // swagger:operation GET /repos/{owner}/{repo}/issues/{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: index
  30. // in: path
  31. // description: index of the issue
  32. // type: integer
  33. // required: true
  34. // - name: since
  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: since
  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: index
  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/CreateIssueCommentOption"
  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}/issues/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. editIssueComment(ctx, form)
  187. }
  188. // EditIssueCommentDeprecated modify a comment of an issue
  189. func EditIssueCommentDeprecated(ctx *context.APIContext, form api.EditIssueCommentOption) {
  190. // swagger:operation PATCH /repos/{owner}/{repo}/issues/{index}/comments/{id} issue issueEditCommentDeprecated
  191. // ---
  192. // summary: Edit a comment
  193. // deprecated: true
  194. // consumes:
  195. // - application/json
  196. // produces:
  197. // - application/json
  198. // parameters:
  199. // - name: owner
  200. // in: path
  201. // description: owner of the repo
  202. // type: string
  203. // required: true
  204. // - name: repo
  205. // in: path
  206. // description: name of the repo
  207. // type: string
  208. // required: true
  209. // - name: index
  210. // in: path
  211. // description: this parameter is ignored
  212. // type: integer
  213. // required: true
  214. // - name: id
  215. // in: path
  216. // description: id of the comment to edit
  217. // type: integer
  218. // required: true
  219. // - name: body
  220. // in: body
  221. // schema:
  222. // "$ref": "#/definitions/EditIssueCommentOption"
  223. // responses:
  224. // "200":
  225. // "$ref": "#/responses/Comment"
  226. editIssueComment(ctx, form)
  227. }
  228. func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) {
  229. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  230. if err != nil {
  231. if models.IsErrCommentNotExist(err) {
  232. ctx.Error(404, "GetCommentByID", err)
  233. } else {
  234. ctx.Error(500, "GetCommentByID", err)
  235. }
  236. return
  237. }
  238. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  239. ctx.Status(403)
  240. return
  241. } else if comment.Type != models.CommentTypeComment {
  242. ctx.Status(204)
  243. return
  244. }
  245. oldContent := comment.Content
  246. comment.Content = form.Body
  247. if err := models.UpdateComment(ctx.User, comment, oldContent); err != nil {
  248. ctx.Error(500, "UpdateComment", err)
  249. return
  250. }
  251. ctx.JSON(200, comment.APIFormat())
  252. }
  253. // DeleteIssueComment delete a comment from an issue
  254. func DeleteIssueComment(ctx *context.APIContext) {
  255. // swagger:operation DELETE /repos/{owner}/{repo}/issues/comments/{id} issue issueDeleteComment
  256. // ---
  257. // summary: Delete a comment
  258. // parameters:
  259. // - name: owner
  260. // in: path
  261. // description: owner of the repo
  262. // type: string
  263. // required: true
  264. // - name: repo
  265. // in: path
  266. // description: name of the repo
  267. // type: string
  268. // required: true
  269. // - name: id
  270. // in: path
  271. // description: id of comment to delete
  272. // type: integer
  273. // required: true
  274. // responses:
  275. // "204":
  276. // "$ref": "#/responses/empty"
  277. deleteIssueComment(ctx)
  278. }
  279. // DeleteIssueCommentDeprecated delete a comment from an issue
  280. func DeleteIssueCommentDeprecated(ctx *context.APIContext) {
  281. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/comments/{id} issue issueDeleteCommentDeprecated
  282. // ---
  283. // summary: Delete a comment
  284. // deprecated: true
  285. // parameters:
  286. // - name: owner
  287. // in: path
  288. // description: owner of the repo
  289. // type: string
  290. // required: true
  291. // - name: repo
  292. // in: path
  293. // description: name of the repo
  294. // type: string
  295. // required: true
  296. // - name: index
  297. // in: path
  298. // description: this parameter is ignored
  299. // type: integer
  300. // required: true
  301. // - name: id
  302. // in: path
  303. // description: id of comment to delete
  304. // type: integer
  305. // required: true
  306. // responses:
  307. // "204":
  308. // "$ref": "#/responses/empty"
  309. deleteIssueComment(ctx)
  310. }
  311. func deleteIssueComment(ctx *context.APIContext) {
  312. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  313. if err != nil {
  314. if models.IsErrCommentNotExist(err) {
  315. ctx.Error(404, "GetCommentByID", err)
  316. } else {
  317. ctx.Error(500, "GetCommentByID", err)
  318. }
  319. return
  320. }
  321. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  322. ctx.Status(403)
  323. return
  324. } else if comment.Type != models.CommentTypeComment {
  325. ctx.Status(204)
  326. return
  327. }
  328. if err = models.DeleteComment(ctx.User, comment); err != nil {
  329. ctx.Error(500, "DeleteCommentByID", err)
  330. return
  331. }
  332. ctx.Status(204)
  333. }