Properly map projects from SQ and Gitea in config

Signed-off-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com>
This commit is contained in:
justusbunsi 2021-06-22 11:27:53 +02:00
parent f84442009c
commit 6dfb2dd846
No known key found for this signature in database
GPG key ID: 990B348ECAC9C7DB
3 changed files with 53 additions and 49 deletions

View file

@ -19,18 +19,6 @@ gitea:
# # or path to file containing the plain text secret
# secretFile: /path/to/gitea/webhook/secret
# List of repository the used Gitea account has access to and shall be handled by the bot. Other repository webhooks
# will be ignored.
# A repository specification contains the owner name and the repository name itself. The owner can be the name of a
# real account or an organization in which the repository is located.
# If empty array given, don't filter requests for repositories and proceed them all.
repositories: []
# - owner: justusbunsi
# name: example-repo
# - owner: my-organization
# name: example-repo
# SonarQube related configuration. Necessary for requesting data from the API and processing the webhook.
sonarqube:
# API endpoint of your SonarQube instance.
@ -53,8 +41,13 @@ sonarqube:
# # or path to file containing the plain text secret
# secretFile: /path/to/sonarqube/webhook/secret
# List of project keys from inside SonarQube that should be handled. Webhooks containing other projects will be ignored.
# If empty array given, don't filter requests for repositories and proceed them all.
projects: []
# - project-1
# - project-2
# List of project mappings to take care of. Webhooks for other projects will be ignored.
# At least one must be configured. Otherwise all webhooks (no matter which source) because the bot cannot map on its own.
projects:
- sonarqube:
key: project-1
# A repository specification contains the owner name and the repository name itself. The owner can be the name of a
# real account or an organization in which the repository is located.
gitea:
owner: justusbunsi
name: example-repo

View file

