From 5f19f5400142bac017af2ee1a073c61cacf06d72 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 26 Mar 2023 18:45:15 +0100 Subject: [PATCH] Ensure that label filename extensions are removed when setting up repo options The previous code did not remove the .yaml/.yml extension from label files within the customPath. This would lead to duplicate lists of labels. Fix #23715 Close #23717 Signed-off-by: Andrew Thornton --- modules/label/parser.go | 1 + modules/repository/init.go | 36 ++++++++++++++++++++---------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/modules/label/parser.go b/modules/label/parser.go index 55bf570de6b9..f50f4ca4742a 100644 --- a/modules/label/parser.go +++ b/modules/label/parser.go @@ -36,6 +36,7 @@ func (err ErrTemplateLoad) Error() string { // 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) { + // Always check if .yaml or .yml exists and prefer those data, err := options.Labels(name + ".yaml") if err == nil && len(data) > 0 { return parseYamlFormat(name+".yaml", data) diff --git a/modules/repository/init.go b/modules/repository/init.go index f9a33cd4f68c..32753bef6162 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -45,7 +45,18 @@ var ( func LoadRepoConfig() { // Load .gitignore and license files and readme templates. types := []string{"gitignore", "license", "readme", "label"} - typeFiles := make([][]string, 4) + + removeExtension := func(f string) string { + ext := strings.ToLower(filepath.Ext(f)) + if ext == ".yaml" || ext == ".yml" { + return f[:len(f)-len(ext)] + } + return f + } + + labelTemplatesFiles := []string{} + typeFiles := []*[]string{&Gitignores, &Licenses, &Readmes, &labelTemplatesFiles} + for i, t := range types { files, err := options.Dir(t) if err != nil { @@ -53,10 +64,7 @@ func LoadRepoConfig() { } if t == "label" { for i, f := range files { - ext := strings.ToLower(filepath.Ext(f)) - if ext == ".yaml" || ext == ".yml" { - files[i] = f[:len(f)-len(ext)] - } + files[i] = removeExtension(f) } } customPath := path.Join(setting.CustomPath, "options", t) @@ -71,26 +79,22 @@ func LoadRepoConfig() { } for _, f := range customFiles { + if t == "label" { + f = removeExtension(f) + } + if !util.SliceContainsString(files, f, true) { files = append(files, f) } } } - typeFiles[i] = files + sort.Strings(files) + *typeFiles[i] = files } - Gitignores = typeFiles[0] - Licenses = typeFiles[1] - Readmes = typeFiles[2] - LabelTemplatesFiles := typeFiles[3] - sort.Strings(Gitignores) - sort.Strings(Licenses) - sort.Strings(Readmes) - sort.Strings(LabelTemplatesFiles) - // Load label templates LabelTemplates = make(map[string]string) - for _, templateFile := range LabelTemplatesFiles { + for _, templateFile := range labelTemplatesFiles { labels, err := label.LoadFormatted(templateFile) if err != nil { log.Error("Failed to load labels: %v", err)