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.

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