diff options
author | Unknwon <u@gogs.io> | 2016-08-29 20:00:06 -0700 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-08-29 20:00:06 -0700 |
commit | 28cf0e6aaa3cf9cc8e29524bd8e400587fc72d22 (patch) | |
tree | 03c2983e1e49650032e0e045c0b5dca90cb850dd /models | |
parent | 92fb30c5260cc067da28ae3cd031b76f7b7976db (diff) | |
download | gitea-28cf0e6aaa3cf9cc8e29524bd8e400587fc72d22.tar.gz gitea-28cf0e6aaa3cf9cc8e29524bd8e400587fc72d22.zip |
#3459 code quality improvement
Diffstat (limited to 'models')
-rw-r--r-- | models/issue_label.go | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/models/issue_label.go b/models/issue_label.go index f70732f6c8..13bf2005f3 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -7,6 +7,7 @@ package models import ( "fmt" "html/template" + "regexp" "strconv" "strings" @@ -17,6 +18,40 @@ import ( "github.com/gogits/gogs/modules/base" ) +var labelColorPattern = regexp.MustCompile("#([a-fA-F0-9]{6})") + +// GetLabelTemplateFile loads the label template file by given name, +// then parses and returns a list of name-color pairs. +func GetLabelTemplateFile(name string) ([][2]string, error) { + data, err := getRepoInitFile("label", name) + if err != nil { + return nil, fmt.Errorf("getRepoInitFile: %v", err) + } + + lines := strings.Split(string(data), "\n") + list := make([][2]string, 0, len(lines)) + for i := 0; i < len(lines); i++ { + line := strings.TrimSpace(lines[i]) + if len(line) == 0 { + continue + } + + fields := strings.SplitN(line, " ", 2) + if len(fields) != 2 { + return nil, fmt.Errorf("line is malformed: %s", line) + } + + if !labelColorPattern.MatchString(fields[0]) { + return nil, fmt.Errorf("bad HTML color code in line: %s", line) + } + + fields[1] = strings.TrimSpace(fields[1]) + list = append(list, [2]string{fields[1], fields[0]}) + } + + return list, nil +} + // Label represents a label of repository for issues. type Label struct { ID int64 `xorm:"pk autoincr"` @@ -62,9 +97,9 @@ func (l *Label) ForegroundColor() template.CSS { return template.CSS("#000") } -// NewLabel creates new label of repository. -func NewLabel(l *Label) error { - _, err := x.Insert(l) +// NewLabels creates new label(s) for a repository. +func NewLabels(labels ...*Label) error { + _, err := x.Insert(labels) return err } |