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:
parent
f84442009c
commit
6dfb2dd846
|
@ -19,18 +19,6 @@ gitea:
|
||||||
# # or path to file containing the plain text secret
|
# # or path to file containing the plain text secret
|
||||||
# secretFile: /path/to/gitea/webhook/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 related configuration. Necessary for requesting data from the API and processing the webhook.
|
||||||
sonarqube:
|
sonarqube:
|
||||||
# API endpoint of your SonarQube instance.
|
# API endpoint of your SonarQube instance.
|
||||||
|
@ -53,8 +41,13 @@ sonarqube:
|
||||||
# # or path to file containing the plain text secret
|
# # or path to file containing the plain text secret
|
||||||
# secretFile: /path/to/sonarqube/webhook/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.
|
# List of project mappings to take care of. Webhooks for other projects will be ignored.
|
||||||
# If empty array given, don't filter requests for repositories and proceed them all.
|
# At least one must be configured. Otherwise all webhooks (no matter which source) because the bot cannot map on its own.
|
||||||
projects: []
|
projects:
|
||||||
# - project-1
|
- sonarqube:
|
||||||
# - project-2
|
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
|
||||||
|
|
|
@ -27,19 +27,25 @@ type GiteaConfig struct {
|
||||||
Url string
|
Url string
|
||||||
Token Token
|
Token Token
|
||||||
Webhook Webhook
|
Webhook Webhook
|
||||||
Repositories []GiteaRepository
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type SonarQubeConfig struct {
|
type SonarQubeConfig struct {
|
||||||
Url string
|
Url string
|
||||||
Token Token
|
Token Token
|
||||||
Webhook Webhook
|
Webhook Webhook
|
||||||
Projects []string
|
}
|
||||||
|
|
||||||
|
type Project struct {
|
||||||
|
SonarQube struct {
|
||||||
|
Key string
|
||||||
|
} `mapstructure:"sonarqube"`
|
||||||
|
Gitea GiteaRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Gitea GiteaConfig
|
Gitea GiteaConfig
|
||||||
SonarQube SonarQubeConfig
|
SonarQube SonarQubeConfig
|
||||||
|
Projects []Project
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -59,14 +65,14 @@ func ApplyConfigDefaults() {
|
||||||
viper.SetDefault("gitea.token.file", "")
|
viper.SetDefault("gitea.token.file", "")
|
||||||
viper.SetDefault("gitea.webhook.secret", "")
|
viper.SetDefault("gitea.webhook.secret", "")
|
||||||
viper.SetDefault("gitea.webhook.secretFile", "")
|
viper.SetDefault("gitea.webhook.secretFile", "")
|
||||||
viper.SetDefault("gitea.repositories", []GiteaRepository{})
|
|
||||||
|
|
||||||
viper.SetDefault("sonarqube.url", "")
|
viper.SetDefault("sonarqube.url", "")
|
||||||
viper.SetDefault("sonarqube.token.value", "")
|
viper.SetDefault("sonarqube.token.value", "")
|
||||||
viper.SetDefault("sonarqube.token.file", "")
|
viper.SetDefault("sonarqube.token.file", "")
|
||||||
viper.SetDefault("sonarqube.webhook.secret", "")
|
viper.SetDefault("sonarqube.webhook.secret", "")
|
||||||
viper.SetDefault("sonarqube.webhook.secretFile", "")
|
viper.SetDefault("sonarqube.webhook.secretFile", "")
|
||||||
viper.SetDefault("sonarqube.projects", []string{})
|
|
||||||
|
viper.SetDefault("projects", []Project{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadSecretFile(file string, defaultValue string) (string) {
|
func ReadSecretFile(file string, defaultValue string) (string) {
|
||||||
|
@ -93,6 +99,7 @@ func Load(configPath string) {
|
||||||
var fullConfig struct {
|
var fullConfig struct {
|
||||||
Gitea GiteaConfig
|
Gitea GiteaConfig
|
||||||
SonarQube SonarQubeConfig `mapstructure:"sonarqube"`
|
SonarQube SonarQubeConfig `mapstructure:"sonarqube"`
|
||||||
|
Projects []Project
|
||||||
}
|
}
|
||||||
|
|
||||||
err = viper.Unmarshal(&fullConfig)
|
err = viper.Unmarshal(&fullConfig)
|
||||||
|
@ -102,6 +109,7 @@ func Load(configPath string) {
|
||||||
|
|
||||||
Gitea = fullConfig.Gitea
|
Gitea = fullConfig.Gitea
|
||||||
SonarQube = fullConfig.SonarQube
|
SonarQube = fullConfig.SonarQube
|
||||||
|
Projects = fullConfig.Projects
|
||||||
|
|
||||||
Gitea.Webhook.Secret = ReadSecretFile(Gitea.Webhook.SecretFile, Gitea.Webhook.Secret)
|
Gitea.Webhook.Secret = ReadSecretFile(Gitea.Webhook.SecretFile, Gitea.Webhook.Secret)
|
||||||
Gitea.Token.Value = ReadSecretFile(Gitea.Token.File, Gitea.Token.Value)
|
Gitea.Token.Value = ReadSecretFile(Gitea.Token.File, Gitea.Token.Value)
|
||||||
|
|
|
@ -9,23 +9,25 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var defaultConfigInlineSecrets []byte = []byte(
|
var defaultConfig []byte = []byte(
|
||||||
`gitea:
|
`gitea:
|
||||||
url: https://example.com/gitea
|
url: https://example.com/gitea
|
||||||
token:
|
token:
|
||||||
value: d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565
|
value: d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565
|
||||||
webhook:
|
webhook:
|
||||||
secret: haxxor-gitea-secret
|
secret: haxxor-gitea-secret
|
||||||
repositories:
|
|
||||||
- owner: some-owner
|
|
||||||
name: a-repository-name
|
|
||||||
sonarqube:
|
sonarqube:
|
||||||
url: https://example.com/sonarqube
|
url: https://example.com/sonarqube
|
||||||
token:
|
token:
|
||||||
value: a09eb5785b25bb2cbacf48808a677a0709f02d8e
|
value: a09eb5785b25bb2cbacf48808a677a0709f02d8e
|
||||||
webhook:
|
webhook:
|
||||||
secret: haxxor-sonarqube-secret
|
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) {
|
func WriteConfigFile(t *testing.T, content []byte) {
|
||||||
|
@ -44,13 +46,13 @@ func TestLoadWithMissingFile(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadWithExistingFile(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")
|
assert.NotPanics(t, func() { Load(os.TempDir()) }, "Unexpected panic while reading existing file")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadGiteaStructure(t *testing.T) {
|
func TestLoadGiteaStructure(t *testing.T) {
|
||||||
WriteConfigFile(t, defaultConfigInlineSecrets)
|
WriteConfigFile(t, defaultConfig)
|
||||||
Load(os.TempDir())
|
Load(os.TempDir())
|
||||||
|
|
||||||
expected := GiteaConfig{
|
expected := GiteaConfig{
|
||||||
|
@ -61,12 +63,6 @@ func TestLoadGiteaStructure(t *testing.T) {
|
||||||
Webhook: Webhook{
|
Webhook: Webhook{
|
||||||
Secret: "haxxor-gitea-secret",
|
Secret: "haxxor-gitea-secret",
|
||||||
},
|
},
|
||||||
Repositories: []GiteaRepository{
|
|
||||||
GiteaRepository{
|
|
||||||
Owner: "some-owner",
|
|
||||||
Name: "a-repository-name",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.EqualValues(t, expected, Gitea)
|
assert.EqualValues(t, expected, Gitea)
|
||||||
|
@ -75,7 +71,7 @@ func TestLoadGiteaStructure(t *testing.T) {
|
||||||
func TestLoadGiteaStructureInjectedEnvs(t *testing.T) {
|
func TestLoadGiteaStructureInjectedEnvs(t *testing.T) {
|
||||||
os.Setenv("PRBOT_GITEA_WEBHOOK_SECRET", "injected-webhook-secret")
|
os.Setenv("PRBOT_GITEA_WEBHOOK_SECRET", "injected-webhook-secret")
|
||||||
os.Setenv("PRBOT_GITEA_TOKEN_VALUE", "injected-token")
|
os.Setenv("PRBOT_GITEA_TOKEN_VALUE", "injected-token")
|
||||||
WriteConfigFile(t, defaultConfigInlineSecrets)
|
WriteConfigFile(t, defaultConfig)
|
||||||
Load(os.TempDir())
|
Load(os.TempDir())
|
||||||
|
|
||||||
expected := GiteaConfig{
|
expected := GiteaConfig{
|
||||||
|
@ -86,12 +82,6 @@ func TestLoadGiteaStructureInjectedEnvs(t *testing.T) {
|
||||||
Webhook: Webhook{
|
Webhook: Webhook{
|
||||||
Secret: "injected-webhook-secret",
|
Secret: "injected-webhook-secret",
|
||||||
},
|
},
|
||||||
Repositories: []GiteaRepository{
|
|
||||||
GiteaRepository{
|
|
||||||
Owner: "some-owner",
|
|
||||||
Name: "a-repository-name",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.EqualValues(t, expected, Gitea)
|
assert.EqualValues(t, expected, Gitea)
|
||||||
|
@ -103,7 +93,7 @@ func TestLoadGiteaStructureInjectedEnvs(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadSonarQubeStructure(t *testing.T) {
|
func TestLoadSonarQubeStructure(t *testing.T) {
|
||||||
WriteConfigFile(t, defaultConfigInlineSecrets)
|
WriteConfigFile(t, defaultConfig)
|
||||||
Load(os.TempDir())
|
Load(os.TempDir())
|
||||||
|
|
||||||
expected := SonarQubeConfig{
|
expected := SonarQubeConfig{
|
||||||
|
@ -114,7 +104,6 @@ func TestLoadSonarQubeStructure(t *testing.T) {
|
||||||
Webhook: Webhook{
|
Webhook: Webhook{
|
||||||
Secret: "haxxor-sonarqube-secret",
|
Secret: "haxxor-sonarqube-secret",
|
||||||
},
|
},
|
||||||
Projects: []string{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.EqualValues(t, expected, SonarQube)
|
assert.EqualValues(t, expected, SonarQube)
|
||||||
|
@ -123,7 +112,7 @@ func TestLoadSonarQubeStructure(t *testing.T) {
|
||||||
func TestLoadSonarQubeStructureInjectedEnvs(t *testing.T) {
|
func TestLoadSonarQubeStructureInjectedEnvs(t *testing.T) {
|
||||||
os.Setenv("PRBOT_SONARQUBE_WEBHOOK_SECRET", "injected-webhook-secret")
|
os.Setenv("PRBOT_SONARQUBE_WEBHOOK_SECRET", "injected-webhook-secret")
|
||||||
os.Setenv("PRBOT_SONARQUBE_TOKEN_VALUE", "injected-token")
|
os.Setenv("PRBOT_SONARQUBE_TOKEN_VALUE", "injected-token")
|
||||||
WriteConfigFile(t, defaultConfigInlineSecrets)
|
WriteConfigFile(t, defaultConfig)
|
||||||
Load(os.TempDir())
|
Load(os.TempDir())
|
||||||
|
|
||||||
expected := SonarQubeConfig{
|
expected := SonarQubeConfig{
|
||||||
|
@ -134,7 +123,6 @@ func TestLoadSonarQubeStructureInjectedEnvs(t *testing.T) {
|
||||||
Webhook: Webhook{
|
Webhook: Webhook{
|
||||||
Secret: "injected-webhook-secret",
|
Secret: "injected-webhook-secret",
|
||||||
},
|
},
|
||||||
Projects: []string{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.EqualValues(t, expected, SonarQube)
|
assert.EqualValues(t, expected, SonarQube)
|
||||||
|
@ -163,12 +151,10 @@ func TestLoadStructureWithFileReferenceResolving(t *testing.T) {
|
||||||
url: https://example.com/gitea
|
url: https://example.com/gitea
|
||||||
token:
|
token:
|
||||||
value: fake-gitea-token
|
value: fake-gitea-token
|
||||||
repositories: []
|
|
||||||
sonarqube:
|
sonarqube:
|
||||||
url: https://example.com/sonarqube
|
url: https://example.com/sonarqube
|
||||||
token:
|
token:
|
||||||
value: fake-sonarqube-token
|
value: fake-sonarqube-token
|
||||||
projects: []
|
|
||||||
`))
|
`))
|
||||||
os.Setenv("PRBOT_GITEA_WEBHOOK_SECRETFILE", giteaWebhookSecretFile)
|
os.Setenv("PRBOT_GITEA_WEBHOOK_SECRETFILE", giteaWebhookSecretFile)
|
||||||
os.Setenv("PRBOT_GITEA_TOKEN_FILE", giteaTokenFile)
|
os.Setenv("PRBOT_GITEA_TOKEN_FILE", giteaTokenFile)
|
||||||
|
@ -185,7 +171,6 @@ sonarqube:
|
||||||
Secret: "gitea-totally-secret",
|
Secret: "gitea-totally-secret",
|
||||||
SecretFile: giteaWebhookSecretFile,
|
SecretFile: giteaWebhookSecretFile,
|
||||||
},
|
},
|
||||||
Repositories: []GiteaRepository{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedSonarQube := SonarQubeConfig{
|
expectedSonarQube := SonarQubeConfig{
|
||||||
|
@ -198,7 +183,6 @@ sonarqube:
|
||||||
Secret: "sonarqube-totally-secret",
|
Secret: "sonarqube-totally-secret",
|
||||||
SecretFile: sonarqubeWebhookSecretFile,
|
SecretFile: sonarqubeWebhookSecretFile,
|
||||||
},
|
},
|
||||||
Projects: []string{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Load(os.TempDir())
|
Load(os.TempDir())
|
||||||
|
@ -216,3 +200,22 @@ sonarqube:
|
||||||
os.Unsetenv("PRBOT_SONARQUBE_TOKEN_FILE")
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue