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.

217 lines
5.4 KiB

  1. // Copyright 2016 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. // ListMilestones list all the milestones for a repository
  12. func ListMilestones(ctx *context.APIContext) {
  13. // swagger:operation GET /repos/{owner}/{repo}/milestones/{id} issue issueGetMilestone
  14. // ---
  15. // summary: Get a milestone
  16. // produces:
  17. // - application/json
  18. // responses:
  19. // "200":
  20. // "$ref": "#/responses/Milestone"
  21. milestones, err := models.GetMilestonesByRepoID(ctx.Repo.Repository.ID)
  22. if err != nil {
  23. ctx.Error(500, "GetMilestonesByRepoID", err)
  24. return
  25. }
  26. apiMilestones := make([]*api.Milestone, len(milestones))
  27. for i := range milestones {
  28. apiMilestones[i] = milestones[i].APIFormat()
  29. }
  30. ctx.JSON(200, &apiMilestones)
  31. }
  32. // GetMilestone get a milestone for a repository
  33. func GetMilestone(ctx *context.APIContext) {
  34. // swagger:operation GET /repos/{owner}/{repo}/milestones issue issueGetMilestones
  35. // ---
  36. // summary: Get all of a repository's milestones
  37. // produces:
  38. // - application/json
  39. // parameters:
  40. // - name: owner
  41. // in: path
  42. // description: owner of the repo
  43. // type: string
  44. // required: true
  45. // - name: repo
  46. // in: path
  47. // description: name of the repo
  48. // type: string
  49. // required: true
  50. // parameters:
  51. // - name: owner
  52. // in: path
  53. // description: owner of the repo
  54. // type: string
  55. // required: true
  56. // - name: repo
  57. // in: path
  58. // description: name of the repo
  59. // type: string
  60. // required: true
  61. // - name: id
  62. // in: path
  63. // description: id of the milestone to get
  64. // type: integer
  65. // required: true
  66. // responses:
  67. // "200":
  68. // "$ref": "#/responses/MilestoneList"
  69. milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  70. if err != nil {
  71. if models.IsErrMilestoneNotExist(err) {
  72. ctx.Status(404)
  73. } else {
  74. ctx.Error(500, "GetMilestoneByRepoID", err)
  75. }
  76. return
  77. }
  78. ctx.JSON(200, milestone.APIFormat())
  79. }
  80. // CreateMilestone create a milestone for a repository
  81. func CreateMilestone(ctx *context.APIContext, form api.CreateMilestoneOption) {
  82. // swagger:operation POST /repos/{owner}/{repo}/milestones issue issueCreateMilestone
  83. // ---
  84. // summary: Create a milestone
  85. // consumes:
  86. // - application/json
  87. // produces:
  88. // - application/json
  89. // parameters:
  90. // - name: owner
  91. // in: path
  92. // description: owner of the repo
  93. // type: string
  94. // required: true
  95. // - name: repo
  96. // in: path
  97. // description: name of the repo
  98. // type: string
  99. // required: true
  100. // - name: body
  101. // in: body
  102. // schema:
  103. // "$ref": "#/definitions/CreateMilestoneOption"
  104. // responses:
  105. // "201":
  106. // "$ref": "#/responses/Milestone"
  107. if form.Deadline == nil {
  108. defaultDeadline, _ := time.ParseInLocation("2006-01-02", "9999-12-31", time.Local)
  109. form.Deadline = &defaultDeadline
  110. }
  111. milestone := &models.Milestone{
  112. RepoID: ctx.Repo.Repository.ID,
  113. Name: form.Title,
  114. Content: form.Description,
  115. Deadline: *form.Deadline,
  116. }
  117. if err := models.NewMilestone(milestone); err != nil {
  118. ctx.Error(500, "NewMilestone", err)
  119. return
  120. }
  121. ctx.JSON(201, milestone.APIFormat())
  122. }
  123. // EditMilestone modify a milestone for a repository
  124. func EditMilestone(ctx *context.APIContext, form api.EditMilestoneOption) {
  125. // swagger:operation PATCH /repos/{owner}/{repo}/milestones/{id} issue issueEditMilestone
  126. // ---
  127. // summary: Update a milestone
  128. // consumes:
  129. // - application/json
  130. // produces:
  131. // - application/json
  132. // parameters:
  133. // - name: owner
  134. // in: path
  135. // description: owner of the repo
  136. // type: string
  137. // required: true
  138. // - name: repo
  139. // in: path
  140. // description: name of the repo
  141. // type: string
  142. // required: true
  143. // - name: body
  144. // in: body
  145. // schema:
  146. // "$ref": "#/definitions/EditMilestoneOption"
  147. // responses:
  148. // "200":
  149. // "$ref": "#/responses/Milestone"
  150. milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  151. if err != nil {
  152. if models.IsErrMilestoneNotExist(err) {
  153. ctx.Status(404)
  154. } else {
  155. ctx.Error(500, "GetMilestoneByRepoID", err)
  156. }
  157. return
  158. }
  159. if len(form.Title) > 0 {
  160. milestone.Name = form.Title
  161. }
  162. if form.Description != nil {
  163. milestone.Content = *form.Description
  164. }
  165. if form.Deadline != nil && !form.Deadline.IsZero() {
  166. milestone.Deadline = *form.Deadline
  167. }
  168. if err := models.UpdateMilestone(milestone); err != nil {
  169. ctx.Handle(500, "UpdateMilestone", err)
  170. return
  171. }
  172. ctx.JSON(200, milestone.APIFormat())
  173. }
  174. // DeleteMilestone delete a milestone for a repository
  175. func DeleteMilestone(ctx *context.APIContext) {
  176. // swagger:operation DELETE /repos/{owner}/{repo}/milestones/{id} issue issueDeleteMilestone
  177. // ---
  178. // summary: Delete a milestone
  179. // parameters:
  180. // - name: owner
  181. // in: path
  182. // description: owner of the repo
  183. // type: string
  184. // required: true
  185. // - name: repo
  186. // in: path
  187. // description: name of the repo
  188. // type: string
  189. // required: true
  190. // - name: body
  191. // in: path
  192. // description: id of the milestone to delete
  193. // type: integer
  194. // required: true
  195. // responses:
  196. // "204":
  197. // "$ref": "#/responses/empty"
  198. if err := models.DeleteMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  199. ctx.Error(500, "DeleteMilestoneByRepoID", err)
  200. return
  201. }
  202. ctx.Status(204)
  203. }