Let the bot panic if no project mapping provided

The bot cannot properly handle webhooks when no projects are configured.
So it is good to not let the bot run with such an invalid configuration.

Signed-off-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com>
This commit is contained in:
justusbunsi 2021-06-24 10:36:52 +02:00
parent 6dfb2dd846
commit 08eab186db
No known key found for this signature in database
GPG key ID: 990B348ECAC9C7DB
2 changed files with 31 additions and 0 deletions

View file

@ -107,6 +107,10 @@ func Load(configPath string) {
panic(fmt.Errorf("Unable to load config into struct, %v", err))
}
if len(fullConfig.Projects) == 0 {
panic("Invalid configuration. At least one project mapping is necessary.")
}
Gitea = fullConfig.Gitea
SonarQube = fullConfig.SonarQube
Projects = fullConfig.Projects

View file

@ -155,6 +155,12 @@ sonarqube:
url: https://example.com/sonarqube
token:
value: fake-sonarqube-token
projects:
- sonarqube:
key: gitea-sonarqube-pr-bot
gitea:
owner: example-organization
name: pr-bot
`))
os.Setenv("PRBOT_GITEA_WEBHOOK_SECRETFILE", giteaWebhookSecretFile)
os.Setenv("PRBOT_GITEA_TOKEN_FILE", giteaTokenFile)
@ -219,3 +225,24 @@ func TestLoadProjectsStructure(t *testing.T) {
assert.EqualValues(t, expectedProjects, Projects)
}
func TestLoadProjectsStructureWithNoMapping(t *testing.T) {
invalidConfig := []byte(
`gitea:
url: https://example.com/gitea
token:
value: d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565
webhook:
secret: haxxor-gitea-secret
sonarqube:
url: https://example.com/sonarqube
token:
value: a09eb5785b25bb2cbacf48808a677a0709f02d8e
webhook:
secret: haxxor-sonarqube-secret
projects: []
`)
WriteConfigFile(t, invalidConfig)
assert.Panics(t, func() { Load(os.TempDir()) }, "No panic for empty project mapping that is required")
}