2017-01-30 07:46:45 -05:00
|
|
|
// Copyright 2017 The Gitea 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 (
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/auth"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2017-03-14 21:10:35 -04:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2017-01-30 07:46:45 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplLabels base.TplName = "repo/issue/labels"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Labels render issue's labels page
|
|
|
|
func Labels(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.labels")
|
|
|
|
ctx.Data["PageIsIssueList"] = true
|
|
|
|
ctx.Data["PageIsLabels"] = true
|
|
|
|
ctx.Data["RequireMinicolors"] = true
|
2019-02-16 12:33:09 -05:00
|
|
|
ctx.Data["RequireTribute"] = true
|
2017-01-30 07:46:45 -05:00
|
|
|
ctx.Data["LabelTemplates"] = models.LabelTemplates
|
|
|
|
ctx.HTML(200, tplLabels)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitializeLabels init labels for a repository
|
|
|
|
func InitializeLabels(ctx *context.Context, form auth.InitializeLabelsForm) {
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
list, err := models.GetLabelTemplateFile(form.TemplateName)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.issues.label_templates.fail_to_load_file", form.TemplateName, err))
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := make([]*models.Label, len(list))
|
|
|
|
for i := 0; i < len(list); i++ {
|
|
|
|
labels[i] = &models.Label{
|
2018-03-12 22:03:55 -04:00
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
|
|
|
Name: list[i][0],
|
|
|
|
Description: list[i][2],
|
|
|
|
Color: list[i][1],
|
2017-01-30 07:46:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := models.NewLabels(labels...); err != nil {
|
2018-01-10 16:34:17 -05:00
|
|
|
ctx.ServerError("NewLabels", err)
|
2017-01-30 07:46:45 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetrieveLabels find all the labels of a repository
|
|
|
|
func RetrieveLabels(ctx *context.Context) {
|
|
|
|
labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.Query("sort"))
|
|
|
|
if err != nil {
|
2018-01-10 16:34:17 -05:00
|
|
|
ctx.ServerError("RetrieveLabels.GetLabels", err)
|
2017-01-30 07:46:45 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, l := range labels {
|
|
|
|
l.CalOpenIssues()
|
|
|
|
}
|
|
|
|
ctx.Data["Labels"] = labels
|
|
|
|
ctx.Data["NumLabels"] = len(labels)
|
|
|
|
ctx.Data["SortType"] = ctx.Query("sort")
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLabel create new label for repository
|
|
|
|
func NewLabel(ctx *context.Context, form auth.CreateLabelForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.labels")
|
|
|
|
ctx.Data["PageIsLabels"] = true
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l := &models.Label{
|
2018-03-12 22:03:55 -04:00
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
|
|
|
Name: form.Title,
|
|
|
|
Description: form.Description,
|
|
|
|
Color: form.Color,
|
2017-01-30 07:46:45 -05:00
|
|
|
}
|
2017-06-25 02:15:09 -04:00
|
|
|
if err := models.NewLabel(l); err != nil {
|
2018-01-10 16:34:17 -05:00
|
|
|
ctx.ServerError("NewLabel", err)
|
2017-01-30 07:46:45 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateLabel update a label's name and color
|
|
|
|
func UpdateLabel(ctx *context.Context, form auth.CreateLabelForm) {
|
|
|
|
l, err := models.GetLabelByID(form.ID)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case models.IsErrLabelNotExist(err):
|
|
|
|
ctx.Error(404)
|
|
|
|
default:
|
2018-01-10 16:34:17 -05:00
|
|
|
ctx.ServerError("UpdateLabel", err)
|
2017-01-30 07:46:45 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Name = form.Title
|
2018-03-12 22:03:55 -04:00
|
|
|
l.Description = form.Description
|
2017-01-30 07:46:45 -05:00
|
|
|
l.Color = form.Color
|
|
|
|
if err := models.UpdateLabel(l); err != nil {
|
2018-01-10 16:34:17 -05:00
|
|
|
ctx.ServerError("UpdateLabel", err)
|
2017-01-30 07:46:45 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteLabel delete a label
|
|
|
|
func DeleteLabel(ctx *context.Context) {
|
|
|
|
if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
|
|
|
|
ctx.Flash.Error("DeleteLabel: " + err.Error())
|
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.issues.label_deletion_success"))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(200, map[string]interface{}{
|
|
|
|
"redirect": ctx.Repo.RepoLink + "/labels",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateIssueLabel change issue's labels
|
|
|
|
func UpdateIssueLabel(ctx *context.Context) {
|
2017-03-14 21:10:35 -04:00
|
|
|
issues := getActionIssues(ctx)
|
2017-01-30 07:46:45 -05:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-14 21:10:35 -04:00
|
|
|
switch action := ctx.Query("action"); action {
|
|
|
|
case "clear":
|
|
|
|
for _, issue := range issues {
|
|
|
|
if err := issue.ClearLabels(ctx.User); err != nil {
|
2018-01-10 16:34:17 -05:00
|
|
|
ctx.ServerError("ClearLabels", err)
|
2017-03-14 21:10:35 -04:00
|
|
|
return
|
|
|
|
}
|
2017-01-30 07:46:45 -05:00
|
|
|
}
|
2017-03-14 21:10:35 -04:00
|
|
|
case "attach", "detach", "toggle":
|
2017-01-30 07:46:45 -05:00
|
|
|
label, err := models.GetLabelByID(ctx.QueryInt64("id"))
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrLabelNotExist(err) {
|
|
|
|
ctx.Error(404, "GetLabelByID")
|
|
|
|
} else {
|
2018-01-10 16:34:17 -05:00
|
|
|
ctx.ServerError("GetLabelByID", err)
|
2017-01-30 07:46:45 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-14 21:10:35 -04:00
|
|
|
if action == "toggle" {
|
2017-12-15 16:11:02 -05:00
|
|
|
// detach if any issues already have label, otherwise attach
|
|
|
|
action = "attach"
|
2017-03-14 21:10:35 -04:00
|
|
|
for _, issue := range issues {
|
|
|
|
if issue.HasLabel(label.ID) {
|
2017-12-15 16:11:02 -05:00
|
|
|
action = "detach"
|
2017-03-14 21:10:35 -04:00
|
|
|
break
|
|
|
|
}
|
2017-01-30 07:46:45 -05:00
|
|
|
}
|
2017-03-14 21:10:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if action == "attach" {
|
|
|
|
for _, issue := range issues {
|
|
|
|
if err = issue.AddLabel(ctx.User, label); err != nil {
|
2018-01-10 16:34:17 -05:00
|
|
|
ctx.ServerError("AddLabel", err)
|
2017-03-14 21:10:35 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, issue := range issues {
|
|
|
|
if err = issue.RemoveLabel(ctx.User, label); err != nil {
|
2018-01-10 16:34:17 -05:00
|
|
|
ctx.ServerError("RemoveLabel", err)
|
2017-03-14 21:10:35 -04:00
|
|
|
return
|
|
|
|
}
|
2017-01-30 07:46:45 -05:00
|
|
|
}
|
|
|
|
}
|
2017-03-14 21:10:35 -04:00
|
|
|
default:
|
|
|
|
log.Warn("Unrecognized action: %s", action)
|
|
|
|
ctx.Error(500)
|
|
|
|
return
|
2017-01-30 07:46:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(200, map[string]interface{}{
|
|
|
|
"ok": true,
|
|
|
|
})
|
|
|
|
}
|