Skip to content

Fix new label validation #34624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion modules/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
ExclusiveOrder int `yaml:"exclusive_order,omitempty"`
}

type ErrInvalidLabelColor struct {
Color string
}

func (e *ErrInvalidLabelColor) Error() string {
return fmt.Sprintf("invalid label color: %s", e.Color)

Check failure on line 29 in modules/label/label.go

View workflow job for this annotation

GitHub Actions / lint-backend

string-format: fmt.Sprintf can be replaced with string concatenation (perfsprint)

Check failure on line 29 in modules/label/label.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

string-format: fmt.Sprintf can be replaced with string concatenation (perfsprint)

Check failure on line 29 in modules/label/label.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

string-format: fmt.Sprintf can be replaced with string concatenation (perfsprint)
}

func IsErrInvalidLabelColor(err error) bool {
_, ok := err.(*ErrInvalidLabelColor)
return ok
}

// NormalizeColor normalizes a color string to a 6-character hex code
func NormalizeColor(color string) (string, error) {
// normalize case
Expand All @@ -32,7 +45,9 @@
}

if !colorPattern.MatchString(color) {
return "", fmt.Errorf("bad color code: %s", color)
return "", &ErrInvalidLabelColor{
Color: color,
}
}

// convert 3-character shorthand into 6-character version
Expand Down
14 changes: 12 additions & 2 deletions routers/web/org/org_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ func NewLabel(ctx *context.Context) {
ExclusiveOrder: form.ExclusiveOrder,
}
if err := issues_model.NewLabel(ctx, l); err != nil {
ctx.ServerError("NewLabel", err)
if label.IsErrInvalidLabelColor(err) {
ctx.Flash.Error(err.Error())
ctx.Redirect(ctx.Org.OrgLink + "/settings/labels")
} else {
ctx.ServerError("NewLabel", err)
}
return
}
ctx.Redirect(ctx.Org.OrgLink + "/settings/labels")
Expand All @@ -79,7 +84,12 @@ func UpdateLabel(ctx *context.Context) {
l.Color = form.Color
l.SetArchived(form.IsArchived)
if err := issues_model.UpdateLabel(ctx, l); err != nil {
ctx.ServerError("UpdateLabel", err)
if label.IsErrInvalidLabelColor(err) {
ctx.Flash.Error(err.Error())
ctx.Redirect(ctx.Org.OrgLink + "/settings/labels")
} else {
ctx.ServerError("UpdateLabel", err)
}
return
}
ctx.Redirect(ctx.Org.OrgLink + "/settings/labels")
Expand Down
14 changes: 12 additions & 2 deletions routers/web/repo/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ func NewLabel(ctx *context.Context) {
Color: form.Color,
}
if err := issues_model.NewLabel(ctx, l); err != nil {
ctx.ServerError("NewLabel", err)
if label.IsErrInvalidLabelColor(err) {
ctx.Flash.Error(err.Error())
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
} else {
ctx.ServerError("NewLabel", err)
}
return
}
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
Expand All @@ -146,7 +151,12 @@ func UpdateLabel(ctx *context.Context) {

l.SetArchived(form.IsArchived)
if err := issues_model.UpdateLabel(ctx, l); err != nil {
ctx.ServerError("UpdateLabel", err)
if label.IsErrInvalidLabelColor(err) {
ctx.Flash.Error(err.Error())
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
} else {
ctx.ServerError("UpdateLabel", err)
}
return
}
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
Expand Down
Loading