changelog/config/config.go
John Olheiser 57e620da5b
Some checks failed
continuous-integration/drone/push Build is failing
Updates and priority changes (#62)
Reviewed-on: #62
Reviewed-by: 6543 <6543@obermui.de>
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: John Olheiser <john+gitea@jolheiser.com>
Co-committed-by: John Olheiser <john+gitea@jolheiser.com>
2022-06-22 09:27:24 +08:00

90 lines
1.9 KiB
Go

// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package config
import (
_ "embed"
"io/ioutil"
"regexp"
"gopkg.in/yaml.v2"
)
//go:embed changelog.example.yml
var DefaultConfig []byte
// Group is a grouping of PRs
type Group struct {
Name string `yaml:"name"`
Labels []string `yaml:"labels"`
Default bool `yaml:"default"`
}
// NameLabel is a Group mapping for a Label to a Name
type NameLabel struct {
Name string
Label string
}
// NameLabels returns a slice of NameLabel
func (g Group) NameLabels() []NameLabel {
nl := make([]NameLabel, 0)
for _, l := range g.Labels {
nl = append(nl, NameLabel{
Name: g.Name,
Label: l,
})
}
return nl
}
// Config is the changelog settings
type Config struct {
Repo string `yaml:"repo"`
Service string `yaml:"service"`
BaseURL string `yaml:"base-url"`
Groups []Group `yaml:"groups"`
SkipLabels string `yaml:"skip-labels"`
SkipRegex *regexp.Regexp `yaml:"-"`
}
// NameLabels returns a slice of NameLabel for each Group, keeping them in order (priority)
func (c Config) NameLabels() []NameLabel {
nl := make([]NameLabel, 0)
for _, g := range c.Groups {
nl = append(nl, g.NameLabels()...)
}
return nl
}
// New Load a config from a path, defaulting to changelog.example.yml
func New(configPath string) (*Config, error) {
var err error
configContent := DefaultConfig
if len(configPath) != 0 {
configContent, err = ioutil.ReadFile(configPath)
if err != nil {
return nil, err
}
}
var cfg *Config
if err = yaml.Unmarshal(configContent, &cfg); err != nil {
return nil, err
}
if len(cfg.SkipLabels) > 0 {
if cfg.SkipRegex, err = regexp.Compile(cfg.SkipLabels); err != nil {
return nil, err
}
}
if cfg.Service == "" {
cfg.Service = "github"
}
return cfg, nil
}