Add namingPattern options to configuration

Signed-off-by: Steven Kriegler <sk.bunsenbrenner@gmail.com>
This commit is contained in:
justusbunsi 2022-06-18 12:55:37 +02:00
parent 4aad9c3e17
commit 1610bbc4a9
No known key found for this signature in database
GPG key ID: 82B29BF2507F9F8B
4 changed files with 110 additions and 0 deletions

View file

@ -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"

View file

@ -0,0 +1,8 @@
package settings
import "regexp"
type PatternConfig struct {
RegExp *regexp.Regexp
Template string
}

View file

@ -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"),
}
}

View file

@ -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")
})
}