2016-08-03 21:54:16 +05:30
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2018-11-28 16:56:14 +05:30
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2016-08-03 21:54:16 +05:30
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2019-12-20 22:37:12 +05:30
|
|
|
"net/http"
|
2016-10-07 22:47:27 +05:30
|
|
|
"strconv"
|
|
|
|
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-05-11 15:51:34 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-01-25 00:30:29 +05:30
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2016-08-03 21:54:16 +05:30
|
|
|
)
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// ListLabels list all the labels of a repository
|
2016-08-03 21:54:16 +05:30
|
|
|
func ListLabels(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/labels issue issueListLabels
|
|
|
|
// ---
|
|
|
|
// summary: Get all of a repository's labels
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
2020-01-25 00:30:29 +05:30
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
|
|
|
// description: page size of results, maximum page size is 50
|
|
|
|
// type: integer
|
2017-11-13 12:32:25 +05:30
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/LabelList"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2020-01-25 00:30:29 +05:30
|
|
|
labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.Query("sort"), utils.GetListOptions(ctx))
|
2016-08-03 21:54:16 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetLabelsByRepoID", err)
|
2016-08-03 21:54:16 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiLabels := make([]*api.Label, len(labels))
|
|
|
|
for i := range labels {
|
2016-08-14 16:47:26 +05:30
|
|
|
apiLabels[i] = labels[i].APIFormat()
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.JSON(http.StatusOK, &apiLabels)
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// GetLabel get label by repository and label id
|
2016-08-03 21:54:16 +05:30
|
|
|
func GetLabel(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/labels/{id} issue issueGetLabel
|
|
|
|
// ---
|
|
|
|
// summary: Get a single label
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: id
|
|
|
|
// in: path
|
|
|
|
// description: id of the label to get
|
|
|
|
// type: integer
|
2018-10-21 09:10:42 +05:30
|
|
|
// format: int64
|
2017-11-13 12:32:25 +05:30
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/Label"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2016-10-07 22:47:27 +05:30
|
|
|
var (
|
|
|
|
label *models.Label
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
strID := ctx.Params(":id")
|
|
|
|
if intID, err2 := strconv.ParseInt(strID, 10, 64); err2 != nil {
|
|
|
|
label, err = models.GetLabelInRepoByName(ctx.Repo.Repository.ID, strID)
|
|
|
|
} else {
|
|
|
|
label, err = models.GetLabelInRepoByID(ctx.Repo.Repository.ID, intID)
|
|
|
|
}
|
2016-08-03 21:54:16 +05:30
|
|
|
if err != nil {
|
|
|
|
if models.IsErrLabelNotExist(err) {
|
2019-03-19 07:59:43 +05:30
|
|
|
ctx.NotFound()
|
2016-08-03 21:54:16 +05:30
|
|
|
} else {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetLabelByRepoID", err)
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.JSON(http.StatusOK, label.APIFormat())
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// CreateLabel create a label for a repository
|
2016-08-04 00:21:22 +05:30
|
|
|
func CreateLabel(ctx *context.APIContext, form api.CreateLabelOption) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/labels issue issueCreateLabel
|
|
|
|
// ---
|
|
|
|
// summary: Create a label
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/CreateLabelOption"
|
|
|
|
// responses:
|
|
|
|
// "201":
|
|
|
|
// "$ref": "#/responses/Label"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2016-08-03 21:54:16 +05:30
|
|
|
label := &models.Label{
|
2019-06-16 18:09:52 +05:30
|
|
|
Name: form.Name,
|
|
|
|
Color: form.Color,
|
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
|
|
|
Description: form.Description,
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
2017-06-25 11:45:09 +05:30
|
|
|
if err := models.NewLabel(label); err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "NewLabel", err)
|
2016-08-03 21:54:16 +05:30
|
|
|
return
|
|
|
|
}
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.JSON(http.StatusCreated, label.APIFormat())
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// EditLabel modify a label for a repository
|
2016-08-04 00:21:22 +05:30
|
|
|
func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation PATCH /repos/{owner}/{repo}/labels/{id} issue issueEditLabel
|
|
|
|
// ---
|
|
|
|
// summary: Update a label
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: id
|
|
|
|
// in: path
|
|
|
|
// description: id of the label to edit
|
|
|
|
// type: integer
|
2018-10-21 09:10:42 +05:30
|
|
|
// format: int64
|
2017-11-13 12:32:25 +05:30
|
|
|
// required: true
|
|
|
|
// - name: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/EditLabelOption"
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/Label"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2016-08-04 00:21:22 +05:30
|
|
|
label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
|
2016-08-03 21:54:16 +05:30
|
|
|
if err != nil {
|
|
|
|
if models.IsErrLabelNotExist(err) {
|
2019-03-19 07:59:43 +05:30
|
|
|
ctx.NotFound()
|
2016-08-03 21:54:16 +05:30
|
|
|
} else {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetLabelByRepoID", err)
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-04 00:21:22 +05:30
|
|
|
if form.Name != nil {
|
|
|
|
label.Name = *form.Name
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
2016-08-04 00:21:22 +05:30
|
|
|
if form.Color != nil {
|
|
|
|
label.Color = *form.Color
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
2019-06-16 18:09:52 +05:30
|
|
|
if form.Description != nil {
|
|
|
|
label.Description = *form.Description
|
|
|
|
}
|
2016-08-03 21:54:16 +05:30
|
|
|
if err := models.UpdateLabel(label); err != nil {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError("UpdateLabel", err)
|
2016-08-03 21:54:16 +05:30
|
|
|
return
|
|
|
|
}
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.JSON(http.StatusOK, label.APIFormat())
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// DeleteLabel delete a label for a repository
|
2016-08-03 21:54:16 +05:30
|
|
|
func DeleteLabel(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/labels/{id} issue issueDeleteLabel
|
|
|
|
// ---
|
|
|
|
// summary: Delete a label
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: id
|
|
|
|
// in: path
|
|
|
|
// description: id of the label to delete
|
|
|
|
// type: integer
|
2018-10-21 09:10:42 +05:30
|
|
|
// format: int64
|
2017-11-13 12:32:25 +05:30
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2016-08-03 21:54:16 +05:30
|
|
|
if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteLabel", err)
|
2016-08-03 21:54:16 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Status(http.StatusNoContent)
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|