Add namingPattern options to configuration
Signed-off-by: Steven Kriegler <sk.bunsenbrenner@gmail.com>
This commit is contained in:
parent
4aad9c3e17
commit
1610bbc4a9
|
@ -57,3 +57,17 @@ projects:
|
|||
gitea:
|
||||
owner: justusbunsi
|
||||
name: example-repo
|
||||
|
||||
# Define pull request names from SonarScanner analysis. Default pattern matches the Jenkins Gitea plugin schema.
|
||||
namingPattern:
|
||||
# Regular expression that MUST HAVE exactly ONE GROUP that matches the integer part of the PR.
|
||||
# That integer part is identical to the pull request ID in Gitea.
|
||||
regex: "^PR-(\\d+)$"
|
||||
|
||||
# Valid Go format string. It MUST have one integer placeholder which will be replaced by the pull request ID.
|
||||
# See: https://pkg.go.dev/fmt#hdr-Printing
|
||||
template: "PR-%d"
|
||||
|
||||
# Example for integer-only names
|
||||
# # regex: "^(\\d+)$"
|
||||
# # template: "%d"
|
||||
|
|
8
internal/settings/pattern.go
Normal file
8
internal/settings/pattern.go
Normal file
|
@ -0,0 +1,8 @@
|
|||
package settings
|
||||
|
||||
import "regexp"
|
||||
|
||||
type PatternConfig struct {
|
||||
RegExp *regexp.Regexp
|
||||
Template string
|
||||
}
|
|
@ -2,6 +2,7 @@ package settings
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
@ -11,6 +12,7 @@ var (
|
|||
Gitea GiteaConfig
|
||||
SonarQube SonarQubeConfig
|
||||
Projects []Project
|
||||
Pattern PatternConfig
|
||||
)
|
||||
|
||||
func newConfigReader(configFile string) *viper.Viper {
|
||||
|
@ -33,6 +35,8 @@ func newConfigReader(configFile string) *viper.Viper {
|
|||
v.SetDefault("sonarqube.webhook.secretFile", "")
|
||||
v.SetDefault("sonarqube.additionalMetrics", []string{})
|
||||
v.SetDefault("projects", []Project{})
|
||||
v.SetDefault("namingPattern.regex", `^PR-(\d+)$`)
|
||||
v.SetDefault("namingPattern.template", "PR-%d")
|
||||
|
||||
return v
|
||||
}
|
||||
|
@ -71,4 +75,8 @@ func Load(configFile string) {
|
|||
Webhook: NewWebhook(r.GetString, "sonarqube", errCallback),
|
||||
AdditionalMetrics: r.GetStringSlice("sonarqube.additionalMetrics"),
|
||||
}
|
||||
Pattern = PatternConfig{
|
||||
RegExp: regexp.MustCompile(r.GetString("namingPattern.regex")),
|
||||
Template: r.GetString("namingPattern.template"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -29,6 +30,9 @@ projects:
|
|||
gitea:
|
||||
owner: example-organization
|
||||
name: pr-bot
|
||||
namingPattern:
|
||||
regex: "^PR-(\\d+)$"
|
||||
template: "PR-%d"
|
||||
`)
|
||||
|
||||
func WriteConfigFile(t *testing.T, content []byte) string {
|
||||
|
@ -289,3 +293,79 @@ projects: []
|
|||
|
||||
assert.Panics(t, func() { Load(c) }, "No panic for empty project mapping that is required")
|
||||
}
|
||||
|
||||
func TestLoadNamingPatternStructure(t *testing.T) {
|
||||
c := WriteConfigFile(t, defaultConfig)
|
||||
Load(c)
|
||||
|
||||
expected := PatternConfig{
|
||||
RegExp: regexp.MustCompile(`^PR-(\d+)$`),
|
||||
Template: "PR-%d",
|
||||
}
|
||||
|
||||
assert.EqualValues(t, expected, Pattern)
|
||||
}
|
||||
|
||||
func TestLoadNamingPatternStructureWithInternalDefaults(t *testing.T) {
|
||||
c := WriteConfigFile(t, []byte(
|
||||
`gitea:
|
||||
url: https://example.com/gitea
|
||||
token:
|
||||
value: fake-gitea-token
|
||||
sonarqube:
|
||||
url: https://example.com/sonarqube
|
||||
token:
|
||||
value: fake-sonarqube-token
|
||||
additionalMetrics: "new_security_hotspots"
|
||||
projects:
|
||||
- sonarqube:
|
||||
key: gitea-sonarqube-pr-bot
|
||||
gitea:
|
||||
owner: example-organization
|
||||
name: pr-bot
|
||||
`))
|
||||
Load(c)
|
||||
|
||||
expected := PatternConfig{
|
||||
RegExp: regexp.MustCompile(`^PR-(\d+)$`),
|
||||
Template: "PR-%d",
|
||||
}
|
||||
|
||||
assert.EqualValues(t, expected, Pattern)
|
||||
}
|
||||
|
||||
func TestLoadNamingPatternStructureInjectedEnvs(t *testing.T) {
|
||||
os.Setenv("PRBOT_NAMINGPATTERN_REGEX", "test-(\\d+)-pullrequest")
|
||||
os.Setenv("PRBOT_NAMINGPATTERN_TEMPLATE", "test-%d-pullrequest")
|
||||
c := WriteConfigFile(t, defaultConfig)
|
||||
Load(c)
|
||||
|
||||
expected := PatternConfig{
|
||||
RegExp: regexp.MustCompile(`test-(\d+)-pullrequest`),
|
||||
Template: "test-%d-pullrequest",
|
||||
}
|
||||
|
||||
assert.EqualValues(t, expected, Pattern)
|
||||
|
||||
t.Cleanup(func() {
|
||||
os.Unsetenv("PRBOT_NAMINGPATTERN_REGEX")
|
||||
os.Unsetenv("PRBOT_NAMINGPATTERN_TEMPLATE")
|
||||
})
|
||||
}
|
||||
|
||||
func TestLoadNamingPatternStructureMixedInput(t *testing.T) {
|
||||
os.Setenv("PRBOT_NAMINGPATTERN_REGEX", "test-(\\d+)-pullrequest")
|
||||
c := WriteConfigFile(t, defaultConfig)
|
||||
Load(c)
|
||||
|
||||
expected := PatternConfig{
|
||||
RegExp: regexp.MustCompile(`test-(\d+)-pullrequest`),
|
||||
Template: "PR-%d",
|
||||
}
|
||||
|
||||
assert.EqualValues(t, expected, Pattern)
|
||||
|
||||
t.Cleanup(func() {
|
||||
os.Unsetenv("PRBOT_NAMINGPATTERN_REGEX")
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue