From 08eab186dbe9881959d519421f43477827a7268e Mon Sep 17 00:00:00 2001 From: justusbunsi <61625851+justusbunsi@users.noreply.github.com> Date: Thu, 24 Jun 2021 10:36:52 +0200 Subject: [PATCH] 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> --- internal/settings/settings.go | 4 ++++ internal/settings/settings_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/internal/settings/settings.go b/internal/settings/settings.go index 943251d..04c9dc4 100644 --- a/internal/settings/settings.go +++ b/internal/settings/settings.go @@ -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 diff --git a/internal/settings/settings_test.go b/internal/settings/settings_test.go index a9e475b..a2631eb 100644 --- a/internal/settings/settings_test.go +++ b/internal/settings/settings_test.go @@ -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") +}