Skip to content

Commit

Permalink
always use DisplayName
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Mar 29, 2023
1 parent 77ab786 commit 964d81b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
38 changes: 19 additions & 19 deletions modules/label/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ 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,
// LoadTemplateFile loads the label template file by given file 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)
func LoadTemplateFile(fileName string) ([]*Label, error) {
data, err := options.Labels(fileName)
if err != nil {
return nil, ErrTemplateLoad{name, fmt.Errorf("GetTemplateFile: %w", err)}
return nil, ErrTemplateLoad{fileName, fmt.Errorf("LoadTemplateFile: %w", err)}
}

if strings.HasSuffix(name, ".yaml") || strings.HasSuffix(name, ".yml") {
return parseYamlFormat(name, data)
if strings.HasSuffix(fileName, ".yaml") || strings.HasSuffix(fileName, ".yml") {
return parseYamlFormat(fileName, data)
}
return parseLegacyFormat(name, 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 {
Expand All @@ -58,19 +58,19 @@ 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
}

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++ {
Expand All @@ -81,18 +81,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),
})
Expand All @@ -101,10 +101,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) {
// LoadLabelFileDescription loads the labels' list of a template file as a string separated by comma
func LoadLabelFileDescription(fileName string) (string, error) {
var buf strings.Builder
list, err := GetTemplateFile(name)
list, err := LoadTemplateFile(fileName)
if err != nil {
return "", err
}
Expand Down
3 changes: 1 addition & 2 deletions modules/repository/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/label"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
Expand Down Expand Up @@ -190,7 +189,7 @@ func CreateRepository(doer, u *user_model.User, opts CreateRepoOptions) (*repo_m

// Check if label template exist
if len(opts.IssueLabels) > 0 {
if _, err := label.GetTemplateFile(opts.IssueLabels); err != nil {
if _, err := LoadTemplateLabelsByDisplayName(opts.IssueLabels); err != nil {
return nil, err
}
}
Expand Down
22 changes: 17 additions & 5 deletions modules/repository/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ var (
Readmes []string

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

type optionFileList struct {
Expand Down Expand Up @@ -106,14 +107,16 @@ func LoadRepoConfig() error {
// Load label templates
labelTemplatesFiles := mergeCustomLabels(typeFiles[3])
for _, templateFile := range labelTemplatesFiles {
labels, err := label.LoadFormatted(templateFile)
description, err := label.LoadLabelFileDescription(templateFile)
if err != nil {
return fmt.Errorf("failed to load labels: %w", err)
}
displayName := strings.TrimSuffix(templateFile, filepath.Ext(templateFile))
labelTemplateFileMap[displayName] = templateFile
LabelTemplateFiles = append(LabelTemplateFiles, OptionFile{
DisplayName: strings.TrimSuffix(templateFile, filepath.Ext(templateFile)),
DisplayName: displayName,
FileName: templateFile,
Description: labels,
Description: description,
})
}

Expand Down Expand Up @@ -364,7 +367,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re

// InitializeLabels adds a label set to a repository using a template
func InitializeLabels(ctx context.Context, id int64, labelTemplate string, isOrg bool) error {
list, err := label.GetTemplateFile(labelTemplate)
list, err := LoadTemplateLabelsByDisplayName(labelTemplate)
if err != nil {
return err
}
Expand All @@ -390,3 +393,12 @@ func InitializeLabels(ctx context.Context, id int64, labelTemplate string, isOrg
}
return nil
}

// LoadTemplateLabelsByDisplayName loads a label template by its display name
func LoadTemplateLabelsByDisplayName(name string) ([]*label.Label, error) {
fileName, ok := labelTemplateFileMap[name]
if !ok {
return nil, fmt.Errorf("label template %s not found", name)
}
return label.LoadTemplateFile(fileName)
}
2 changes: 1 addition & 1 deletion templates/repo/create.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
<div class="menu">
<div class="item" data-value="">{{.locale.Tr "repo.issue_labels_helper"}}</div>
{{range .LabelTemplateFiles}}
<div class="item" data-value="{{.FileName}}">{{.DisplayName}}<br><i>({{.Description}})</i></div>
<div class="item" data-value="{{.DisplayName}}">{{.DisplayName}}<br><i>({{.Description}})</i></div>
{{end}}
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/issue/labels/label_load_template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="default text">{{.locale.Tr "repo.issues.label_templates.helper"}}</div>
<div class="menu">
{{range .LabelTemplateFiles}}
<div class="item" data-value="{{.FileName}}">{{.DisplayName}}<br><i>({{.Description}})</i></div>
<div class="item" data-value="{{.DisplayName}}">{{.DisplayName}}<br><i>({{.Description}})</i></div>
{{end}}
</div>
{{svg "octicon-triangle-down" 18 "dropdown icon"}}
Expand Down

0 comments on commit 964d81b

Please sign in to comment.