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

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. // GetIssueComment Get a comment by ID
  188. func GetIssueComment(ctx *context.APIContext) {
  189. // swagger:operation GET /repos/{owner}/{repo}/issues/comments/{id} issue issueGetComment
  190. // ---
  191. // summary: Get 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
  210. // type: integer
  211. // format: int64
  212. // required: true
  213. // responses:
  214. // "200":
  215. // "$ref": "#/responses/Comment"
  216. // "204":
  217. // "$ref": "#/responses/empty"
  218. // "403":
  219. // "$ref": "#/responses/forbidden"
  220. // "404":
  221. // "$ref": "#/responses/notFound"
  222. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  223. if err != nil {
  224. if models.IsErrCommentNotExist(err) {
  225. ctx.NotFound(err)
  226. } else {
  227. ctx.Error(http.StatusInternalServerError, "GetCommentByID", err)
  228. }
  229. return
  230. }
  231. if err = comment.LoadIssue(); err != nil {
  232. ctx.InternalServerError(err)
  233. return
  234. }
  235. if comment.Issue.RepoID != ctx.Repo.Repository.ID {
  236. ctx.Status(http.StatusNotFound)
  237. return
  238. }
  239. if comment.Type != models.CommentTypeComment {
  240. ctx.Status(http.StatusNoContent)
  241. return
  242. }
  243. if err := comment.LoadPoster(); err != nil {
  244. ctx.Error(http.StatusInternalServerError, "comment.LoadPoster", err)
  245. return
  246. }
  247. ctx.JSON(http.StatusOK, comment.APIFormat())
  248. }
  249. // EditIssueComment modify a comment of an issue
  250. func EditIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) {
  251. // swagger:operation PATCH /repos/{owner}/{repo}/issues/comments/{id} issue issueEditComment
  252. // ---
  253. // summary: Edit a comment
  254. // consumes:
  255. // - application/json
  256. // produces:
  257. // - application/json
  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 the comment to edit
  272. // type: integer
  273. // format: int64
  274. // required: true
  275. // - name: body
  276. // in: body
  277. // schema:
  278. // "$ref": "#/definitions/EditIssueCommentOption"
  279. // responses:
  280. // "200":
  281. // "$ref": "#/responses/Comment"
  282. // "204":
  283. // "$ref": "#/responses/empty"
  284. // "403":
  285. // "$ref": "#/responses/forbidden"
  286. // "404":
  287. // "$ref": "#/responses/notFound"
  288. editIssueComment(ctx, form)
  289. }
  290. // EditIssueCommentDeprecated modify a comment of an issue
  291. func EditIssueCommentDeprecated(ctx *context.APIContext, form api.EditIssueCommentOption) {
  292. // swagger:operation PATCH /repos/{owner}/{repo}/issues/{index}/comments/{id} issue issueEditCommentDeprecated
  293. // ---
  294. // summary: Edit a comment
  295. // deprecated: true
  296. // consumes:
  297. // - application/json
  298. // produces:
  299. // - application/json
  300. // parameters:
  301. // - name: owner
  302. // in: path
  303. // description: owner of the repo
  304. // type: string
  305. // required: true
  306. // - name: repo
  307. // in: path
  308. // description: name of the repo
  309. // type: string
  310. // required: true
  311. // - name: index
  312. // in: path
  313. // description: this parameter is ignored
  314. // type: integer
  315. // required: true
  316. // - name: id
  317. // in: path
  318. // description: id of the comment to edit
  319. // type: integer
  320. // format: int64
  321. // required: true
  322. // - name: body
  323. // in: body
  324. // schema:
  325. // "$ref": "#/definitions/EditIssueCommentOption"
  326. // responses:
  327. // "200":
  328. // "$ref": "#/responses/Comment"
  329. // "204":
  330. // "$ref": "#/responses/empty"
  331. // "403":
  332. // "$ref": "#/responses/forbidden"
  333. // "404":
  334. // "$ref": "#/responses/notFound"
  335. editIssueComment(ctx, form)
  336. }
  337. func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) {
  338. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  339. if err != nil {
  340. if models.IsErrCommentNotExist(err) {
  341. ctx.NotFound(err)
  342. } else {
  343. ctx.Error(http.StatusInternalServerError, "GetCommentByID", err)
  344. }
  345. return
  346. }
  347. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  348. ctx.Status(http.StatusForbidden)
  349. return
  350. } else if comment.Type != models.CommentTypeComment {
  351. ctx.Status(http.StatusNoContent)
  352. return
  353. }
  354. oldContent := comment.Content
  355. comment.Content = form.Body
  356. if err := comment_service.UpdateComment(comment, ctx.User, oldContent); err != nil {
  357. ctx.Error(http.StatusInternalServerError, "UpdateComment", err)
  358. return
  359. }
  360. ctx.JSON(http.StatusOK, comment.APIFormat())
  361. }
  362. // DeleteIssueComment delete a comment from an issue
  363. func DeleteIssueComment(ctx *context.APIContext) {
  364. // swagger:operation DELETE /repos/{owner}/{repo}/issues/comments/{id} issue issueDeleteComment
  365. // ---
  366. // summary: Delete a comment
  367. // parameters:
  368. // - name: owner
  369. // in: path
  370. // description: owner of the repo
  371. // type: string
  372. // required: true
  373. // - name: repo
  374. // in: path
  375. // description: name of the repo
  376. // type: string
  377. // required: true
  378. // - name: id
  379. // in: path
  380. // description: id of comment to delete
  381. // type: integer
  382. // format: int64
  383. // required: true
  384. // responses:
  385. // "204":
  386. // "$ref": "#/responses/empty"
  387. // "403":
  388. // "$ref": "#/responses/forbidden"
  389. // "404":
  390. // "$ref": "#/responses/notFound"
  391. deleteIssueComment(ctx)
  392. }
  393. // DeleteIssueCommentDeprecated delete a comment from an issue
  394. func DeleteIssueCommentDeprecated(ctx *context.APIContext) {
  395. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/comments/{id} issue issueDeleteCommentDeprecated
  396. // ---
  397. // summary: Delete a comment
  398. // deprecated: true
  399. // parameters:
  400. // - name: owner
  401. // in: path
  402. // description: owner of the repo
  403. // type: string
  404. // required: true
  405. // - name: repo
  406. // in: path
  407. // description: name of the repo
  408. // type: string
  409. // required: true
  410. // - name: index
  411. // in: path
  412. // description: this parameter is ignored
  413. // type: integer
  414. // required: true
  415. // - name: id
  416. // in: path
  417. // description: id of comment to delete
  418. // type: integer
  419. // format: int64
  420. // required: true
  421. // responses:
  422. // "204":
  423. // "$ref": "#/responses/empty"
  424. // "403":
  425. // "$ref": "#/responses/forbidden"
  426. // "404":
  427. // "$ref": "#/responses/notFound"
  428. deleteIssueComment(ctx)
  429. }
  430. func deleteIssueComment(ctx *context.APIContext) {
  431. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  432. if err != nil {
  433. if models.IsErrCommentNotExist(err) {
  434. ctx.NotFound(err)
  435. } else {
  436. ctx.Error(http.StatusInternalServerError, "GetCommentByID", err)
  437. }
  438. return
  439. }
  440. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  441. ctx.Status(http.StatusForbidden)
  442. return
  443. } else if comment.Type != models.CommentTypeComment {
  444. ctx.Status(http.StatusNoContent)
  445. return
  446. }
  447. if err = comment_service.DeleteComment(comment, ctx.User); err != nil {
  448. ctx.Error(http.StatusInternalServerError, "DeleteCommentByID", err)
  449. return
  450. }
  451. ctx.Status(http.StatusNoContent)
  452. }