mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-25 11:22:16 +00:00
Backport #22976 Extract from #11669 and enhancement to #22585 to support exclusive scoped labels in label templates * Move label template functionality to label module * Fix handling of color codes * Add Advanced label template Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
parent
39178b5756
commit
5d5f907e7f
15 changed files with 488 additions and 241 deletions
46
modules/label/label.go
Normal file
46
modules/label/label.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package label
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// colorPattern is a regexp which can validate label color
|
||||
var colorPattern = regexp.MustCompile("^#?(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})$")
|
||||
|
||||
// Label represents label information loaded from template
|
||||
type Label struct {
|
||||
Name string `yaml:"name"`
|
||||
Color string `yaml:"color"`
|
||||
Description string `yaml:"description,omitempty"`
|
||||
Exclusive bool `yaml:"exclusive,omitempty"`
|
||||
}
|
||||
|
||||
// NormalizeColor normalizes a color string to a 6-character hex code
|
||||
func NormalizeColor(color string) (string, error) {
|
||||
// normalize case
|
||||
color = strings.TrimSpace(strings.ToLower(color))
|
||||
|
||||
// add leading hash
|
||||
if len(color) == 6 || len(color) == 3 {
|
||||
color = "#" + color
|
||||
}
|
||||
|
||||
if !colorPattern.MatchString(color) {
|
||||
return "", fmt.Errorf("bad color code: %s", color)
|
||||
}
|
||||
|
||||
// convert 3-character shorthand into 6-character version
|
||||
if len(color) == 4 {
|
||||
r := color[1]
|
||||
g := color[2]
|
||||
b := color[3]
|
||||
color = fmt.Sprintf("#%c%c%c%c%c%c", r, r, g, g, b, b)
|
||||
}
|
||||
|
||||
return color, nil
|
||||
}
|
126
modules/label/parser.go
Normal file
126
modules/label/parser.go
Normal file
|
@ -0,0 +1,126 @@
|
|||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package label
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/options"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type labelFile struct {
|
||||
Labels []*Label `yaml:"labels"`
|
||||
}
|
||||
|
||||
// ErrTemplateLoad represents a "ErrTemplateLoad" kind of error.
|
||||
type ErrTemplateLoad struct {
|
||||
TemplateFile string
|
||||
OriginalError error
|
||||
}
|
||||
|
||||
// IsErrTemplateLoad checks if an error is a ErrTemplateLoad.
|
||||
func IsErrTemplateLoad(err error) bool {
|
||||
_, ok := err.(ErrTemplateLoad)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrTemplateLoad) Error() string {
|
||||
return fmt.Sprintf("Failed to load label template file '%s': %v", err.TemplateFile, err.OriginalError)
|
||||
}
|
||||
|
||||
// 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.GetRepoInitFile("label", name+".yaml")
|
||||
if err == nil && len(data) > 0 {
|
||||
return parseYamlFormat(name+".yaml", data)
|
||||
}
|
||||
|
||||
data, err = options.GetRepoInitFile("label", name+".yml")
|
||||
if err == nil && len(data) > 0 {
|
||||
return parseYamlFormat(name+".yml", data)
|
||||
}
|
||||
|
||||
data, err = options.GetRepoInitFile("label", name)
|
||||
if err != nil {
|
||||
return nil, ErrTemplateLoad{name, fmt.Errorf("GetRepoInitFile: %w", err)}
|
||||
}
|
||||
|
||||
return parseLegacyFormat(name, data)
|
||||
}
|
||||
|
||||
func parseYamlFormat(name string, data []byte) ([]*Label, error) {
|
||||
lf := &labelFile{}
|
||||
|
||||
if err := yaml.Unmarshal(data, lf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Validate label data and fix colors
|
||||
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")}
|
||||
}
|
||||
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)}
|
||||
}
|
||||
l.Color = color
|
||||
}
|
||||
|
||||
return lf.Labels, nil
|
||||
}
|
||||
|
||||
func parseLegacyFormat(name string, data []byte) ([]*Label, error) {
|
||||
lines := strings.Split(string(data), "\n")
|
||||
list := make([]*Label, 0, len(lines))
|
||||
for i := 0; i < len(lines); i++ {
|
||||
line := strings.TrimSpace(lines[i])
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
parts, description, _ := strings.Cut(line, ";")
|
||||
|
||||
color, name, ok := strings.Cut(parts, " ")
|
||||
if !ok {
|
||||
return nil, ErrTemplateLoad{name, 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)}
|
||||
}
|
||||
|
||||
list = append(list, &Label{
|
||||
Name: strings.TrimSpace(name),
|
||||
Color: color,
|
||||
Description: strings.TrimSpace(description),
|
||||
})
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// LoadFormatted loads the labels' list of a template file as a string separated by comma
|
||||
func LoadFormatted(name string) (string, error) {
|
||||
var buf strings.Builder
|
||||
list, err := GetTemplateFile(name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for i := 0; i < len(list); i++ {
|
||||
if i > 0 {
|
||||
buf.WriteString(", ")
|
||||
}
|
||||
buf.WriteString(list[i].Name)
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
72
modules/label/parser_test.go
Normal file
72
modules/label/parser_test.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package label
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestYamlParser(t *testing.T) {
|
||||
data := []byte(`labels:
|
||||
- name: priority/low
|
||||
exclusive: true
|
||||
color: "#0000ee"
|
||||
description: "Low priority"
|
||||
- name: priority/medium
|
||||
exclusive: true
|
||||
color: "0e0"
|
||||
description: "Medium priority"
|
||||
- name: priority/high
|
||||
exclusive: true
|
||||
color: "#ee0000"
|
||||
description: "High priority"
|
||||
- name: type/bug
|
||||
color: "#f00"
|
||||
description: "Bug"`)
|
||||
|
||||
labels, err := parseYamlFormat("test", data)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, labels, 4)
|
||||
assert.Equal(t, "priority/low", labels[0].Name)
|
||||
assert.True(t, labels[0].Exclusive)
|
||||
assert.Equal(t, "#0000ee", labels[0].Color)
|
||||
assert.Equal(t, "Low priority", labels[0].Description)
|
||||
assert.Equal(t, "priority/medium", labels[1].Name)
|
||||
assert.True(t, labels[1].Exclusive)
|
||||
assert.Equal(t, "#00ee00", labels[1].Color)
|
||||
assert.Equal(t, "Medium priority", labels[1].Description)
|
||||
assert.Equal(t, "priority/high", labels[2].Name)
|
||||
assert.True(t, labels[2].Exclusive)
|
||||
assert.Equal(t, "#ee0000", labels[2].Color)
|
||||
assert.Equal(t, "High priority", labels[2].Description)
|
||||
assert.Equal(t, "type/bug", labels[3].Name)
|
||||
assert.False(t, labels[3].Exclusive)
|
||||
assert.Equal(t, "#ff0000", labels[3].Color)
|
||||
assert.Equal(t, "Bug", labels[3].Description)
|
||||
}
|
||||
|
||||
func TestLegacyParser(t *testing.T) {
|
||||
data := []byte(`#ee0701 bug ; Something is not working
|
||||
#cccccc duplicate ; This issue or pull request already exists
|
||||
#84b6eb enhancement`)
|
||||
|
||||
labels, err := parseLegacyFormat("test", data)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, labels, 3)
|
||||
assert.Equal(t, "bug", labels[0].Name)
|
||||
assert.False(t, labels[0].Exclusive)
|
||||
assert.Equal(t, "#ee0701", labels[0].Color)
|
||||
assert.Equal(t, "Something is not working", labels[0].Description)
|
||||
assert.Equal(t, "duplicate", labels[1].Name)
|
||||
assert.False(t, labels[1].Exclusive)
|
||||
assert.Equal(t, "#cccccc", labels[1].Color)
|
||||
assert.Equal(t, "This issue or pull request already exists", labels[1].Description)
|
||||
assert.Equal(t, "enhancement", labels[2].Name)
|
||||
assert.False(t, labels[2].Exclusive)
|
||||
assert.Equal(t, "#84b6eb", labels[2].Color)
|
||||
assert.Empty(t, labels[2].Description)
|
||||
}
|
44
modules/options/repo.go
Normal file
44
modules/options/repo.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package options
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
// GetRepoInitFile returns repository init files
|
||||
func GetRepoInitFile(tp, name string) ([]byte, error) {
|
||||
cleanedName := strings.TrimLeft(path.Clean("/"+name), "/")
|
||||
relPath := path.Join("options", tp, cleanedName)
|
||||
|
||||
// Use custom file when available.
|
||||
customPath := path.Join(setting.CustomPath, relPath)
|
||||
isFile, err := util.IsFile(customPath)
|
||||
if err != nil {
|
||||
log.Error("Unable to check if %s is a file. Error: %v", customPath, err)
|
||||
}
|
||||
if isFile {
|
||||
return os.ReadFile(customPath)
|
||||
}
|
||||
|
||||
switch tp {
|
||||
case "readme":
|
||||
return Readme(cleanedName)
|
||||
case "gitignore":
|
||||
return Gitignore(cleanedName)
|
||||
case "license":
|
||||
return License(cleanedName)
|
||||
case "label":
|
||||
return Labels(cleanedName)
|
||||
default:
|
||||
return []byte{}, fmt.Errorf("Invalid init file type")
|
||||
}
|
||||
}
|
|
@ -23,6 +23,7 @@ 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"
|
||||
|
@ -189,7 +190,7 @@ func CreateRepository(doer, u *user_model.User, opts CreateRepoOptions) (*repo_m
|
|||
|
||||
// Check if label template exist
|
||||
if len(opts.IssueLabels) > 0 {
|
||||
if _, err := GetLabelTemplateFile(opts.IssueLabels); err != nil {
|
||||
if _, err := label.GetTemplateFile(opts.IssueLabels); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ import (
|
|||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/label"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/options"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
@ -40,114 +41,6 @@ var (
|
|||
LabelTemplates map[string]string
|
||||
)
|
||||
|
||||
// ErrIssueLabelTemplateLoad represents a "ErrIssueLabelTemplateLoad" kind of error.
|
||||
type ErrIssueLabelTemplateLoad struct {
|
||||
TemplateFile string
|
||||
OriginalError error
|
||||
}
|
||||
|
||||
// IsErrIssueLabelTemplateLoad checks if an error is a ErrIssueLabelTemplateLoad.
|
||||
func IsErrIssueLabelTemplateLoad(err error) bool {
|
||||
_, ok := err.(ErrIssueLabelTemplateLoad)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrIssueLabelTemplateLoad) Error() string {
|
||||
return fmt.Sprintf("Failed to load label template file '%s': %v", err.TemplateFile, err.OriginalError)
|
||||
}
|
||||
|
||||
// GetRepoInitFile returns repository init files
|
||||
func GetRepoInitFile(tp, name string) ([]byte, error) {
|
||||
cleanedName := strings.TrimLeft(path.Clean("/"+name), "/")
|
||||
relPath := path.Join("options", tp, cleanedName)
|
||||
|
||||
// Use custom file when available.
|
||||
customPath := path.Join(setting.CustomPath, relPath)
|
||||
isFile, err := util.IsFile(customPath)
|
||||
if err != nil {
|
||||
log.Error("Unable to check if %s is a file. Error: %v", customPath, err)
|
||||
}
|
||||
if isFile {
|
||||
return os.ReadFile(customPath)
|
||||
}
|
||||
|
||||
switch tp {
|
||||
case "readme":
|
||||
return options.Readme(cleanedName)
|
||||
case "gitignore":
|
||||
return options.Gitignore(cleanedName)
|
||||
case "license":
|
||||
return options.License(cleanedName)
|
||||
case "label":
|
||||
return options.Labels(cleanedName)
|
||||
default:
|
||||
return []byte{}, fmt.Errorf("Invalid init file type")
|
||||
}
|
||||
}
|
||||
|
||||
// GetLabelTemplateFile loads the label template file by given name,
|
||||
// then parses and returns a list of name-color pairs and optionally description.
|
||||
func GetLabelTemplateFile(name string) ([][3]string, error) {
|
||||
data, err := GetRepoInitFile("label", name)
|
||||
if err != nil {
|
||||
return nil, ErrIssueLabelTemplateLoad{name, fmt.Errorf("GetRepoInitFile: %w", err)}
|
||||
}
|
||||
|
||||
lines := strings.Split(string(data), "\n")
|
||||
list := make([][3]string, 0, len(lines))
|
||||
for i := 0; i < len(lines); i++ {
|
||||
line := strings.TrimSpace(lines[i])
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
parts := strings.SplitN(line, ";", 2)
|
||||
|
||||
fields := strings.SplitN(parts[0], " ", 2)
|
||||
if len(fields) != 2 {
|
||||
return nil, ErrIssueLabelTemplateLoad{name, fmt.Errorf("line is malformed: %s", line)}
|
||||
}
|
||||
|
||||
color := strings.Trim(fields[0], " ")
|
||||
if len(color) == 6 {
|
||||
color = "#" + color
|
||||
}
|
||||
if !issues_model.LabelColorPattern.MatchString(color) {
|
||||
return nil, ErrIssueLabelTemplateLoad{name, fmt.Errorf("bad HTML color code in line: %s", line)}
|
||||
}
|
||||
|
||||
var description string
|
||||
|
||||
if len(parts) > 1 {
|
||||
description = strings.TrimSpace(parts[1])
|
||||
}
|
||||
|
||||
fields[1] = strings.TrimSpace(fields[1])
|
||||
list = append(list, [3]string{fields[1], color, description})
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func loadLabels(labelTemplate string) ([]string, error) {
|
||||
list, err := GetLabelTemplateFile(labelTemplate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
labels := make([]string, len(list))
|
||||
for i := 0; i < len(list); i++ {
|
||||
labels[i] = list[i][0]
|
||||
}
|
||||
return labels, nil
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// LoadRepoConfig loads the repository config
|
||||
func LoadRepoConfig() {
|
||||
// Load .gitignore and license files and readme templates.
|
||||
|
@ -158,6 +51,14 @@ 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 {
|
||||
|
@ -190,7 +91,7 @@ func LoadRepoConfig() {
|
|||
// Load label templates
|
||||
LabelTemplates = make(map[string]string)
|
||||
for _, templateFile := range LabelTemplatesFiles {
|
||||
labels, err := LoadLabelsFormatted(templateFile)
|
||||
labels, err := label.LoadFormatted(templateFile)
|
||||
if err != nil {
|
||||
log.Error("Failed to load labels: %v", err)
|
||||
}
|
||||
|
@ -235,7 +136,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
|
|||
}
|
||||
|
||||
// README
|
||||
data, err := GetRepoInitFile("readme", opts.Readme)
|
||||
data, err := options.GetRepoInitFile("readme", opts.Readme)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetRepoInitFile[%s]: %w", opts.Readme, err)
|
||||
}
|
||||
|
@ -263,7 +164,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
|
|||
var buf bytes.Buffer
|
||||
names := strings.Split(opts.Gitignores, ",")
|
||||
for _, name := range names {
|
||||
data, err = GetRepoInitFile("gitignore", name)
|
||||
data, err = options.GetRepoInitFile("gitignore", name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetRepoInitFile[%s]: %w", name, err)
|
||||
}
|
||||
|
@ -281,7 +182,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
|
|||
|
||||
// LICENSE
|
||||
if len(opts.License) > 0 {
|
||||
data, err = GetRepoInitFile("license", opts.License)
|
||||
data, err = options.GetRepoInitFile("license", opts.License)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetRepoInitFile[%s]: %w", opts.License, err)
|
||||
}
|
||||
|
@ -443,7 +344,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 := GetLabelTemplateFile(labelTemplate)
|
||||
list, err := label.GetTemplateFile(labelTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -451,9 +352,10 @@ func InitializeLabels(ctx context.Context, id int64, labelTemplate string, isOrg
|
|||
labels := make([]*issues_model.Label, len(list))
|
||||
for i := 0; i < len(list); i++ {
|
||||
labels[i] = &issues_model.Label{
|
||||
Name: list[i][0],
|
||||
Description: list[i][2],
|
||||
Color: list[i][1],
|
||||
Name: list[i].Name,
|
||||
Exclusive: list[i].Exclusive,
|
||||
Description: list[i].Description,
|
||||
Color: list[i].Color,
|
||||
}
|
||||
if isOrg {
|
||||
labels[i].OrgID = id
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue