aboutsummaryrefslogtreecommitdiffstats
path: root/modules/label/parser.go
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-04-10 16:44:02 +0800
committerGitHub <noreply@github.com>2023-04-10 16:44:02 +0800
commit4e3348135723dfc03dcc91b196b5da6f20b8a4ea (patch)
tree8734e1d537d93243dd6ba478b9f2890cc8ac0d89 /modules/label/parser.go
parentbb6c670cff1a081d9f5f8bdb3dc91abe5d9e35b9 (diff)
downloadgitea-4e3348135723dfc03dcc91b196b5da6f20b8a4ea.tar.gz
gitea-4e3348135723dfc03dcc91b196b5da6f20b8a4ea.zip
Make label templates have consistent behavior and priority (#23749)
Fix https://github.com/go-gitea/gitea/issues/23715 Other related PRs: * #23717 * #23716 * #23719 This PR is different from others, it tries to resolve the problem fundamentally (and brings more benefits) Although it looks like some more lines are added, actually many new lines are for tests. ---- Before, the code was just "guessing" the file type and try to parse them. <details> ![image](https://user-images.githubusercontent.com/2114189/228002245-57d58e27-1078-4da9-bf42-5bc0b264c6ce.png) </details> This PR: * Always remember the original option file names, and always use correct parser for them. * Another benefit is that we can sort the Label Templates now (before there was a map, its key order is undefined) ![image](https://user-images.githubusercontent.com/2114189/228002432-931b9f18-3908-484b-a36b-04760c9ad132.png)
Diffstat (limited to 'modules/label/parser.go')
-rw-r--r--modules/label/parser.go48
1 files changed, 20 insertions, 28 deletions
diff --git a/modules/label/parser.go b/modules/label/parser.go
index 55bf570de6..511bac823f 100644
--- a/modules/label/parser.go
+++ b/modules/label/parser.go
@@ -30,31 +30,23 @@ func IsErrTemplateLoad(err error) bool {
}
func (err ErrTemplateLoad) Error() string {
- return fmt.Sprintf("Failed to load label template file '%s': %v", err.TemplateFile, err.OriginalError)
+ return fmt.Sprintf("failed to load label template file %q: %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.Labels(name + ".yaml")
- if err == nil && len(data) > 0 {
- return parseYamlFormat(name+".yaml", data)
- }
-
- data, err = options.Labels(name + ".yml")
- if err == nil && len(data) > 0 {
- return parseYamlFormat(name+".yml", data)
- }
-
- data, err = options.Labels(name)
+// LoadTemplateFile loads the label template file by given file name, returns a slice of Label structs.
+func LoadTemplateFile(fileName string) ([]*Label, error) {
+ data, err := options.Labels(fileName)
if err != nil {
- return nil, ErrTemplateLoad{name, fmt.Errorf("GetRepoInitFile: %w", err)}
+ return nil, ErrTemplateLoad{fileName, fmt.Errorf("LoadTemplateFile: %w", err)}
}
- return parseLegacyFormat(name, data)
+ if strings.HasSuffix(fileName, ".yaml") || strings.HasSuffix(fileName, ".yml") {
+ return parseYamlFormat(fileName, data)
+ }
+ return parseLegacyFormat(fileName, data)
}
-func parseYamlFormat(name string, data []byte) ([]*Label, error) {
+func parseYamlFormat(fileName string, data []byte) ([]*Label, error) {
lf := &labelFile{}
if err := yaml.Unmarshal(data, lf); err != nil {
@@ -65,11 +57,11 @@ func parseYamlFormat(name string, data []byte) ([]*Label, error) {
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")}
+ return nil, ErrTemplateLoad{fileName, 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)}
+ return nil, ErrTemplateLoad{fileName, fmt.Errorf("bad HTML color code '%s' in label: %s", l.Color, l.Name)}
}
l.Color = color
}
@@ -77,7 +69,7 @@ func parseYamlFormat(name string, data []byte) ([]*Label, error) {
return lf.Labels, nil
}
-func parseLegacyFormat(name string, data []byte) ([]*Label, error) {
+func parseLegacyFormat(fileName string, data []byte) ([]*Label, error) {
lines := strings.Split(string(data), "\n")
list := make([]*Label, 0, len(lines))
for i := 0; i < len(lines); i++ {
@@ -88,18 +80,18 @@ func parseLegacyFormat(name string, data []byte) ([]*Label, error) {
parts, description, _ := strings.Cut(line, ";")
- color, name, ok := strings.Cut(parts, " ")
+ color, labelName, ok := strings.Cut(parts, " ")
if !ok {
- return nil, ErrTemplateLoad{name, fmt.Errorf("line is malformed: %s", line)}
+ return nil, ErrTemplateLoad{fileName, 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)}
+ return nil, ErrTemplateLoad{fileName, fmt.Errorf("bad HTML color code '%s' in line: %s", color, line)}
}
list = append(list, &Label{
- Name: strings.TrimSpace(name),
+ Name: strings.TrimSpace(labelName),
Color: color,
Description: strings.TrimSpace(description),
})
@@ -108,10 +100,10 @@ func parseLegacyFormat(name string, data []byte) ([]*Label, error) {
return list, nil
}
-// LoadFormatted loads the labels' list of a template file as a string separated by comma
-func LoadFormatted(name string) (string, error) {
+// LoadTemplateDescription loads the labels from a template file, returns a description string by joining each Label.Name with comma
+func LoadTemplateDescription(fileName string) (string, error) {
var buf strings.Builder
- list, err := GetTemplateFile(name)
+ list, err := LoadTemplateFile(fileName)
if err != nil {
return "", err
}