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