本站源代码
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

403 lines
10KB

  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. "time"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/notification"
  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(500, "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(500, "FindComments", err)
  63. return
  64. }
  65. if err := models.CommentList(comments).LoadPosters(); err != nil {
  66. ctx.Error(500, "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(200, &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(500, "FindComments", err)
  112. return
  113. }
  114. if err = models.CommentList(comments).LoadPosters(); err != nil {
  115. ctx.Error(500, "LoadPosters", err)
  116. return
  117. }
  118. apiComments := make([]*api.Comment, len(comments))
  119. if err := models.CommentList(comments).LoadIssues(); err != nil {
  120. ctx.Error(500, "LoadIssues", err)
  121. return
  122. }
  123. if err := models.CommentList(comments).LoadPosters(); err != nil {
  124. ctx.Error(500, "LoadPosters", err)
  125. return
  126. }
  127. if _, err := models.CommentList(comments).Issues().LoadRepositories(); err != nil {
  128. ctx.Error(500, "LoadRepositories", err)
  129. return
  130. }
  131. for i := range comments {
  132. apiComments[i] = comments[i].APIFormat()
  133. }
  134. ctx.JSON(200, &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. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  170. if err != nil {
  171. ctx.Error(500, "GetIssueByIndex", err)
  172. return
  173. }
  174. if issue.IsLocked && !ctx.Repo.CanWrite(models.UnitTypeIssues) && !ctx.User.IsAdmin {
  175. ctx.Error(403, "CreateIssueComment", errors.New(ctx.Tr("repo.issues.comment_on_locked")))
  176. return
  177. }
  178. comment, err := comment_service.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Body, nil)
  179. if err != nil {
  180. ctx.Error(500, "CreateIssueComment", err)
  181. return
  182. }
  183. notification.NotifyCreateIssueComment(ctx.User, ctx.Repo.Repository, issue, comment)
  184. ctx.JSON(201, comment.APIFormat())
  185. }
  186. // EditIssueComment modify a comment of an issue
  187. func EditIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) {
  188. // swagger:operation PATCH /repos/{owner}/{repo}/issues/comments/{id} issue issueEditComment
  189. // ---
  190. // summary: Edit a comment
  191. // consumes:
  192. // - application/json
  193. // produces:
  194. // - application/json
  195. // parameters:
  196. // - name: owner
  197. // in: path
  198. // description: owner of the repo
  199. // type: string
  200. // required: true
  201. // - name: repo
  202. // in: path
  203. // description: name of the repo
  204. // type: string
  205. // required: true
  206. // - name: id
  207. // in: path
  208. // description: id of the comment to edit
  209. // type: integer
  210. // format: int64
  211. // required: true
  212. // - name: body
  213. // in: body
  214. // schema:
  215. // "$ref": "#/definitions/EditIssueCommentOption"
  216. // responses:
  217. // "200":
  218. // "$ref": "#/responses/Comment"
  219. editIssueComment(ctx, form)
  220. }
  221. // EditIssueCommentDeprecated modify a comment of an issue
  222. func EditIssueCommentDeprecated(ctx *context.APIContext, form api.EditIssueCommentOption) {
  223. // swagger:operation PATCH /repos/{owner}/{repo}/issues/{index}/comments/{id} issue issueEditCommentDeprecated
  224. // ---
  225. // summary: Edit a comment
  226. // deprecated: true
  227. // consumes:
  228. // - application/json
  229. // produces:
  230. // - application/json
  231. // parameters:
  232. // - name: owner
  233. // in: path
  234. // description: owner of the repo
  235. // type: string
  236. // required: true
  237. // - name: repo
  238. // in: path
  239. // description: name of the repo
  240. // type: string
  241. // required: true
  242. // - name: index
  243. // in: path
  244. // description: this parameter is ignored
  245. // type: integer
  246. // required: true
  247. // - name: id
  248. // in: path
  249. // description: id of the comment to edit
  250. // type: integer
  251. // format: int64
  252. // required: true
  253. // - name: body
  254. // in: body
  255. // schema:
  256. // "$ref": "#/definitions/EditIssueCommentOption"
  257. // responses:
  258. // "200":
  259. // "$ref": "#/responses/Comment"
  260. editIssueComment(ctx, form)
  261. }
  262. func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) {
  263. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  264. if err != nil {
  265. if models.IsErrCommentNotExist(err) {
  266. ctx.NotFound(err)
  267. } else {
  268. ctx.Error(500, "GetCommentByID", err)
  269. }
  270. return
  271. }
  272. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  273. ctx.Status(403)
  274. return
  275. } else if comment.Type != models.CommentTypeComment {
  276. ctx.Status(204)
  277. return
  278. }
  279. oldContent := comment.Content
  280. comment.Content = form.Body
  281. if err := comment_service.UpdateComment(comment, ctx.User, oldContent); err != nil {
  282. ctx.Error(500, "UpdateComment", err)
  283. return
  284. }
  285. notification.NotifyUpdateComment(ctx.User, comment, oldContent)
  286. ctx.JSON(200, comment.APIFormat())
  287. }
  288. // DeleteIssueComment delete a comment from an issue
  289. func DeleteIssueComment(ctx *context.APIContext) {
  290. // swagger:operation DELETE /repos/{owner}/{repo}/issues/comments/{id} issue issueDeleteComment
  291. // ---
  292. // summary: Delete a comment
  293. // parameters:
  294. // - name: owner
  295. // in: path
  296. // description: owner of the repo
  297. // type: string
  298. // required: true
  299. // - name: repo
  300. // in: path
  301. // description: name of the repo
  302. // type: string
  303. // required: true
  304. // - name: id
  305. // in: path
  306. // description: id of comment to delete
  307. // type: integer
  308. // format: int64
  309. // required: true
  310. // responses:
  311. // "204":
  312. // "$ref": "#/responses/empty"
  313. deleteIssueComment(ctx)
  314. }
  315. // DeleteIssueCommentDeprecated delete a comment from an issue
  316. func DeleteIssueCommentDeprecated(ctx *context.APIContext) {
  317. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/comments/{id} issue issueDeleteCommentDeprecated
  318. // ---
  319. // summary: Delete a comment
  320. // deprecated: true
  321. // parameters:
  322. // - name: owner
  323. // in: path
  324. // description: owner of the repo
  325. // type: string
  326. // required: true
  327. // - name: repo
  328. // in: path
  329. // description: name of the repo
  330. // type: string
  331. // required: true
  332. // - name: index
  333. // in: path
  334. // description: this parameter is ignored
  335. // type: integer
  336. // required: true
  337. // - name: id
  338. // in: path
  339. // description: id of comment to delete
  340. // type: integer
  341. // format: int64
  342. // required: true
  343. // responses:
  344. // "204":
  345. // "$ref": "#/responses/empty"
  346. deleteIssueComment(ctx)
  347. }
  348. func deleteIssueComment(ctx *context.APIContext) {
  349. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  350. if err != nil {
  351. if models.IsErrCommentNotExist(err) {
  352. ctx.NotFound(err)
  353. } else {
  354. ctx.Error(500, "GetCommentByID", err)
  355. }
  356. return
  357. }
  358. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  359. ctx.Status(403)
  360. return
  361. } else if comment.Type != models.CommentTypeComment {
  362. ctx.Status(204)
  363. return
  364. }
  365. if err = comment_service.DeleteComment(comment, ctx.User); err != nil {
  366. ctx.Error(500, "DeleteCommentByID", err)
  367. return
  368. }
  369. notification.NotifyDeleteComment(ctx.User, comment)
  370. ctx.Status(204)
  371. }
上海开阖软件有限公司 沪ICP备12045867号-1