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.

356 lines
8.9 KiB

8 years ago
  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}/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. comment.Content = form.Body
  246. if err := models.UpdateComment(comment); err != nil {
  247. ctx.Error(500, "UpdateComment", err)
  248. return
  249. }
  250. ctx.JSON(200, comment.APIFormat())
  251. }
  252. // DeleteIssueComment delete a comment from an issue
  253. func DeleteIssueComment(ctx *context.APIContext) {
  254. // swagger:operation DELETE /repos/{owner}/{repo}/issues/comments/{id} issue issueDeleteComment
  255. // ---
  256. // summary: Delete a comment
  257. // parameters:
  258. // - name: owner
  259. // in: path
  260. // description: owner of the repo
  261. // type: string
  262. // required: true
  263. // - name: repo
  264. // in: path
  265. // description: name of the repo
  266. // type: string
  267. // required: true
  268. // - name: id
  269. // in: path
  270. // description: id of comment to delete
  271. // type: integer
  272. // required: true
  273. // responses:
  274. // "204":
  275. // "$ref": "#/responses/empty"
  276. deleteIssueComment(ctx)
  277. }
  278. // DeleteIssueCommentDeprecated delete a comment from an issue
  279. func DeleteIssueCommentDeprecated(ctx *context.APIContext) {
  280. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/comments/{id} issue issueDeleteCommentDeprecated
  281. // ---
  282. // summary: Delete a comment
  283. // deprecated: true
  284. // parameters:
  285. // - name: owner
  286. // in: path
  287. // description: owner of the repo
  288. // type: string
  289. // required: true
  290. // - name: repo
  291. // in: path
  292. // description: name of the repo
  293. // type: string
  294. // required: true
  295. // - name: index
  296. // in: path
  297. // description: this parameter is ignored
  298. // type: integer
  299. // required: true
  300. // - name: id
  301. // in: path
  302. // description: id of comment to delete
  303. // type: integer
  304. // required: true
  305. // responses:
  306. // "204":
  307. // "$ref": "#/responses/empty"
  308. deleteIssueComment(ctx)
  309. }
  310. func deleteIssueComment(ctx *context.APIContext) {
  311. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  312. if err != nil {
  313. if models.IsErrCommentNotExist(err) {
  314. ctx.Error(404, "GetCommentByID", err)
  315. } else {
  316. ctx.Error(500, "GetCommentByID", err)
  317. }
  318. return
  319. }
  320. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  321. ctx.Status(403)
  322. return
  323. } else if comment.Type != models.CommentTypeComment {
  324. ctx.Status(204)
  325. return
  326. }
  327. if err = models.DeleteComment(comment); err != nil {
  328. ctx.Error(500, "DeleteCommentByID", err)
  329. return
  330. }
  331. ctx.Status(204)
  332. }