本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

230 lines
5.9KB

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