code.gitea.io/gitea@v1.21.7/routers/api/v1/repo/label.go (about) 1 // Copyright 2016 The Gogs Authors. All rights reserved. 2 // Copyright 2018 The Gitea Authors. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 5 package repo 6 7 import ( 8 "net/http" 9 "strconv" 10 11 issues_model "code.gitea.io/gitea/models/issues" 12 "code.gitea.io/gitea/modules/context" 13 "code.gitea.io/gitea/modules/label" 14 api "code.gitea.io/gitea/modules/structs" 15 "code.gitea.io/gitea/modules/web" 16 "code.gitea.io/gitea/routers/api/v1/utils" 17 "code.gitea.io/gitea/services/convert" 18 ) 19 20 // ListLabels list all the labels of a repository 21 func ListLabels(ctx *context.APIContext) { 22 // swagger:operation GET /repos/{owner}/{repo}/labels issue issueListLabels 23 // --- 24 // summary: Get all of a repository's labels 25 // produces: 26 // - application/json 27 // parameters: 28 // - name: owner 29 // in: path 30 // description: owner of the repo 31 // type: string 32 // required: true 33 // - name: repo 34 // in: path 35 // description: name of the repo 36 // type: string 37 // required: true 38 // - name: page 39 // in: query 40 // description: page number of results to return (1-based) 41 // type: integer 42 // - name: limit 43 // in: query 44 // description: page size of results 45 // type: integer 46 // responses: 47 // "200": 48 // "$ref": "#/responses/LabelList" 49 // "404": 50 // "$ref": "#/responses/notFound" 51 52 labels, err := issues_model.GetLabelsByRepoID(ctx, ctx.Repo.Repository.ID, ctx.FormString("sort"), utils.GetListOptions(ctx)) 53 if err != nil { 54 ctx.Error(http.StatusInternalServerError, "GetLabelsByRepoID", err) 55 return 56 } 57 58 count, err := issues_model.CountLabelsByRepoID(ctx, ctx.Repo.Repository.ID) 59 if err != nil { 60 ctx.InternalServerError(err) 61 return 62 } 63 64 ctx.SetTotalCountHeader(count) 65 ctx.JSON(http.StatusOK, convert.ToLabelList(labels, ctx.Repo.Repository, nil)) 66 } 67 68 // GetLabel get label by repository and label id 69 func GetLabel(ctx *context.APIContext) { 70 // swagger:operation GET /repos/{owner}/{repo}/labels/{id} issue issueGetLabel 71 // --- 72 // summary: Get a single label 73 // produces: 74 // - application/json 75 // parameters: 76 // - name: owner 77 // in: path 78 // description: owner of the repo 79 // type: string 80 // required: true 81 // - name: repo 82 // in: path 83 // description: name of the repo 84 // type: string 85 // required: true 86 // - name: id 87 // in: path 88 // description: id of the label to get 89 // type: integer 90 // format: int64 91 // required: true 92 // responses: 93 // "200": 94 // "$ref": "#/responses/Label" 95 // "404": 96 // "$ref": "#/responses/notFound" 97 98 var ( 99 l *issues_model.Label 100 err error 101 ) 102 strID := ctx.Params(":id") 103 if intID, err2 := strconv.ParseInt(strID, 10, 64); err2 != nil { 104 l, err = issues_model.GetLabelInRepoByName(ctx, ctx.Repo.Repository.ID, strID) 105 } else { 106 l, err = issues_model.GetLabelInRepoByID(ctx, ctx.Repo.Repository.ID, intID) 107 } 108 if err != nil { 109 if issues_model.IsErrRepoLabelNotExist(err) { 110 ctx.NotFound() 111 } else { 112 ctx.Error(http.StatusInternalServerError, "GetLabelByRepoID", err) 113 } 114 return 115 } 116 117 ctx.JSON(http.StatusOK, convert.ToLabel(l, ctx.Repo.Repository, nil)) 118 } 119 120 // CreateLabel create a label for a repository 121 func CreateLabel(ctx *context.APIContext) { 122 // swagger:operation POST /repos/{owner}/{repo}/labels issue issueCreateLabel 123 // --- 124 // summary: Create a label 125 // consumes: 126 // - application/json 127 // produces: 128 // - application/json 129 // parameters: 130 // - name: owner 131 // in: path 132 // description: owner of the repo 133 // type: string 134 // required: true 135 // - name: repo 136 // in: path 137 // description: name of the repo 138 // type: string 139 // required: true 140 // - name: body 141 // in: body 142 // schema: 143 // "$ref": "#/definitions/CreateLabelOption" 144 // responses: 145 // "201": 146 // "$ref": "#/responses/Label" 147 // "404": 148 // "$ref": "#/responses/notFound" 149 // "422": 150 // "$ref": "#/responses/validationError" 151 152 form := web.GetForm(ctx).(*api.CreateLabelOption) 153 154 color, err := label.NormalizeColor(form.Color) 155 if err != nil { 156 ctx.Error(http.StatusUnprocessableEntity, "StringToColor", err) 157 return 158 } 159 form.Color = color 160 l := &issues_model.Label{ 161 Name: form.Name, 162 Exclusive: form.Exclusive, 163 Color: form.Color, 164 RepoID: ctx.Repo.Repository.ID, 165 Description: form.Description, 166 } 167 l.SetArchived(form.IsArchived) 168 if err := issues_model.NewLabel(ctx, l); err != nil { 169 ctx.Error(http.StatusInternalServerError, "NewLabel", err) 170 return 171 } 172 173 ctx.JSON(http.StatusCreated, convert.ToLabel(l, ctx.Repo.Repository, nil)) 174 } 175 176 // EditLabel modify a label for a repository 177 func EditLabel(ctx *context.APIContext) { 178 // swagger:operation PATCH /repos/{owner}/{repo}/labels/{id} issue issueEditLabel 179 // --- 180 // summary: Update a label 181 // consumes: 182 // - application/json 183 // produces: 184 // - application/json 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 label to edit 199 // type: integer 200 // format: int64 201 // required: true 202 // - name: body 203 // in: body 204 // schema: 205 // "$ref": "#/definitions/EditLabelOption" 206 // responses: 207 // "200": 208 // "$ref": "#/responses/Label" 209 // "404": 210 // "$ref": "#/responses/notFound" 211 // "422": 212 // "$ref": "#/responses/validationError" 213 214 form := web.GetForm(ctx).(*api.EditLabelOption) 215 l, err := issues_model.GetLabelInRepoByID(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) 216 if err != nil { 217 if issues_model.IsErrRepoLabelNotExist(err) { 218 ctx.NotFound() 219 } else { 220 ctx.Error(http.StatusInternalServerError, "GetLabelByRepoID", err) 221 } 222 return 223 } 224 225 if form.Name != nil { 226 l.Name = *form.Name 227 } 228 if form.Exclusive != nil { 229 l.Exclusive = *form.Exclusive 230 } 231 if form.Color != nil { 232 color, err := label.NormalizeColor(*form.Color) 233 if err != nil { 234 ctx.Error(http.StatusUnprocessableEntity, "StringToColor", err) 235 return 236 } 237 l.Color = color 238 } 239 if form.Description != nil { 240 l.Description = *form.Description 241 } 242 l.SetArchived(form.IsArchived != nil && *form.IsArchived) 243 if err := issues_model.UpdateLabel(ctx, l); err != nil { 244 ctx.Error(http.StatusInternalServerError, "UpdateLabel", err) 245 return 246 } 247 248 ctx.JSON(http.StatusOK, convert.ToLabel(l, ctx.Repo.Repository, nil)) 249 } 250 251 // DeleteLabel delete a label for a repository 252 func DeleteLabel(ctx *context.APIContext) { 253 // swagger:operation DELETE /repos/{owner}/{repo}/labels/{id} issue issueDeleteLabel 254 // --- 255 // summary: Delete a label 256 // parameters: 257 // - name: owner 258 // in: path 259 // description: owner of the repo 260 // type: string 261 // required: true 262 // - name: repo 263 // in: path 264 // description: name of the repo 265 // type: string 266 // required: true 267 // - name: id 268 // in: path 269 // description: id of the label to delete 270 // type: integer 271 // format: int64 272 // required: true 273 // responses: 274 // "204": 275 // "$ref": "#/responses/empty" 276 // "404": 277 // "$ref": "#/responses/notFound" 278 279 if err := issues_model.DeleteLabel(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil { 280 ctx.Error(http.StatusInternalServerError, "DeleteLabel", err) 281 return 282 } 283 284 ctx.Status(http.StatusNoContent) 285 }