From c074ee9db2afad2bdf6d79d9500dae05311cd1fb Mon Sep 17 00:00:00 2001 From: Oscar Costa Date: Wed, 4 Dec 2019 12:29:59 -0800 Subject: [PATCH 1/4] Showing the list of labels of template files #7812 --- models/issue_label.go | 19 +++++++++++++++++++ models/repo.go | 14 ++++++++++---- templates/repo/create.tmpl | 4 ++-- templates/repo/issue/labels.tmpl | 4 ++-- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/models/issue_label.go b/models/issue_label.go index 4318406af71e6..c38931c6e72c1 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -132,6 +132,25 @@ func (label *Label) ForegroundColor() template.CSS { return template.CSS("#000") } +func loadLabels(labelTemplate string) []string { + list, err := GetLabelTemplateFile(labelTemplate) + if err != nil { + //return ErrIssueLabelTemplateLoad{labelTemplate, err} + } + + labels := make([]string, len(list)) + for i := 0; i < len(list); i++ { + labels[i] = list[i][0] + } + return labels +} + +// Load the labels' list of a template file as a string separated by comma +func LoadLabelsFormatted(labelTemplate string) string { + labels := loadLabels(labelTemplate) + return strings.Join(labels, ", ") +} + func initalizeLabels(e Engine, repoID int64, labelTemplate string) error { list, err := GetLabelTemplateFile(labelTemplate) if err != nil { diff --git a/models/repo.go b/models/repo.go index 347c9095794f3..d6118e788b51c 100644 --- a/models/repo.go +++ b/models/repo.go @@ -63,8 +63,8 @@ var ( // Readmes contains the readme files Readmes []string - // LabelTemplates contains the label template files - LabelTemplates []string + // LabelTemplates contains the label template files and the list of labels for each file + LabelTemplates map[string]string // ItemsPerPage maximum items per page in forks, watchers and stars of a repo ItemsPerPage = 40 @@ -99,11 +99,17 @@ func loadRepoConfig() { Gitignores = typeFiles[0] Licenses = typeFiles[1] Readmes = typeFiles[2] - LabelTemplates = typeFiles[3] + LabelTemplatesFiles := typeFiles[3] sort.Strings(Gitignores) sort.Strings(Licenses) sort.Strings(Readmes) - sort.Strings(LabelTemplates) + sort.Strings(LabelTemplatesFiles) + + // Load label templates + LabelTemplates = make(map[string]string) + for _, templateFile := range LabelTemplatesFiles { + LabelTemplates[templateFile] = LoadLabelsFormatted(templateFile) + } // Filter out invalid names and promote preferred licenses. sortedLicenses := make([]string, 0, len(Licenses)) diff --git a/templates/repo/create.tmpl b/templates/repo/create.tmpl index c71c3ab5a6afd..b63af09cecef6 100644 --- a/templates/repo/create.tmpl +++ b/templates/repo/create.tmpl @@ -109,8 +109,8 @@
{{.i18n.Tr "repo.issue_labels_helper"}}
diff --git a/templates/repo/issue/labels.tmpl b/templates/repo/issue/labels.tmpl index c3bfc9ccc6742..32a5c2a4ef141 100644 --- a/templates/repo/issue/labels.tmpl +++ b/templates/repo/issue/labels.tmpl @@ -79,8 +79,8 @@
{{.i18n.Tr "repo.issues.label_templates.helper"}}
From 262cf7c548dc33feda7ff1f4d713be1ad4f0f591 Mon Sep 17 00:00:00 2001 From: Oscar Costa Date: Wed, 4 Dec 2019 13:35:36 -0800 Subject: [PATCH 2/4] Returning and logging errors when loading labels --- models/issue_label.go | 12 ++++++------ models/repo.go | 6 +++++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/models/issue_label.go b/models/issue_label.go index c38931c6e72c1..a50d7afd27b01 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -132,23 +132,23 @@ func (label *Label) ForegroundColor() template.CSS { return template.CSS("#000") } -func loadLabels(labelTemplate string) []string { +func loadLabels(labelTemplate string) ([]string, error) { list, err := GetLabelTemplateFile(labelTemplate) if err != nil { - //return ErrIssueLabelTemplateLoad{labelTemplate, err} + return nil, ErrIssueLabelTemplateLoad{labelTemplate, err} } labels := make([]string, len(list)) for i := 0; i < len(list); i++ { labels[i] = list[i][0] } - return labels + return labels, nil } // Load the labels' list of a template file as a string separated by comma -func LoadLabelsFormatted(labelTemplate string) string { - labels := loadLabels(labelTemplate) - return strings.Join(labels, ", ") +func LoadLabelsFormatted(labelTemplate string) (string, error) { + labels, err := loadLabels(labelTemplate) + return strings.Join(labels, ", "), err } func initalizeLabels(e Engine, repoID int64, labelTemplate string) error { diff --git a/models/repo.go b/models/repo.go index d6118e788b51c..3b7072bb21e85 100644 --- a/models/repo.go +++ b/models/repo.go @@ -108,7 +108,11 @@ func loadRepoConfig() { // Load label templates LabelTemplates = make(map[string]string) for _, templateFile := range LabelTemplatesFiles { - LabelTemplates[templateFile] = LoadLabelsFormatted(templateFile) + labels, err := LoadLabelsFormatted(templateFile) + if err != nil { + log.Fatal("Failed to load labels: %v", err) + } + LabelTemplates[templateFile] = labels } // Filter out invalid names and promote preferred licenses. From 45ec0d55e8a47a3275dde2c5004483a0fa277770 Mon Sep 17 00:00:00 2001 From: Oscar Costa Date: Wed, 4 Dec 2019 14:20:28 -0800 Subject: [PATCH 3/4] Commenting public method --- models/issue_label.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/issue_label.go b/models/issue_label.go index a50d7afd27b01..1ea0ed85cc14f 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -145,7 +145,7 @@ func loadLabels(labelTemplate string) ([]string, error) { return labels, nil } -// Load the labels' list of a template file as a string separated by comma +// LoadLabelsFormatted loads the labels' list of a template file as a string separated by comma func LoadLabelsFormatted(labelTemplate string) (string, error) { labels, err := loadLabels(labelTemplate) return strings.Join(labels, ", "), err From 68c831c7336ef78eb5be1c791881a85508a8c559 Mon Sep 17 00:00:00 2001 From: Oscar Costa Date: Thu, 5 Dec 2019 09:35:58 -0800 Subject: [PATCH 4/4] Change log level in case of error loading labels. --- models/repo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/repo.go b/models/repo.go index 3b7072bb21e85..c26538aa6bee7 100644 --- a/models/repo.go +++ b/models/repo.go @@ -110,7 +110,7 @@ func loadRepoConfig() { for _, templateFile := range LabelTemplatesFiles { labels, err := LoadLabelsFormatted(templateFile) if err != nil { - log.Fatal("Failed to load labels: %v", err) + log.Error("Failed to load labels: %v", err) } LabelTemplates[templateFile] = labels }