mirror of https://github.com/go-gitea/gitea.git
Backport #22976 Extract from #11669 and enhancement to #22585 to support exclusive scoped labels in label templates * Move label template functionality to label module * Fix handling of color codes * Add Advanced label template Co-authored-by: Lauris BH <lauris@nix.lv>pull/23225/head^2
parent
39178b5756
commit
5d5f907e7f
@ -0,0 +1,46 @@
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package label
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// colorPattern is a regexp which can validate label color
|
||||
var colorPattern = regexp.MustCompile("^#?(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})$")
|
||||
|
||||
// Label represents label information loaded from template
|
||||
type Label struct {
|
||||
Name string `yaml:"name"`
|
||||
Color string `yaml:"color"`
|
||||
Description string `yaml:"description,omitempty"`
|
||||
Exclusive bool `yaml:"exclusive,omitempty"`
|
||||
}
|
||||
|
||||
// NormalizeColor normalizes a color string to a 6-character hex code
|
||||
func NormalizeColor(color string) (string, error) {
|
||||
// normalize case
|
||||
color = strings.TrimSpace(strings.ToLower(color))
|
||||
|
||||
// add leading hash
|
||||
if len(color) == 6 || len(color) == 3 {
|
||||
color = "#" + color
|
||||
}
|
||||
|
||||
if !colorPattern.MatchString(color) {
|
||||
return "", fmt.Errorf("bad color code: %s", color)
|
||||
}
|
||||
|
||||
// convert 3-character shorthand into 6-character version
|
||||
if len(color) == 4 {
|
||||
r := color[1]
|
||||
g := color[2]
|
||||
b := color[3]
|
||||
color = fmt.Sprintf("#%c%c%c%c%c%c", r, r, g, g, b, b)
|
||||
}
|
||||
|
||||
return color, nil
|
||||
}
|
@ -0,0 +1,126 @@
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package label
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/options"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type labelFile struct {
|
||||
Labels []*Label `yaml:"labels"`
|
||||
}
|
||||
|
||||
// ErrTemplateLoad represents a "ErrTemplateLoad" kind of error.
|
||||
type ErrTemplateLoad struct {
|
||||
TemplateFile string
|
||||
OriginalError error
|
||||
}
|
||||
|
||||
// IsErrTemplateLoad checks if an error is a ErrTemplateLoad.
|
||||
func IsErrTemplateLoad(err error) bool {
|
||||
_, ok := err.(ErrTemplateLoad)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrTemplateLoad) Error() string {
|
||||
return fmt.Sprintf("Failed to load label template file '%s': %v", err.TemplateFile, err.OriginalError)
|
||||
}
|
||||
|
||||
// GetTemplateFile loads the label template file by given name,
|
||||
// then parses and returns a list of name-color pairs and optionally description.
|
||||
func GetTemplateFile(name string) ([]*Label, error) {
|
||||
data, err := options.GetRepoInitFile("label", name+".yaml")
|
||||
if err == nil && len(data) > 0 {
|
||||
return parseYamlFormat(name+".yaml", data)
|
||||
}
|
||||
|
||||
data, err = options.GetRepoInitFile("label", name+".yml")
|
||||
if err == nil && len(data) > 0 {
|
||||
return parseYamlFormat(name+".yml", data)
|
||||
}
|
||||
|
||||
data, err = options.GetRepoInitFile("label", name)
|
||||
if err != nil {
|
||||
return nil, ErrTemplateLoad{name, fmt.Errorf("GetRepoInitFile: %w", err)}
|
||||
}
|
||||
|
||||
return parseLegacyFormat(name, data)
|
||||
}
|
||||
|
||||
func parseYamlFormat(name string, data []byte) ([]*Label, error) {
|
||||
lf := &labelFile{}
|
||||
|
||||
if err := yaml.Unmarshal(data, lf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Validate label data and fix colors
|
||||
for _, l := range lf.Labels {
|
||||
l.Color = strings.TrimSpace(l.Color)
|
||||
if len(l.Name) == 0 || len(l.Color) == 0 {
|
||||
return nil, ErrTemplateLoad{name, errors.New("label name and color are required fields")}
|
||||
}
|
||||
color, err := NormalizeColor(l.Color)
|
||||
if err != nil {
|
||||
return nil, ErrTemplateLoad{name, fmt.Errorf("bad HTML color code '%s' in label: %s", l.Color, l.Name)}
|
||||
}
|
||||
l.Color = color
|
||||
}
|
||||
|
||||
return lf.Labels, nil
|
||||
}
|
||||
|
||||
func parseLegacyFormat(name string, data []byte) ([]*Label, error) {
|
||||
lines := strings.Split(string(data), "\n")
|
||||
list := make([]*Label, 0, len(lines))
|
||||
for i := 0; i < len(lines); i++ {
|
||||
line := strings.TrimSpace(lines[i])
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
parts, description, _ := strings.Cut(line, ";")
|
||||
|
||||
color, name, ok := strings.Cut(parts, " ")
|
||||
if !ok {
|
||||
return nil, ErrTemplateLoad{name, fmt.Errorf("line is malformed: %s", line)}
|
||||
}
|
||||
|
||||
color, err := NormalizeColor(color)
|
||||
if err != nil {
|
||||
return nil, ErrTemplateLoad{name, fmt.Errorf("bad HTML color code '%s' in line: %s", color, line)}
|
||||
}
|
||||
|
||||
list = append(list, &Label{
|
||||
Name: strings.TrimSpace(name),
|
||||
Color: color,
|
||||
Description: strings.TrimSpace(description),
|
||||
})
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// LoadFormatted loads the labels' list of a template file as a string separated by comma
|
||||
func LoadFormatted(name string) (string, error) {
|
||||
var buf strings.Builder
|
||||
list, err := GetTemplateFile(name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for i := 0; i < len(list); i++ {
|
||||
if i > 0 {
|
||||
buf.WriteString(", ")
|
||||
}
|
||||
buf.WriteString(list[i].Name)
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package label
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestYamlParser(t *testing.T) {
|
||||
data := []byte(`labels:
|
||||
- name: priority/low
|
||||
exclusive: true
|
||||
color: "#0000ee"
|
||||
description: "Low priority"
|
||||
- name: priority/medium
|
||||
exclusive: true
|
||||
color: "0e0"
|
||||
description: "Medium priority"
|
||||
- name: priority/high
|
||||
exclusive: true
|
||||
color: "#ee0000"
|
||||
description: "High priority"
|
||||
- name: type/bug
|
||||
color: "#f00"
|
||||
description: "Bug"`)
|
||||
|
||||
labels, err := parseYamlFormat("test", data)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, labels, 4)
|
||||
assert.Equal(t, "priority/low", labels[0].Name)
|
||||
assert.True(t, labels[0].Exclusive)
|
||||
assert.Equal(t, "#0000ee", labels[0].Color)
|
||||
assert.Equal(t, "Low priority", labels[0].Description)
|
||||
assert.Equal(t, "priority/medium", labels[1].Name)
|
||||
assert.True(t, labels[1].Exclusive)
|
||||
assert.Equal(t, "#00ee00", labels[1].Color)
|
||||
assert.Equal(t, "Medium priority", labels[1].Description)
|
||||
assert.Equal(t, "priority/high", labels[2].Name)
|
||||
assert.True(t, labels[2].Exclusive)
|
||||
assert.Equal(t, "#ee0000", labels[2].Color)
|
||||
assert.Equal(t, "High priority", labels[2].Description)
|
||||
assert.Equal(t, "type/bug", labels[3].Name)
|
||||
assert.False(t, labels[3].Exclusive)
|
||||
assert.Equal(t, "#ff0000", labels[3].Color)
|
||||
assert.Equal(t, "Bug", labels[3].Description)
|
||||
}
|
||||
|
||||
func TestLegacyParser(t *testing.T) {
|
||||
data := []byte(`#ee0701 bug ; Something is not working
|
||||
#cccccc duplicate ; This issue or pull request already exists
|
||||
#84b6eb enhancement`)
|
||||
|
||||
labels, err := parseLegacyFormat("test", data)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, labels, 3)
|
||||
assert.Equal(t, "bug", labels[0].Name)
|
||||
assert.False(t, labels[0].Exclusive)
|
||||
assert.Equal(t, "#ee0701", labels[0].Color)
|
||||
assert.Equal(t, "Something is not working", labels[0].Description)
|
||||
assert.Equal(t, "duplicate", labels[1].Name)
|
||||
assert.False(t, labels[1].Exclusive)
|
||||
assert.Equal(t, "#cccccc", labels[1].Color)
|
||||
assert.Equal(t, "This issue or pull request already exists", labels[1].Description)
|
||||
assert.Equal(t, "enhancement", labels[2].Name)
|
||||
assert.False(t, labels[2].Exclusive)
|
||||
assert.Equal(t, "#84b6eb", labels[2].Color)
|
||||
assert.Empty(t, labels[2].Description)
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package options
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
// GetRepoInitFile returns repository init files
|
||||
func GetRepoInitFile(tp, name string) ([]byte, error) {
|
||||
cleanedName := strings.TrimLeft(path.Clean("/"+name), "/")
|
||||
relPath := path.Join("options", tp, cleanedName)
|
||||
|
||||
// Use custom file when available.
|
||||
customPath := path.Join(setting.CustomPath, relPath)
|
||||
isFile, err := util.IsFile(customPath)
|
||||
if err != nil {
|
||||
log.Error("Unable to check if %s is a file. Error: %v", customPath, err)
|
||||
}
|
||||
if isFile {
|
||||
return os.ReadFile(customPath)
|
||||
}
|
||||
|
||||
switch tp {
|
||||
case "readme":
|
||||
return Readme(cleanedName)
|
||||
case "gitignore":
|
||||
return Gitignore(cleanedName)
|
||||
case "license":
|
||||
return License(cleanedName)
|
||||
case "label":
|
||||
return Labels(cleanedName)
|
||||
default:
|
||||
return []byte{}, fmt.Errorf("Invalid init file type")
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
labels:
|
||||
- name: "Kind/Bug"
|
||||
color: ee0701
|
||||
description: Something is not working
|
||||
- name: "Kind/Feature"
|
||||
color: 0288d1
|
||||
description: New functionality
|
||||
- name: "Kind/Enhancement"
|
||||
color: 84b6eb
|
||||
description: Improve existing functionality
|
||||
- name: "Kind/Security"
|
||||
color: 9c27b0
|
||||
description: This is security issue
|
||||
- name: "Kind/Testing"
|
||||
color: 795548
|
||||
description: Issue or pull request related to testing
|
||||
- name: "Kind/Breaking"
|
||||
color: c62828
|
||||
description: Breaking change that won't be backward compatible
|
||||
- name: "Kind/Documentation"
|
||||
color: 37474f
|
||||
description: Documentation changes
|
||||
- name: "Reviewed/Duplicate"
|
||||
exclusive: true
|
||||
color: 616161
|
||||
description: This issue or pull request already exists
|
||||
- name: "Reviewed/Invalid"
|
||||
exclusive: true
|
||||
color: 546e7a
|
||||
description: Invalid issue
|
||||
- name: "Reviewed/Confirmed"
|
||||
exclusive: true
|
||||
color: 795548
|
||||
description: Issue has been confirmed
|
||||
- name: "Reviewed/Won't Fix"
|
||||
exclusive: true
|
||||
color: eeeeee
|
||||
description: This issue won't be fixed
|
||||
- name: "Status/Need More Info"
|
||||
exclusive: true
|
||||
color: 424242
|
||||
description: Feedback is required to reproduce issue or to continue work
|
||||
- name: "Status/Blocked"
|
||||
exclusive: true
|
||||
color: 880e4f
|
||||
description: Something is blocking this issue or pull request
|
||||
- name: "Status/Abandoned"
|
||||
exclusive: true
|
||||
color: "222222"
|
||||
description: Somebody has started to work on this but abandoned work
|
||||
- name: "Priority/Critical"
|
||||
exclusive: true
|
||||
color: b71c1c
|
||||
description: The priority is critical
|
||||
priority: critical
|
||||
- name: "Priority/High"
|
||||
exclusive: true
|
||||
color: d32f2f
|
||||
description: The priority is high
|
||||
priority: high
|
||||
- name: "Priority/Medium"
|
||||
exclusive: true
|
||||
color: e64a19
|
||||
description: The priority is medium
|
||||
priority: medium
|
||||
- name: "Priority/Low"
|
||||
exclusive: true
|
||||
color: 4caf50
|
||||
description: The priority is low
|
||||
priority: low
|
Loading…
Reference in New Issue