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.

milestone.go 6.3 kB

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