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-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-02-29 08:19:50 +05:30
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2019-05-11 15:51:34 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2019-10-15 10:33:05 +05:30
|
|
|
issue_service "code.gitea.io/gitea/services/issue"
|
2016-08-03 21:54:16 +05:30
|
|
|
)
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// ListIssueLabels list all the labels of an issue
|
2016-08-04 00:21:22 +05:30
|
|
|
func ListIssueLabels(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/issues/{index}/labels issue issueGetLabels
|
|
|
|
// ---
|
|
|
|
// summary: Get an issue'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
|
|
|
|
// - name: index
|
|
|
|
// in: path
|
|
|
|
// description: index of the issue
|
|
|
|
// 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/LabelList"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2016-08-03 21:54:16 +05:30
|
|
|
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrIssueNotExist(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, "GetIssueByIndex", err)
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-13 21:15:19 +05:30
|
|
|
if err := issue.LoadAttributes(); err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
|
2019-02-13 21:15:19 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-29 08:19:50 +05:30
|
|
|
ctx.JSON(http.StatusOK, convert.ToLabelList(issue.Labels))
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// AddIssueLabels add labels for an issue
|
2016-08-03 21:54:16 +05:30
|
|
|
func AddIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
|
2018-05-08 11:35:18 +05:30
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/issues/{index}/labels issue issueAddLabel
|
2017-11-13 12:32:25 +05:30
|
|
|
// ---
|
|
|
|
// summary: Add a label to an issue
|
|
|
|
// 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: index
|
|
|
|
// in: path
|
|
|
|
// description: index of the issue
|
|
|
|
// 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/IssueLabelsOption"
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/LabelList"
|
2019-12-20 22:37:12 +05:30
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
|
2020-02-24 04:23:08 +05:30
|
|
|
issue, labels, err := prepareForReplaceOrAdd(ctx, form)
|
2016-08-03 21:54:16 +05:30
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-19 15:18:29 +05:30
|
|
|
if err = issue_service.AddLabels(issue, ctx.User, labels); err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "AddLabels", err)
|
2016-08-04 00:21:22 +05:30
|
|
|
return
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
|
2016-08-04 00:21:22 +05:30
|
|
|
labels, err = models.GetLabelsByIssueID(issue.ID)
|
2016-08-03 21:54:16 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetLabelsByIssueID", err)
|
2016-08-03 21:54:16 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-29 08:19:50 +05:30
|
|
|
ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// DeleteIssueLabel delete a label for an issue
|
2016-08-04 00:21:22 +05:30
|
|
|
func DeleteIssueLabel(ctx *context.APIContext) {
|
2018-05-08 11:35:18 +05:30
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/labels/{id} issue issueRemoveLabel
|
2017-11-13 12:32:25 +05:30
|
|
|
// ---
|
|
|
|
// summary: Remove a label from an issue
|
|
|
|
// 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: index
|
|
|
|
// in: path
|
|
|
|
// description: index of the issue
|
|
|
|
// type: integer
|
2018-10-21 09:10:42 +05:30
|
|
|
// format: int64
|
2017-11-13 12:32:25 +05:30
|
|
|
// required: true
|
|
|
|
// - name: id
|
|
|
|
// in: path
|
|
|
|
// description: id of the label to remove
|
|
|
|
// 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
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/validationError"
|
|
|
|
|
2016-08-03 21:54:16 +05:30
|
|
|
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrIssueNotExist(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, "GetIssueByIndex", err)
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-28 16:56:14 +05:30
|
|
|
if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Status(http.StatusForbidden)
|
2018-11-28 16:56:14 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-08-04 00:21:22 +05:30
|
|
|
if models.IsErrLabelNotExist(err) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", err)
|
2016-08-03 21:54:16 +05:30
|
|
|
} else {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetLabelInRepoByID", err)
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-24 04:23:08 +05:30
|
|
|
if err := issue_service.RemoveLabel(issue, ctx.User, label); err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteIssueLabel", err)
|
2016-08-04 00:21:22 +05:30
|
|
|
return
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Status(http.StatusNoContent)
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// ReplaceIssueLabels replace labels for an issue
|
2016-08-04 00:21:22 +05:30
|
|
|
func ReplaceIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
|
2018-05-08 11:35:18 +05:30
|
|
|
// swagger:operation PUT /repos/{owner}/{repo}/issues/{index}/labels issue issueReplaceLabels
|
2017-11-13 12:32:25 +05:30
|
|
|
// ---
|
|
|
|
// summary: Replace an issue's labels
|
|
|
|
// 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: index
|
|
|
|
// in: path
|
|
|
|
// description: index of the issue
|
|
|
|
// 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/IssueLabelsOption"
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/LabelList"
|
2019-12-20 22:37:12 +05:30
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
|
2020-02-24 04:23:08 +05:30
|
|
|
issue, labels, err := prepareForReplaceOrAdd(ctx, form)
|
2016-08-03 21:54:16 +05:30
|
|
|
if err != nil {
|
2018-11-28 16:56:14 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-24 04:23:08 +05:30
|
|
|
if err := issue_service.ReplaceLabels(issue, ctx.User, labels); err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "ReplaceLabels", err)
|
2016-08-03 21:54:16 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-04 00:21:22 +05:30
|
|
|
labels, err = models.GetLabelsByIssueID(issue.ID)
|
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetLabelsByIssueID", err)
|
2016-08-04 00:21:22 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-29 08:19:50 +05:30
|
|
|
ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// ClearIssueLabels delete all the labels for an issue
|
2016-08-03 21:54:16 +05:30
|
|
|
func ClearIssueLabels(ctx *context.APIContext) {
|
2018-05-08 11:35:18 +05:30
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/labels issue issueClearLabels
|
2017-11-13 12:32:25 +05:30
|
|
|
// ---
|
|
|
|
// summary: Remove all labels from an issue
|
|
|
|
// 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: index
|
|
|
|
// in: path
|
|
|
|
// description: index of the issue
|
|
|
|
// 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
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
|
2016-08-03 21:54:16 +05:30
|
|
|
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrIssueNotExist(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, "GetIssueByIndex", err)
|
2016-08-03 21:54:16 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-28 16:56:14 +05:30
|
|
|
if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Status(http.StatusForbidden)
|
2018-11-28 16:56:14 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-15 10:33:05 +05:30
|
|
|
if err := issue_service.ClearLabels(issue, ctx.User); err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "ClearLabels", 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
|
|
|
}
|
2020-02-24 04:23:08 +05:30
|
|
|
|
|
|
|
func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption) (issue *models.Issue, labels []*models.Label, err error) {
|
|
|
|
issue, err = models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrIssueNotExist(err) {
|
|
|
|
ctx.NotFound()
|
|
|
|
} else {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
labels, err = models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDs", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
|
|
|
|
ctx.Status(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|