2016-08-03 09:24:16 -07:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
|
|
import (
|
2019-08-10 13:40:48 -07:00
|
|
|
"net/http"
|
|
|
|
|
|
2018-05-27 08:53:48 +08:00
|
|
|
api "github.com/gogs/go-gogs-client"
|
2016-08-03 09:24:16 -07:00
|
|
|
|
2018-05-27 08:53:48 +08:00
|
|
|
"github.com/gogs/gogs/models"
|
|
|
|
|
"github.com/gogs/gogs/models/errors"
|
|
|
|
|
"github.com/gogs/gogs/pkg/context"
|
2016-08-03 09:24:16 -07:00
|
|
|
)
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func ListIssueLabels(c *context.APIContext) {
|
|
|
|
|
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
|
2016-08-03 09:24:16 -07:00
|
|
|
if err != nil {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
|
2016-08-03 09:24:16 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
apiLabels := make([]*api.Label, len(issue.Labels))
|
|
|
|
|
for i := range issue.Labels {
|
2016-08-14 04:17:26 -07:00
|
|
|
apiLabels[i] = issue.Labels[i].APIFormat()
|
2016-08-03 09:24:16 -07:00
|
|
|
}
|
2019-08-10 13:40:48 -07:00
|
|
|
c.JSONSuccess(&apiLabels)
|
2016-08-03 09:24:16 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func AddIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
|
|
|
|
|
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
|
2016-08-03 09:24:16 -07:00
|
|
|
if err != nil {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
|
2016-08-03 09:24:16 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
labels, err := models.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
|
2016-08-03 11:51:22 -07:00
|
|
|
if err != nil {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.ServerError("GetLabelsInRepoByIDs", err)
|
2016-08-03 09:24:16 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
if err = issue.AddLabels(c.User, labels); err != nil {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.ServerError("AddLabels", err)
|
2016-08-03 11:51:22 -07:00
|
|
|
return
|
2016-08-03 09:24:16 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-03 11:51:22 -07:00
|
|
|
labels, err = models.GetLabelsByIssueID(issue.ID)
|
2016-08-03 09:24:16 -07:00
|
|
|
if err != nil {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.ServerError("GetLabelsByIssueID", err)
|
2016-08-03 09:24:16 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-03 11:51:22 -07:00
|
|
|
apiLabels := make([]*api.Label, len(labels))
|
|
|
|
|
for i := range labels {
|
2016-08-14 04:17:26 -07:00
|
|
|
apiLabels[i] = issue.Labels[i].APIFormat()
|
2016-08-03 09:24:16 -07:00
|
|
|
}
|
2019-08-10 13:40:48 -07:00
|
|
|
c.JSONSuccess(&apiLabels)
|
2016-08-03 09:24:16 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func DeleteIssueLabel(c *context.APIContext) {
|
|
|
|
|
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
|
2016-08-03 09:24:16 -07:00
|
|
|
if err != nil {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
|
2016-08-03 09:24:16 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
|
2016-08-03 09:24:16 -07:00
|
|
|
if err != nil {
|
2016-08-03 11:51:22 -07:00
|
|
|
if models.IsErrLabelNotExist(err) {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.Error(http.StatusUnprocessableEntity, "", err)
|
2016-08-03 09:24:16 -07:00
|
|
|
} else {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.ServerError("GetLabelInRepoByID", err)
|
2016-08-03 09:24:16 -07:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-03 11:51:22 -07:00
|
|
|
if err := models.DeleteIssueLabel(issue, label); err != nil {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.ServerError("DeleteIssueLabel", err)
|
2016-08-03 11:51:22 -07:00
|
|
|
return
|
2016-08-03 09:24:16 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-10 13:40:48 -07:00
|
|
|
c.NoContent()
|
2016-08-03 09:24:16 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func ReplaceIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
|
|
|
|
|
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
|
2016-08-03 09:24:16 -07:00
|
|
|
if err != nil {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
|
2016-08-03 09:24:16 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
labels, err := models.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
|
2016-08-03 09:24:16 -07:00
|
|
|
if err != nil {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.ServerError("GetLabelsInRepoByIDs", err)
|
2016-08-03 09:24:16 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-03 11:51:22 -07:00
|
|
|
if err := issue.ReplaceLabels(labels); err != nil {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.ServerError("ReplaceLabels", err)
|
2016-08-03 09:24:16 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-03 11:51:22 -07:00
|
|
|
labels, err = models.GetLabelsByIssueID(issue.ID)
|
|
|
|
|
if err != nil {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.ServerError("GetLabelsByIssueID", err)
|
2016-08-03 11:51:22 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
apiLabels := make([]*api.Label, len(labels))
|
|
|
|
|
for i := range labels {
|
2016-08-14 04:17:26 -07:00
|
|
|
apiLabels[i] = issue.Labels[i].APIFormat()
|
2016-08-03 11:51:22 -07:00
|
|
|
}
|
2019-08-10 13:40:48 -07:00
|
|
|
c.JSONSuccess(&apiLabels)
|
2016-08-03 09:24:16 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func ClearIssueLabels(c *context.APIContext) {
|
|
|
|
|
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
|
2016-08-03 09:24:16 -07:00
|
|
|
if err != nil {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
|
2016-08-03 09:24:16 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
if err := issue.ClearLabels(c.User); err != nil {
|
2019-08-10 13:40:48 -07:00
|
|
|
c.ServerError("ClearLabels", err)
|
2016-08-03 09:24:16 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 13:40:48 -07:00
|
|
|
c.NoContent()
|
2016-08-03 09:24:16 -07:00
|
|
|
}
|