您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "net/http"
  8. "strconv"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "code.gitea.io/gitea/routers/api/v1/utils"
  13. )
  14. // ListLabels list all the labels of a repository
  15. func ListLabels(ctx *context.APIContext) {
  16. // swagger:operation GET /repos/{owner}/{repo}/labels issue issueListLabels
  17. // ---
  18. // summary: Get all of a repository's labels
  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: page
  33. // in: query
  34. // description: page number of results to return (1-based)
  35. // type: integer
  36. // - name: limit
  37. // in: query
  38. // description: page size of results, maximum page size is 50
  39. // type: integer
  40. // responses:
  41. // "200":
  42. // "$ref": "#/responses/LabelList"
  43. labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.Query("sort"), utils.GetListOptions(ctx))
  44. if err != nil {
  45. ctx.Error(http.StatusInternalServerError, "GetLabelsByRepoID", err)
  46. return
  47. }
  48. apiLabels := make([]*api.Label, len(labels))
  49. for i := range labels {
  50. apiLabels[i] = labels[i].APIFormat()
  51. }
  52. ctx.JSON(http.StatusOK, &apiLabels)
  53. }
  54. // GetLabel get label by repository and label id
  55. func GetLabel(ctx *context.APIContext) {
  56. // swagger:operation GET /repos/{owner}/{repo}/labels/{id} issue issueGetLabel
  57. // ---
  58. // summary: Get a single label
  59. // produces:
  60. // - application/json
  61. // parameters:
  62. // - name: owner
  63. // in: path
  64. // description: owner of the repo
  65. // type: string
  66. // required: true
  67. // - name: repo
  68. // in: path
  69. // description: name of the repo
  70. // type: string
  71. // required: true
  72. // - name: id
  73. // in: path
  74. // description: id of the label to get
  75. // type: integer
  76. // format: int64
  77. // required: true
  78. // responses:
  79. // "200":
  80. // "$ref": "#/responses/Label"
  81. var (
  82. label *models.Label
  83. err error
  84. )
  85. strID := ctx.Params(":id")
  86. if intID, err2 := strconv.ParseInt(strID, 10, 64); err2 != nil {
  87. label, err = models.GetLabelInRepoByName(ctx.Repo.Repository.ID, strID)
  88. } else {
  89. label, err = models.GetLabelInRepoByID(ctx.Repo.Repository.ID, intID)
  90. }
  91. if err != nil {
  92. if models.IsErrLabelNotExist(err) {
  93. ctx.NotFound()
  94. } else {
  95. ctx.Error(http.StatusInternalServerError, "GetLabelByRepoID", err)
  96. }
  97. return
  98. }
  99. ctx.JSON(http.StatusOK, label.APIFormat())
  100. }
  101. // CreateLabel create a label for a repository
  102. func CreateLabel(ctx *context.APIContext, form api.CreateLabelOption) {
  103. // swagger:operation POST /repos/{owner}/{repo}/labels issue issueCreateLabel
  104. // ---
  105. // summary: Create a label
  106. // consumes:
  107. // - application/json
  108. // produces:
  109. // - application/json
  110. // parameters:
  111. // - name: owner
  112. // in: path
  113. // description: owner of the repo
  114. // type: string
  115. // required: true
  116. // - name: repo
  117. // in: path
  118. // description: name of the repo
  119. // type: string
  120. // required: true
  121. // - name: body
  122. // in: body
  123. // schema:
  124. // "$ref": "#/definitions/CreateLabelOption"
  125. // responses:
  126. // "201":
  127. // "$ref": "#/responses/Label"
  128. label := &models.Label{
  129. Name: form.Name,
  130. Color: form.Color,
  131. RepoID: ctx.Repo.Repository.ID,
  132. Description: form.Description,
  133. }
  134. if err := models.NewLabel(label); err != nil {
  135. ctx.Error(http.StatusInternalServerError, "NewLabel", err)
  136. return
  137. }
  138. ctx.JSON(http.StatusCreated, label.APIFormat())
  139. }
  140. // EditLabel modify a label for a repository
  141. func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
  142. // swagger:operation PATCH /repos/{owner}/{repo}/labels/{id} issue issueEditLabel
  143. // ---
  144. // summary: Update a label
  145. // consumes:
  146. // - application/json
  147. // produces:
  148. // - application/json
  149. // parameters:
  150. // - name: owner
  151. // in: path
  152. // description: owner of the repo
  153. // type: string
  154. // required: true
  155. // - name: repo
  156. // in: path
  157. // description: name of the repo
  158. // type: string
  159. // required: true
  160. // - name: id
  161. // in: path
  162. // description: id of the label to edit
  163. // type: integer
  164. // format: int64
  165. // required: true
  166. // - name: body
  167. // in: body
  168. // schema:
  169. // "$ref": "#/definitions/EditLabelOption"
  170. // responses:
  171. // "200":
  172. // "$ref": "#/responses/Label"
  173. label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  174. if err != nil {
  175. if models.IsErrLabelNotExist(err) {
  176. ctx.NotFound()
  177. } else {
  178. ctx.Error(http.StatusInternalServerError, "GetLabelByRepoID", err)
  179. }
  180. return
  181. }
  182. if form.Name != nil {
  183. label.Name = *form.Name
  184. }
  185. if form.Color != nil {
  186. label.Color = *form.Color
  187. }
  188. if form.Description != nil {
  189. label.Description = *form.Description
  190. }
  191. if err := models.UpdateLabel(label); err != nil {
  192. ctx.ServerError("UpdateLabel", err)
  193. return
  194. }
  195. ctx.JSON(http.StatusOK, label.APIFormat())
  196. }
  197. // DeleteLabel delete a label for a repository
  198. func DeleteLabel(ctx *context.APIContext) {
  199. // swagger:operation DELETE /repos/{owner}/{repo}/labels/{id} issue issueDeleteLabel
  200. // ---
  201. // summary: Delete a label
  202. // parameters:
  203. // - name: owner
  204. // in: path
  205. // description: owner of the repo
  206. // type: string
  207. // required: true
  208. // - name: repo
  209. // in: path
  210. // description: name of the repo
  211. // type: string
  212. // required: true
  213. // - name: id
  214. // in: path
  215. // description: id of the label to delete
  216. // type: integer
  217. // format: int64
  218. // required: true
  219. // responses:
  220. // "204":
  221. // "$ref": "#/responses/empty"
  222. if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  223. ctx.Error(http.StatusInternalServerError, "DeleteLabel", err)
  224. return
  225. }
  226. ctx.Status(http.StatusNoContent)
  227. }