Files

259 lines
6.4 KiB
Go
Raw Permalink Normal View History

2020-04-01 00:14:46 -04:00
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2020-04-01 00:14:46 -04:00
package org
import (
"net/http"
"strconv"
"strings"
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/modules/label"
2020-04-01 00:14:46 -04:00
api "code.gitea.io/gitea/modules/structs"
2021-01-26 23:36:53 +08:00
"code.gitea.io/gitea/modules/web"
2020-04-01 00:14:46 -04:00
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/context"
2022-12-29 03:57:15 +01:00
"code.gitea.io/gitea/services/convert"
2020-04-01 00:14:46 -04:00
)
// ListLabels list all the labels of an organization
func ListLabels(ctx *context.APIContext) {
// swagger:operation GET /orgs/{org}/labels organization orgListLabels
// ---
// summary: List an organization's labels
// produces:
// - application/json
// parameters:
// - name: org
// in: path
// description: name of the organization
// type: string
// required: true
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results
2020-04-01 00:14:46 -04:00
// type: integer
// responses:
// "200":
// "$ref": "#/responses/LabelList"
// "404":
// "$ref": "#/responses/notFound"
2020-04-01 00:14:46 -04:00
labels, err := issues_model.GetLabelsByOrgID(ctx, ctx.Org.Organization.ID, ctx.FormString("sort"), utils.GetListOptions(ctx))
2020-04-01 00:14:46 -04:00
if err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2020-04-01 00:14:46 -04:00
return
}
count, err := issues_model.CountLabelsByOrgID(ctx, ctx.Org.Organization.ID)
2021-08-12 14:43:08 +02:00
if err != nil {
2025-02-17 14:13:17 +08:00
ctx.APIErrorInternal(err)
2021-08-12 14:43:08 +02:00
return
}
ctx.SetTotalCountHeader(count)
ctx.JSON(http.StatusOK, convert.ToLabelList(labels, nil, ctx.Org.Organization.AsUser()))
2020-04-01 00:14:46 -04:00
}
// CreateLabel create a label for a repository
2021-01-26 23:36:53 +08:00
func CreateLabel(ctx *context.APIContext) {
2020-04-01 00:14:46 -04:00
// swagger:operation POST /orgs/{org}/labels organization orgCreateLabel
// ---
// summary: Create a label for an organization
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: org
// in: path
// description: name of the organization
// type: string
// required: true
// - name: body
// in: body
// schema:
// "$ref": "#/definitions/CreateLabelOption"
// responses:
// "201":
// "$ref": "#/responses/Label"
// "404":
// "$ref": "#/responses/notFound"
2020-04-01 00:14:46 -04:00
// "422":
// "$ref": "#/responses/validationError"
2021-01-26 23:36:53 +08:00
form := web.GetForm(ctx).(*api.CreateLabelOption)
2020-04-01 00:14:46 -04:00
form.Color = strings.Trim(form.Color, " ")
color, err := label.NormalizeColor(form.Color)
if err != nil {
2025-02-17 14:13:17 +08:00
ctx.APIError(http.StatusUnprocessableEntity, err)
2020-04-01 00:14:46 -04:00
return
}
form.Color = color
2020-04-01 00:14:46 -04:00
label := &issues_model.Label{
2020-04-01 00:14:46 -04:00
Name: form.Name,
2023-02-18 20:17:39 +01:00
Exclusive: form.Exclusive,
2020-04-01 00:14:46 -04:00
Color: form.Color,
OrgID: ctx.Org.Organization.ID,
Description: form.Description,
}
if err := issues_model.NewLabel(ctx, label); err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2020-04-01 00:14:46 -04:00
return
}
2021-09-10 18:03:16 +02:00
ctx.JSON(http.StatusCreated, convert.ToLabel(label, nil, ctx.Org.Organization.AsUser()))
2020-04-01 00:14:46 -04:00
}
// GetLabel get label by organization and label id
func GetLabel(ctx *context.APIContext) {
// swagger:operation GET /orgs/{org}/labels/{id} organization orgGetLabel
// ---
// summary: Get a single label
// produces:
// - application/json
// parameters:
// - name: org
// in: path
// description: name of the organization
// type: string
// required: true
// - name: id
// in: path
// description: id of the label to get
// type: integer
// format: int64
// required: true
// responses:
// "200":
// "$ref": "#/responses/Label"
// "404":
// "$ref": "#/responses/notFound"
2020-04-01 00:14:46 -04:00
var (
label *issues_model.Label
2020-04-01 00:14:46 -04:00
err error
)
2024-12-24 21:47:45 +08:00
strID := ctx.PathParam("id")
2020-04-01 00:14:46 -04:00
if intID, err2 := strconv.ParseInt(strID, 10, 64); err2 != nil {
label, err = issues_model.GetLabelInOrgByName(ctx, ctx.Org.Organization.ID, strID)
2020-04-01 00:14:46 -04:00
} else {
label, err = issues_model.GetLabelInOrgByID(ctx, ctx.Org.Organization.ID, intID)
2020-04-01 00:14:46 -04:00
}
if err != nil {
if issues_model.IsErrOrgLabelNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
2020-04-01 00:14:46 -04:00
} else {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2020-04-01 00:14:46 -04:00
}
return
}
ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, ctx.Org.Organization.AsUser()))
2020-04-01 00:14:46 -04:00
}
// EditLabel modify a label for an Organization
2021-01-26 23:36:53 +08:00
func EditLabel(ctx *context.APIContext) {
2020-04-01 00:14:46 -04:00
// swagger:operation PATCH /orgs/{org}/labels/{id} organization orgEditLabel
// ---
// summary: Update a label
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: org
// in: path
// description: name of the organization
// type: string
// required: true
// - name: id
// in: path
// description: id of the label to edit
// type: integer
// format: int64
// required: true
// - name: body
// in: body
// schema:
// "$ref": "#/definitions/EditLabelOption"
// responses:
// "200":
// "$ref": "#/responses/Label"
// "404":
// "$ref": "#/responses/notFound"
2020-04-01 00:14:46 -04:00
// "422":
// "$ref": "#/responses/validationError"
2021-01-26 23:36:53 +08:00
form := web.GetForm(ctx).(*api.EditLabelOption)
2024-12-24 21:47:45 +08:00
l, err := issues_model.GetLabelInOrgByID(ctx, ctx.Org.Organization.ID, ctx.PathParamInt64("id"))
2020-04-01 00:14:46 -04:00
if err != nil {
if issues_model.IsErrOrgLabelNotExist(err) {
2025-02-17 14:13:17 +08:00
ctx.APIErrorNotFound()
2020-04-01 00:14:46 -04:00
} else {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2020-04-01 00:14:46 -04:00
}
return
}
if form.Name != nil {
l.Name = *form.Name
2020-04-01 00:14:46 -04:00
}
2023-02-18 20:17:39 +01:00
if form.Exclusive != nil {
l.Exclusive = *form.Exclusive
2023-02-18 20:17:39 +01:00
}
2020-04-01 00:14:46 -04:00
if form.Color != nil {
color, err := label.NormalizeColor(*form.Color)
if err != nil {
2025-02-17 14:13:17 +08:00
ctx.APIError(http.StatusUnprocessableEntity, err)
2020-04-01 00:14:46 -04:00
return
}
l.Color = color
2020-04-01 00:14:46 -04:00
}
if form.Description != nil {
l.Description = *form.Description
2020-04-01 00:14:46 -04:00
}
2023-08-14 15:26:14 +05:30
l.SetArchived(form.IsArchived != nil && *form.IsArchived)
if err := issues_model.UpdateLabel(ctx, l); err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2020-04-01 00:14:46 -04:00
return
}
2021-09-10 18:03:16 +02:00
ctx.JSON(http.StatusOK, convert.ToLabel(l, nil, ctx.Org.Organization.AsUser()))
2020-04-01 00:14:46 -04:00
}
// DeleteLabel delete a label for an organization
func DeleteLabel(ctx *context.APIContext) {
// swagger:operation DELETE /orgs/{org}/labels/{id} organization orgDeleteLabel
// ---
// summary: Delete a label
// parameters:
// - name: org
// in: path
// description: name of the organization
// type: string
// required: true
// - name: id
// in: path
// description: id of the label to delete
// type: integer
// format: int64
// required: true
// responses:
// "204":
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/notFound"
2020-04-01 00:14:46 -04:00
2024-12-24 21:47:45 +08:00
if err := issues_model.DeleteLabel(ctx, ctx.Org.Organization.ID, ctx.PathParamInt64("id")); err != nil {
2025-02-18 04:41:03 +08:00
ctx.APIErrorInternal(err)
2020-04-01 00:14:46 -04:00
return
}
ctx.Status(http.StatusNoContent)
}