Skip to content
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

Make label templates have consistent behavior and priority #23749

Merged
merged 18 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 5 additions & 12 deletions modules/label/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,14 @@ 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) {
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)
data, err := options.Labels(name)
if err != nil {
return nil, ErrTemplateLoad{name, fmt.Errorf("GetRepoInitFile: %w", err)}
return nil, ErrTemplateLoad{name, fmt.Errorf("GetTemplateFile: %w", err)}
}

if strings.HasSuffix(name, ".yaml") || strings.HasSuffix(name, ".yml") {
lunny marked this conversation as resolved.
Show resolved Hide resolved
lunny marked this conversation as resolved.
Show resolved Hide resolved
return parseYamlFormat(name, data)
}
return parseLegacyFormat(name, data)
}

Expand Down
40 changes: 24 additions & 16 deletions modules/repository/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import (
asymkey_service "code.gitea.io/gitea/services/asymkey"
)

type OptionFile struct {
FileName string
DisplayName string
Description string
}

var (
// Gitignores contains the gitiginore files
Gitignores []string
Expand All @@ -37,8 +43,8 @@ var (
// Readmes contains the readme files
Readmes []string

// LabelTemplates contains the label template files and the list of labels for each file
LabelTemplates map[string]string
// LabelTemplateFiles contains the label template files and the list of labels for each file
LabelTemplateFiles []OptionFile
)

// LoadRepoConfig loads the repository config
Expand All @@ -51,14 +57,6 @@ func LoadRepoConfig() {
if err != nil {
log.Fatal("Failed to get %s files: %v", t, err)
}
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)]
}
}
}
customPath := path.Join(setting.CustomPath, "options", t)
isDir, err := util.IsDir(customPath)
if err != nil {
Expand All @@ -71,7 +69,14 @@ func LoadRepoConfig() {
}

for _, f := range customFiles {
if !util.SliceContainsString(files, f, true) {
stat, err := os.Stat(filepath.Join(customPath, f))
if err != nil {
log.Fatal("Failed to stat custom %s files: %v", t, err)
}
if stat.Size() == 0 {
// it's good to give end users a chance to hide builtin options if they put an empty file in their custom directory
files = util.SliceRemoveAllFunc(files, func(s string) bool { return strings.EqualFold(s, f) })
} else if !util.SliceContainsString(files, f, true) {
files = append(files, f)
}
}
Expand All @@ -82,20 +87,23 @@ func LoadRepoConfig() {
Gitignores = typeFiles[0]
Licenses = typeFiles[1]
Readmes = typeFiles[2]
LabelTemplatesFiles := typeFiles[3]
labelTemplatesFiles := typeFiles[3]
sort.Strings(Gitignores)
sort.Strings(Licenses)
sort.Strings(Readmes)
sort.Strings(LabelTemplatesFiles)
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)
}
LabelTemplates[templateFile] = labels
LabelTemplateFiles = append(LabelTemplateFiles, OptionFile{
DisplayName: strings.TrimSuffix(templateFile, filepath.Ext(templateFile)),
FileName: templateFile,
Description: labels,
})
}

// Filter out invalid names and promote preferred licenses.
Expand Down
2 changes: 1 addition & 1 deletion routers/web/org/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,6 @@ func Labels(ctx *context.Context) {
ctx.Data["PageIsOrgSettings"] = true
ctx.Data["PageIsOrgSettingsLabels"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["LabelTemplates"] = repo_module.LabelTemplates
ctx.Data["LabelTemplateFiles"] = repo_module.LabelTemplateFiles
ctx.HTML(http.StatusOK, tplSettingsLabels)
}
2 changes: 1 addition & 1 deletion routers/web/repo/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Labels(ctx *context.Context) {
ctx.Data["PageIsIssueList"] = true
ctx.Data["PageIsLabels"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["LabelTemplates"] = repo_module.LabelTemplates
ctx.Data["LabelTemplateFiles"] = repo_module.LabelTemplateFiles
ctx.HTML(http.StatusOK, tplLabels)
}

Expand Down
4 changes: 2 additions & 2 deletions routers/web/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func Create(ctx *context.Context) {

// Give default value for template to render.
ctx.Data["Gitignores"] = repo_module.Gitignores
ctx.Data["LabelTemplates"] = repo_module.LabelTemplates
ctx.Data["LabelTemplateFiles"] = repo_module.LabelTemplateFiles
ctx.Data["Licenses"] = repo_module.Licenses
ctx.Data["Readmes"] = repo_module.Readmes
ctx.Data["readme"] = "Default"
Expand Down Expand Up @@ -200,7 +200,7 @@ func CreatePost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("new_repo")

ctx.Data["Gitignores"] = repo_module.Gitignores
ctx.Data["LabelTemplates"] = repo_module.LabelTemplates
ctx.Data["LabelTemplateFiles"] = repo_module.LabelTemplateFiles
ctx.Data["Licenses"] = repo_module.Licenses
ctx.Data["Readmes"] = repo_module.Readmes

Expand Down
4 changes: 2 additions & 2 deletions templates/repo/create.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@
<div class="default text">{{.locale.Tr "repo.issue_labels_helper"}}</div>
<div class="menu">
<div class="item" data-value="">{{.locale.Tr "repo.issue_labels_helper"}}</div>
{{range $template, $labels := .LabelTemplates}}
<div class="item" data-value="{{$template}}">{{$template}}<br><i>({{$labels}})</i></div>
{{range .LabelTemplateFiles}}
<div class="item" data-value="{{.FileName}}">{{.DisplayName}}<br><i>({{.Description}})</i></div>
{{end}}
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions templates/repo/issue/labels/label_load_template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<input type="hidden" name="template_name" value="Default">
<div class="default text">{{.locale.Tr "repo.issues.label_templates.helper"}}</div>
<div class="menu">
{{range $template, $labels := .LabelTemplates}}
<div class="item" data-value="{{$template}}">{{$template}}<br><i>({{$labels}})</i></div>
{{range .LabelTemplateFiles}}
<div class="item" data-value="{{.FileName}}">{{.DisplayName}}<br><i>({{.Description}})</i></div>
{{end}}
</div>
{{svg "octicon-triangle-down" 18 "dropdown icon"}}
Expand Down