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 9.5 kB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. "code.gitea.io/gitea/modules/notification"
  10. api "code.gitea.io/sdk/gitea"
  11. )
  12. // ListIssueComments list all the comments of an issue
  13. func ListIssueComments(ctx *context.APIContext) {
  14. // swagger:operation GET /repos/{owner}/{repo}/issues/{index}/comments issue issueGetComments
  15. // ---
  16. // summary: List all comments on an issue
  17. // produces:
  18. // - application/json
  19. // parameters:
  20. // - name: owner
  21. // in: path
  22. // description: owner of the repo
  23. // type: string
  24. // required: true
  25. // - name: repo
  26. // in: path
  27. // description: name of the repo
  28. // type: string
  29. // required: true
  30. // - name: index
  31. // in: path
  32. // description: index of the issue
  33. // type: integer
  34. // format: int64
  35. // required: true
  36. // - name: since
  37. // in: query
  38. // description: if provided, only comments updated since the specified time are returned.
  39. // type: string
  40. // responses:
  41. // "200":
  42. // "$ref": "#/responses/CommentList"
  43. var since time.Time
  44. if len(ctx.Query("since")) > 0 {
  45. since, _ = time.Parse(time.RFC3339, ctx.Query("since"))
  46. }
  47. // comments,err:=models.GetCommentsByIssueIDSince(, since)
  48. issue, err := models.GetRawIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  49. if err != nil {
  50. ctx.Error(500, "GetRawIssueByIndex", err)
  51. return
  52. }
  53. comments, err := models.FindComments(models.FindCommentsOptions{
  54. IssueID: issue.ID,
  55. Since: since.Unix(),
  56. Type: models.CommentTypeComment,
  57. })
  58. if err != nil {
  59. ctx.Error(500, "GetCommentsByIssueIDSince", err)
  60. return
  61. }
  62. apiComments := make([]*api.Comment, len(comments))
  63. for i := range comments {
  64. apiComments[i] = comments[i].APIFormat()
  65. }
  66. ctx.JSON(200, &apiComments)
  67. }
  68. // ListRepoIssueComments returns all issue-comments for a repo
  69. func ListRepoIssueComments(ctx *context.APIContext) {
  70. // swagger:operation GET /repos/{owner}/{repo}/issues/comments issue issueGetRepoComments
  71. // ---
  72. // summary: List all comments in a repository
  73. // produces:
  74. // - application/json
  75. // parameters:
  76. // - name: owner
  77. // in: path
  78. // description: owner of the repo
  79. // type: string
  80. // required: true
  81. // - name: repo
  82. // in: path
  83. // description: name of the repo
  84. // type: string
  85. // required: true
  86. // - name: since
  87. // in: query
  88. // description: if provided, only comments updated since the provided time are returned.
  89. // type: string
  90. // responses:
  91. // "200":
  92. // "$ref": "#/responses/CommentList"
  93. var since time.Time
  94. if len(ctx.Query("since")) > 0 {
  95. since, _ = time.Parse(time.RFC3339, ctx.Query("since"))
  96. }
  97. comments, err := models.FindComments(models.FindCommentsOptions{
  98. RepoID: ctx.Repo.Repository.ID,
  99. Since: since.Unix(),
  100. Type: models.CommentTypeComment,
  101. })
  102. if err != nil {
  103. ctx.Error(500, "GetCommentsByRepoIDSince", err)
  104. return
  105. }
  106. apiComments := make([]*api.Comment, len(comments))
  107. for i := range comments {
  108. apiComments[i] = comments[i].APIFormat()
  109. }
  110. ctx.JSON(200, &apiComments)
  111. }
  112. // CreateIssueComment create a comment for an issue
  113. func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOption) {
  114. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/comments issue issueCreateComment
  115. // ---
  116. // summary: Add a comment to an issue
  117. // consumes:
  118. // - application/json
  119. // produces:
  120. // - application/json
  121. // parameters:
  122. // - name: owner
  123. // in: path
  124. // description: owner of the repo
  125. // type: string
  126. // required: true
  127. // - name: repo
  128. // in: path
  129. // description: name of the repo
  130. // type: string
  131. // required: true
  132. // - name: index
  133. // in: path
  134. // description: index of the issue
  135. // type: integer
  136. // format: int64
  137. // required: true
  138. // - name: body
  139. // in: body
  140. // schema:
  141. // "$ref": "#/definitions/CreateIssueCommentOption"
  142. // responses:
  143. // "201":
  144. // "$ref": "#/responses/Comment"
  145. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  146. if err != nil {
  147. ctx.Error(500, "GetIssueByIndex", err)
  148. return
  149. }
  150. comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Body, nil)
  151. if err != nil {
  152. ctx.Error(500, "CreateIssueComment", err)
  153. return
  154. }
  155. notification.NotifyCreateIssueComment(ctx.User, ctx.Repo.Repository, issue, comment)
  156. ctx.JSON(201, comment.APIFormat())
  157. }
  158. // EditIssueComment modify a comment of an issue
  159. func EditIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) {
  160. // swagger:operation PATCH /repos/{owner}/{repo}/issues/comments/{id} issue issueEditComment
  161. // ---
  162. // summary: Edit a comment
  163. // consumes:
  164. // - application/json
  165. // produces:
  166. // - application/json
  167. // parameters:
  168. // - name: owner
  169. // in: path
  170. // description: owner of the repo
  171. // type: string
  172. // required: true
  173. // - name: repo
  174. // in: path
  175. // description: name of the repo
  176. // type: string
  177. // required: true
  178. // - name: id
  179. // in: path
  180. // description: id of the comment to edit
  181. // type: integer
  182. // format: int64
  183. // required: true
  184. // - name: body
  185. // in: body
  186. // schema:
  187. // "$ref": "#/definitions/EditIssueCommentOption"
  188. // responses:
  189. // "200":
  190. // "$ref": "#/responses/Comment"
  191. editIssueComment(ctx, form)
  192. }
  193. // EditIssueCommentDeprecated modify a comment of an issue
  194. func EditIssueCommentDeprecated(ctx *context.APIContext, form api.EditIssueCommentOption) {
  195. // swagger:operation PATCH /repos/{owner}/{repo}/issues/{index}/comments/{id} issue issueEditCommentDeprecated
  196. // ---
  197. // summary: Edit a comment
  198. // deprecated: true
  199. // consumes:
  200. // - application/json
  201. // produces:
  202. // - application/json
  203. // parameters:
  204. // - name: owner
  205. // in: path
  206. // description: owner of the repo
  207. // type: string
  208. // required: true
  209. // - name: repo
  210. // in: path
  211. // description: name of the repo
  212. // type: string
  213. // required: true
  214. // - name: index
  215. // in: path
  216. // description: this parameter is ignored
  217. // type: integer
  218. // required: true
  219. // - name: id
  220. // in: path
  221. // description: id of the comment to edit
  222. // type: integer
  223. // format: int64
  224. // required: true
  225. // - name: body
  226. // in: body
  227. // schema:
  228. // "$ref": "#/definitions/EditIssueCommentOption"
  229. // responses:
  230. // "200":
  231. // "$ref": "#/responses/Comment"
  232. editIssueComment(ctx, form)
  233. }
  234. func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) {
  235. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  236. if err != nil {
  237. if models.IsErrCommentNotExist(err) {
  238. ctx.Error(404, "GetCommentByID", err)
  239. } else {
  240. ctx.Error(500, "GetCommentByID", err)
  241. }
  242. return
  243. }
  244. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  245. ctx.Status(403)
  246. return
  247. } else if comment.Type != models.CommentTypeComment {
  248. ctx.Status(204)
  249. return
  250. }
  251. oldContent := comment.Content
  252. comment.Content = form.Body
  253. if err := models.UpdateComment(ctx.User, comment, oldContent); err != nil {
  254. ctx.Error(500, "UpdateComment", err)
  255. return
  256. }
  257. ctx.JSON(200, comment.APIFormat())
  258. }
  259. // DeleteIssueComment delete a comment from an issue
  260. func DeleteIssueComment(ctx *context.APIContext) {
  261. // swagger:operation DELETE /repos/{owner}/{repo}/issues/comments/{id} issue issueDeleteComment
  262. // ---
  263. // summary: Delete a comment
  264. // parameters:
  265. // - name: owner
  266. // in: path
  267. // description: owner of the repo
  268. // type: string
  269. // required: true
  270. // - name: repo
  271. // in: path
  272. // description: name of the repo
  273. // type: string
  274. // required: true
  275. // - name: id
  276. // in: path
  277. // description: id of comment to delete
  278. // type: integer
  279. // format: int64
  280. // required: true
  281. // responses:
  282. // "204":
  283. // "$ref": "#/responses/empty"
  284. deleteIssueComment(ctx)
  285. }
  286. // DeleteIssueCommentDeprecated delete a comment from an issue
  287. func DeleteIssueCommentDeprecated(ctx *context.APIContext) {
  288. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/comments/{id} issue issueDeleteCommentDeprecated
  289. // ---
  290. // summary: Delete a comment
  291. // deprecated: true
  292. // parameters:
  293. // - name: owner
  294. // in: path
  295. // description: owner of the repo
  296. // type: string
  297. // required: true
  298. // - name: repo
  299. // in: path
  300. // description: name of the repo
  301. // type: string
  302. // required: true
  303. // - name: index
  304. // in: path
  305. // description: this parameter is ignored
  306. // type: integer
  307. // required: true
  308. // - name: id
  309. // in: path
  310. // description: id of comment to delete
  311. // type: integer
  312. // format: int64
  313. // required: true
  314. // responses:
  315. // "204":
  316. // "$ref": "#/responses/empty"
  317. deleteIssueComment(ctx)
  318. }
  319. func deleteIssueComment(ctx *context.APIContext) {
  320. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  321. if err != nil {
  322. if models.IsErrCommentNotExist(err) {
  323. ctx.Error(404, "GetCommentByID", err)
  324. } else {
  325. ctx.Error(500, "GetCommentByID", err)
  326. }
  327. return
  328. }
  329. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  330. ctx.Status(403)
  331. return
  332. } else if comment.Type != models.CommentTypeComment {
  333. ctx.Status(204)
  334. return
  335. }
  336. if err = models.DeleteComment(ctx.User, comment); err != nil {
  337. ctx.Error(500, "DeleteCommentByID", err)
  338. return
  339. }
  340. ctx.Status(204)
  341. }