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

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