@ -27,19 +27,25 @@ type GiteaConfig struct {
Url string
Token Token
Webhook Webhook
Repositories []GiteaRepository
}
type SonarQubeConfig struct {
Url string
Token Token
Webhook Webhook
Projects []string
}
type Project struct {
SonarQube struct {
Key string
} `mapstructure:"sonarqube"`
Gitea GiteaRepository
}
var (
Gitea GiteaConfig
SonarQube SonarQubeConfig
Projects []Project
)
func init() {
@ -59,14 +65,14 @@ func ApplyConfigDefaults() {
viper.SetDefault("gitea.token.file", "")
viper.SetDefault("gitea.webhook.secret", "")
viper.SetDefault("gitea.webhook.secretFile", "")
viper.SetDefault("gitea.repositories", []GiteaRepository{})
viper.SetDefault("sonarqube.url", "")
viper.SetDefault("sonarqube.token.value", "")
viper.SetDefault("sonarqube.token.file", "")
viper.SetDefault("sonarqube.webhook.secret", "")
viper.SetDefault("sonarqube.webhook.secretFile", "")
viper.SetDefault("sonarqube.projects", []string{})
viper.SetDefault("projects", []Project{})
}
func ReadSecretFile(file string, defaultValue string) (string) {
@ -93,6 +99,7 @@ func Load(configPath string) {
var fullConfig struct {
Gitea GiteaConfig
SonarQube SonarQubeConfig `mapstructure:"sonarqube"`
Projects []Project
}
err = viper.Unmarshal(&fullConfig)
@ -102,6 +109,7 @@ func Load(configPath string) {
Gitea = fullConfig.Gitea
SonarQube = fullConfig.SonarQube
Projects = fullConfig.Projects
Gitea.Webhook.Secret = ReadSecretFile(Gitea.Webhook.SecretFile, Gitea.Webhook.Secret)
Gitea.Token.Value = ReadSecretFile(Gitea.Token.File, Gitea.Token.Value)

View file

@ -9,23 +9,25 @@ import (
"github.com/stretchr/testify/assert"
)
var defaultConfigInlineSecrets []byte = []byte(
var defaultConfig []byte = []byte(
`gitea:
url: https://example.com/gitea
token:
value: d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565
webhook:
secret: haxxor-gitea-secret
repositories:
- owner: some-owner
name: a-repository-name
sonarqube:
url: https://example.com/sonarqube
token:
value: a09eb5785b25bb2cbacf48808a677a0709f02d8e
webhook:
secret: haxxor-sonarqube-secret
projects: []
projects:
- sonarqube:
key: gitea-sonarqube-pr-bot
gitea:
owner: example-organization
name: pr-bot
`)
func WriteConfigFile(t *testing.T, content []byte) {
@ -44,13 +46,13 @@ func TestLoadWithMissingFile(t *testing.T) {
}
func TestLoadWithExistingFile(t *testing.T) {
WriteConfigFile(t, defaultConfigInlineSecrets)
WriteConfigFile(t, defaultConfig)
assert.NotPanics(t, func() { Load(os.TempDir()) }, "Unexpected panic while reading existing file")
}
func TestLoadGiteaStructure(t *testing.T) {
WriteConfigFile(t, defaultConfigInlineSecrets)
WriteConfigFile(t, defaultConfig)
Load(os.TempDir())
expected := GiteaConfig{
@ -61,12 +63,6 @@ func TestLoadGiteaStructure(t *testing.T) {
Webhook: Webhook{
Secret: "haxxor-gitea-secret",
},
Repositories: []GiteaRepository{
GiteaRepository{
Owner: "some-owner",
Name: "a-repository-name",
},
},
}
assert.EqualValues(t, expected, Gitea)
@ -75,7 +71,7 @@ func TestLoadGiteaStructure(t *testing.T) {
func TestLoadGiteaStructureInjectedEnvs(t *testing.T) {
os.Setenv("PRBOT_GITEA_WEBHOOK_SECRET", "injected-webhook-secret")
os.Setenv("PRBOT_GITEA_TOKEN_VALUE", "injected-token")
WriteConfigFile(t, defaultConfigInlineSecrets)
WriteConfigFile(t, defaultConfig)
Load(os.TempDir())
expected := GiteaConfig{
@ -86,12 +82,6 @@ func TestLoadGiteaStructureInjectedEnvs(t *testing.T) {
Webhook: Webhook{
Secret: "injected-webhook-secret",
},
Repositories: []GiteaRepository{
GiteaRepository{
Owner: "some-owner",
Name: "a-repository-name",
},
},
}
assert.EqualValues(t, expected, Gitea)
@ -103,7 +93,7 @@ func TestLoadGiteaStructureInjectedEnvs(t *testing.T) {
}
func TestLoadSonarQubeStructure(t *testing.T) {
WriteConfigFile(t, defaultConfigInlineSecrets)
WriteConfigFile(t, defaultConfig)
Load(os.TempDir())
expected := SonarQubeConfig{
@ -114,7 +104,6 @@ func TestLoadSonarQubeStructure(t *testing.T) {
Webhook: Webhook{
Secret: "haxxor-sonarqube-secret",
},
Projects: []string{},
}
assert.EqualValues(t, expected, SonarQube)
@ -123,7 +112,7 @@ func TestLoadSonarQubeStructure(t *testing.T) {
func TestLoadSonarQubeStructureInjectedEnvs(t *testing.T) {
os.Setenv("PRBOT_SONARQUBE_WEBHOOK_SECRET", "injected-webhook-secret")
os.Setenv("PRBOT_SONARQUBE_TOKEN_VALUE", "injected-token")
WriteConfigFile(t, defaultConfigInlineSecrets)
WriteConfigFile(t, defaultConfig)
Load(os.TempDir())
expected := SonarQubeConfig{
@ -134,7 +123,6 @@ func TestLoadSonarQubeStructureInjectedEnvs(t *testing.T) {
Webhook: Webhook{
Secret: "injected-webhook-secret",
},
Projects: []string{},
}
assert.EqualValues(t, expected, SonarQube)
@ -163,12 +151,10 @@ func TestLoadStructureWithFileReferenceResolving(t *testing.T) {
url: https://example.com/gitea
token:
value: fake-gitea-token
repositories: []
sonarqube:
url: https://example.com/sonarqube
token:
value: fake-sonarqube-token
projects: []
`))
os.Setenv("PRBOT_GITEA_WEBHOOK_SECRETFILE", giteaWebhookSecretFile)
os.Setenv("PRBOT_GITEA_TOKEN_FILE", giteaTokenFile)
@ -185,7 +171,6 @@ sonarqube:
Secret: "gitea-totally-secret",
SecretFile: giteaWebhookSecretFile,
},
Repositories: []GiteaRepository{},
}
expectedSonarQube := SonarQubeConfig{
@ -198,7 +183,6 @@ sonarqube:
Secret: "sonarqube-totally-secret",
SecretFile: sonarqubeWebhookSecretFile,
},
Projects: []string{},
}
Load(os.TempDir())
@ -216,3 +200,22 @@ sonarqube:
os.Unsetenv("PRBOT_SONARQUBE_TOKEN_FILE")
})
}
func TestLoadProjectsStructure(t *testing.T) {
WriteConfigFile(t, defaultConfig)
Load(os.TempDir())
expectedProjects := []Project{
Project{
SonarQube: struct {Key string}{
Key: "gitea-sonarqube-pr-bot",
},
Gitea: GiteaRepository{
Owner: "example-organization",
Name: "pr-bot",
},
},
}
assert.EqualValues(t, expectedProjects, Projects)
}