diff --git a/cmd/gitea-sonarqube-bot/main.go b/cmd/gitea-sonarqube-bot/main.go index ba36da2..cdc33c0 100644 --- a/cmd/gitea-sonarqube-bot/main.go +++ b/cmd/gitea-sonarqube-bot/main.go @@ -4,10 +4,10 @@ import ( "os" "path" - "github.com/justusbunsi/gitea-sonarqube-pr-bot/internal/settings" + "gitea-sonarqube-pr-bot/internal/settings" ) -func GetConfigLocation() string { +func getConfigLocation() string { configPath := path.Join("config") if customConfigPath, ok := os.LookupEnv("PRBOT_CONFIG_PATH"); ok { configPath = customConfigPath @@ -17,5 +17,5 @@ func GetConfigLocation() string { } func main() { - settings.Load(GetConfigLocation()) + settings.Load(getConfigLocation()) } diff --git a/cmd/gitea-sonarqube-bot/main_test.go b/cmd/gitea-sonarqube-bot/main_test.go index 6f55850..b48aff4 100644 --- a/cmd/gitea-sonarqube-bot/main_test.go +++ b/cmd/gitea-sonarqube-bot/main_test.go @@ -8,13 +8,13 @@ import ( ) func TestGetConfigLocationWithDefault(t *testing.T) { - assert.Equal(t, "config", GetConfigLocation()) + assert.Equal(t, "config", getConfigLocation()) } func TestGetConfigLocationWithEnvironmentOverride(t *testing.T) { os.Setenv("PRBOT_CONFIG_PATH", "/tmp/") - assert.Equal(t, "/tmp/", GetConfigLocation()) + assert.Equal(t, "/tmp/", getConfigLocation()) t.Cleanup(func() { os.Unsetenv("PRBOT_CONFIG_PATH") diff --git a/go.mod b/go.mod index 3240254..abef9c1 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/justusbunsi/gitea-sonarqube-pr-bot +module gitea-sonarqube-pr-bot go 1.16 diff --git a/internal/settings/settings.go b/internal/settings/settings.go index 208065d..7b4a0f8 100644 --- a/internal/settings/settings.go +++ b/internal/settings/settings.go @@ -8,47 +8,53 @@ import ( "github.com/spf13/viper" ) -type GiteaRepository struct { +type giteaRepository struct { Owner string Name string } -type Token struct { +type token struct { Value string File string } -type Webhook struct { +type webhook struct { Secret string SecretFile string } -type GiteaConfig struct { +type giteaConfig struct { Url string - Token Token - Webhook Webhook + Token token + Webhook webhook } -type SonarQubeConfig struct { +type sonarQubeConfig struct { Url string - Token Token - Webhook Webhook + Token token + Webhook webhook } type Project struct { SonarQube struct { Key string } `mapstructure:"sonarqube"` - Gitea GiteaRepository + Gitea giteaRepository +} + +type fullConfig struct { + Gitea giteaConfig + SonarQube sonarQubeConfig `mapstructure:"sonarqube"` + Projects []Project } var ( - Gitea GiteaConfig - SonarQube SonarQubeConfig + Gitea giteaConfig + SonarQube sonarQubeConfig Projects []Project ) -func ReadSecretFile(file string, defaultValue string) (string) { +func readSecretFile(file string, defaultValue string) (string) { if file == "" { return defaultValue } @@ -61,7 +67,7 @@ func ReadSecretFile(file string, defaultValue string) (string) { return string(content) } -func NewConfigReader() *viper.Viper { +func newConfigReader() *viper.Viper { v := viper.New() v.SetConfigName("config.yaml") v.SetConfigType("yaml") @@ -86,7 +92,7 @@ func NewConfigReader() *viper.Viper { } func Load(configPath string) { - r := NewConfigReader() + r := newConfigReader() r.AddConfigPath(configPath) err := r.ReadInConfig() @@ -94,27 +100,23 @@ func Load(configPath string) { panic(fmt.Errorf("Fatal error while reading config file: %w \n", err)) } - var fullConfig struct { - Gitea GiteaConfig - SonarQube SonarQubeConfig `mapstructure:"sonarqube"` - Projects []Project - } + var configuration fullConfig - err = r.Unmarshal(&fullConfig) + err = r.Unmarshal(&configuration) if err != nil { panic(fmt.Errorf("Unable to load config into struct, %v", err)) } - if len(fullConfig.Projects) == 0 { + if len(configuration.Projects) == 0 { panic("Invalid configuration. At least one project mapping is necessary.") } - Gitea = fullConfig.Gitea - SonarQube = fullConfig.SonarQube - Projects = fullConfig.Projects + Gitea = configuration.Gitea + SonarQube = configuration.SonarQube + Projects = configuration.Projects - Gitea.Webhook.Secret = ReadSecretFile(Gitea.Webhook.SecretFile, Gitea.Webhook.Secret) - Gitea.Token.Value = ReadSecretFile(Gitea.Token.File, Gitea.Token.Value) - SonarQube.Webhook.Secret = ReadSecretFile(SonarQube.Webhook.SecretFile, SonarQube.Webhook.Secret) - SonarQube.Token.Value = ReadSecretFile(SonarQube.Token.File, SonarQube.Token.Value) + Gitea.Webhook.Secret = readSecretFile(Gitea.Webhook.SecretFile, Gitea.Webhook.Secret) + Gitea.Token.Value = readSecretFile(Gitea.Token.File, Gitea.Token.Value) + SonarQube.Webhook.Secret = readSecretFile(SonarQube.Webhook.SecretFile, SonarQube.Webhook.Secret) + SonarQube.Token.Value = readSecretFile(SonarQube.Token.File, SonarQube.Token.Value) } diff --git a/internal/settings/settings_test.go b/internal/settings/settings_test.go index a2631eb..20f8672 100644 --- a/internal/settings/settings_test.go +++ b/internal/settings/settings_test.go @@ -55,12 +55,12 @@ func TestLoadGiteaStructure(t *testing.T) { WriteConfigFile(t, defaultConfig) Load(os.TempDir()) - expected := GiteaConfig{ + expected := giteaConfig{ Url: "https://example.com/gitea", - Token: Token{ + Token: token{ Value: "d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565", }, - Webhook: Webhook{ + Webhook: webhook{ Secret: "haxxor-gitea-secret", }, } @@ -74,12 +74,12 @@ func TestLoadGiteaStructureInjectedEnvs(t *testing.T) { WriteConfigFile(t, defaultConfig) Load(os.TempDir()) - expected := GiteaConfig{ + expected := giteaConfig{ Url: "https://example.com/gitea", - Token: Token{ + Token: token{ Value: "injected-token", }, - Webhook: Webhook{ + Webhook: webhook{ Secret: "injected-webhook-secret", }, } @@ -96,12 +96,12 @@ func TestLoadSonarQubeStructure(t *testing.T) { WriteConfigFile(t, defaultConfig) Load(os.TempDir()) - expected := SonarQubeConfig{ + expected := sonarQubeConfig{ Url: "https://example.com/sonarqube", - Token: Token{ + Token: token{ Value: "a09eb5785b25bb2cbacf48808a677a0709f02d8e", }, - Webhook: Webhook{ + Webhook: webhook{ Secret: "haxxor-sonarqube-secret", }, } @@ -115,12 +115,12 @@ func TestLoadSonarQubeStructureInjectedEnvs(t *testing.T) { WriteConfigFile(t, defaultConfig) Load(os.TempDir()) - expected := SonarQubeConfig{ + expected := sonarQubeConfig{ Url: "https://example.com/sonarqube", - Token: Token{ + Token: token{ Value: "injected-token", }, - Webhook: Webhook{ + Webhook: webhook{ Secret: "injected-webhook-secret", }, } @@ -167,25 +167,25 @@ projects: os.Setenv("PRBOT_SONARQUBE_WEBHOOK_SECRETFILE", sonarqubeWebhookSecretFile) os.Setenv("PRBOT_SONARQUBE_TOKEN_FILE", sonarqubeTokenFile) - expectedGitea := GiteaConfig{ + expectedGitea := giteaConfig{ Url: "https://example.com/gitea", - Token: Token{ + Token: token{ Value: "d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565", File: giteaTokenFile, }, - Webhook: Webhook{ + Webhook: webhook{ Secret: "gitea-totally-secret", SecretFile: giteaWebhookSecretFile, }, } - expectedSonarQube := SonarQubeConfig{ + expectedSonarQube := sonarQubeConfig{ Url: "https://example.com/sonarqube", - Token: Token{ + Token: token{ Value: "a09eb5785b25bb2cbacf48808a677a0709f02d8e", File: sonarqubeTokenFile, }, - Webhook: Webhook{ + Webhook: webhook{ Secret: "sonarqube-totally-secret", SecretFile: sonarqubeWebhookSecretFile, }, @@ -216,7 +216,7 @@ func TestLoadProjectsStructure(t *testing.T) { SonarQube: struct {Key string}{ Key: "gitea-sonarqube-pr-bot", }, - Gitea: GiteaRepository{ + Gitea: giteaRepository{ Owner: "example-organization", Name: "pr-bot", }, diff --git a/internal/webhooks/sonarqube/sonarqube.go b/internal/webhooks/sonarqube/sonarqube.go index 3ea4340..996d85d 100644 --- a/internal/webhooks/sonarqube/sonarqube.go +++ b/internal/webhooks/sonarqube/sonarqube.go @@ -8,7 +8,7 @@ import ( "net/http" "strings" - "github.com/justusbunsi/gitea-sonarqube-pr-bot/internal/settings" + "gitea-sonarqube-pr-bot/internal/settings" ) func inProjectsMapping(p []settings.Project, n string) bool { diff --git a/internal/webhooks/sonarqube/sonarqube_test.go b/internal/webhooks/sonarqube/sonarqube_test.go index 5af8e4b..60fc9f0 100644 --- a/internal/webhooks/sonarqube/sonarqube_test.go +++ b/internal/webhooks/sonarqube/sonarqube_test.go @@ -6,7 +6,7 @@ import ( "net/http/httptest" "testing" - "github.com/justusbunsi/gitea-sonarqube-pr-bot/internal/settings" + "gitea-sonarqube-pr-bot/internal/settings" "github.com/stretchr/testify/assert" ) diff --git a/internal/webhooks/sonarqube/webhook.go b/internal/webhooks/sonarqube/webhook.go index 0e86693..acf34d4 100644 --- a/internal/webhooks/sonarqube/webhook.go +++ b/internal/webhooks/sonarqube/webhook.go @@ -10,22 +10,22 @@ import ( type Webhook struct { ServerUrl string `mapstructure:"serverUrl"` Revision string - Branch Branch - QualityGate QualityGate `mapstructure:"qualityGate"` + Branch branch + QualityGate qualityGate `mapstructure:"qualityGate"` } -type Branch struct { +type branch struct { Name string Type string Url string } -type QualityGate struct { +type qualityGate struct { Status string - Conditions []QualityGateCondition + Conditions []condition } -type QualityGateCondition struct { +type condition struct { Metric string Status string }