summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2020-02-09 15:33:03 +0100
committerGitHub <noreply@github.com>2020-02-09 14:33:03 +0000
commite273817154e98b7b675e43e97cd17f48a603cf9e (patch)
tree9f350c301a87d3dd57b4bdad954cf1c44185cb1a /routers
parent74a4a1e17f99d147db9329718a5ecc87984b0ecd (diff)
downloadgitea-e273817154e98b7b675e43e97cd17f48a603cf9e.tar.gz
gitea-e273817154e98b7b675e43e97cd17f48a603cf9e.zip
[API] Fix inconsistent label color format (#10129)
* update and use labelColorPattern * add TestCases * fix lint * # optional for templates * fix typo * some more * fix lint of **master**
Diffstat (limited to 'routers')
-rw-r--r--routers/api/v1/repo/label.go24
-rw-r--r--routers/repo/issue_label.go4
2 files changed, 25 insertions, 3 deletions
diff --git a/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go
index 6d438610be..422046001d 100644
--- a/routers/api/v1/repo/label.go
+++ b/routers/api/v1/repo/label.go
@@ -6,8 +6,10 @@
package repo
import (
+ "fmt"
"net/http"
"strconv"
+ "strings"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
@@ -135,6 +137,17 @@ func CreateLabel(ctx *context.APIContext, form api.CreateLabelOption) {
// responses:
// "201":
// "$ref": "#/responses/Label"
+ // "422":
+ // "$ref": "#/responses/validationError"
+
+ form.Color = strings.Trim(form.Color, " ")
+ if len(form.Color) == 6 {
+ form.Color = "#" + form.Color
+ }
+ if !models.LabelColorPattern.MatchString(form.Color) {
+ ctx.Error(http.StatusUnprocessableEntity, "ColorPattern", fmt.Errorf("bad color code: %s", form.Color))
+ return
+ }
label := &models.Label{
Name: form.Name,
@@ -182,6 +195,8 @@ func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
// responses:
// "200":
// "$ref": "#/responses/Label"
+ // "422":
+ // "$ref": "#/responses/validationError"
label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
if err != nil {
@@ -197,7 +212,14 @@ func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
label.Name = *form.Name
}
if form.Color != nil {
- label.Color = *form.Color
+ label.Color = strings.Trim(*form.Color, " ")
+ if len(label.Color) == 6 {
+ label.Color = "#" + label.Color
+ }
+ if !models.LabelColorPattern.MatchString(label.Color) {
+ ctx.Error(http.StatusUnprocessableEntity, "ColorPattern", fmt.Errorf("bad color code: %s", label.Color))
+ return
+ }
}
if form.Description != nil {
label.Description = *form.Description
diff --git a/routers/repo/issue_label.go b/routers/repo/issue_label.go
index e61fcbe5cb..8ac9b8d336 100644
--- a/routers/repo/issue_label.go
+++ b/routers/repo/issue_label.go
@@ -35,14 +35,14 @@ func InitializeLabels(ctx *context.Context, form auth.InitializeLabelsForm) {
return
}
- if err := models.InitalizeLabels(models.DefaultDBContext(), ctx.Repo.Repository.ID, form.TemplateName); err != nil {
+ if err := models.InitializeLabels(models.DefaultDBContext(), ctx.Repo.Repository.ID, form.TemplateName); err != nil {
if models.IsErrIssueLabelTemplateLoad(err) {
originalErr := err.(models.ErrIssueLabelTemplateLoad).OriginalError
ctx.Flash.Error(ctx.Tr("repo.issues.label_templates.fail_to_load_file", form.TemplateName, originalErr))
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
return
}
- ctx.ServerError("InitalizeLabels", err)
+ ctx.ServerError("InitializeLabels", err)
return
}
ctx.Redirect(ctx.Repo.RepoLink + "/labels")