From a1990a60f4c8866afdf1cecfb69dc4cc5cd14303 Mon Sep 17 00:00:00 2001 From: justusbunsi <61625851+justusbunsi@users.noreply.github.com> Date: Sun, 3 Oct 2021 17:57:22 +0200 Subject: [PATCH] Format code Signed-off-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com> --- cmd/gitea-sonarqube-bot/main.go | 6 ++-- internal/clients/gitea_sdk/gitea_sdk.go | 1 + .../clients/sonarqube_sdk/sonarqube_sdk.go | 14 +++++----- internal/settings/settings.go | 24 ++++++++-------- internal/settings/settings_test.go | 28 +++++++++---------- internal/settings/token.go | 4 +-- internal/settings/webhook.go | 4 +-- internal/webhook_handler/main.go | 8 +++--- internal/webhook_handler/main_test.go | 2 +- internal/webhook_handler/sonarqube.go | 12 ++++---- internal/webhooks/sonarqube/main_test.go | 2 +- internal/webhooks/sonarqube/webhook.go | 16 +++++------ 12 files changed, 61 insertions(+), 60 deletions(-) diff --git a/cmd/gitea-sonarqube-bot/main.go b/cmd/gitea-sonarqube-bot/main.go index c69dc47..2d8fcd1 100644 --- a/cmd/gitea-sonarqube-bot/main.go +++ b/cmd/gitea-sonarqube-bot/main.go @@ -23,10 +23,10 @@ func main() { settings.Load(getConfigLocation()) app := &cli.App{ - Name: "gitea-sonarqube-pr-bot", - Usage: "Improve your experience with SonarQube and Gitea", + Name: "gitea-sonarqube-pr-bot", + Usage: "Improve your experience with SonarQube and Gitea", Description: `By default, gitea-sonarqube-pr-bot will start running the webserver if no arguments are passed.`, - Action: handler.Serve, + Action: handler.Serve, } err := app.Run(os.Args) diff --git a/internal/clients/gitea_sdk/gitea_sdk.go b/internal/clients/gitea_sdk/gitea_sdk.go index 6d5ea01..5486301 100644 --- a/internal/clients/gitea_sdk/gitea_sdk.go +++ b/internal/clients/gitea_sdk/gitea_sdk.go @@ -3,6 +3,7 @@ package gitea_sdk import ( "fmt" "gitea-sonarqube-pr-bot/internal/settings" + "code.gitea.io/sdk/gitea" ) diff --git a/internal/clients/sonarqube_sdk/sonarqube_sdk.go b/internal/clients/sonarqube_sdk/sonarqube_sdk.go index 5556a4b..b6d683b 100644 --- a/internal/clients/sonarqube_sdk/sonarqube_sdk.go +++ b/internal/clients/sonarqube_sdk/sonarqube_sdk.go @@ -1,11 +1,11 @@ package sonarqube_sdk import ( - "net/http" - "fmt" - "log" "encoding/base64" + "fmt" "io" + "log" + "net/http" "gitea-sonarqube-pr-bot/internal/settings" ) @@ -15,9 +15,9 @@ type SonarQubeSdkInterface interface { } type SonarQubeSdk struct { - client *http.Client + client *http.Client baseUrl string - token string + token string } func (sdk *SonarQubeSdk) GetMeasures(project string, branch string) (string, error) { @@ -43,8 +43,8 @@ func (sdk *SonarQubeSdk) basicAuth() string { func New() *SonarQubeSdk { return &SonarQubeSdk{ - client: &http.Client{}, + client: &http.Client{}, baseUrl: settings.SonarQube.Url, - token: settings.SonarQube.Token.Value, + token: settings.SonarQube.Token.Value, } } diff --git a/internal/settings/settings.go b/internal/settings/settings.go index 7005fd6..bb031a6 100644 --- a/internal/settings/settings.go +++ b/internal/settings/settings.go @@ -9,18 +9,18 @@ import ( type GiteaRepository struct { Owner string - Name string + Name string } type giteaConfig struct { - Url string - Token *token + Url string + Token *token Webhook *webhook } type sonarQubeConfig struct { - Url string - Token *token + Url string + Token *token Webhook *webhook } @@ -32,9 +32,9 @@ type Project struct { } var ( - Gitea giteaConfig + Gitea giteaConfig SonarQube sonarQubeConfig - Projects []Project + Projects []Project ) func newConfigReader() *viper.Viper { @@ -83,16 +83,16 @@ func Load(configPath string) { Projects = projects - errCallback := func(msg string) {panic(msg)} + errCallback := func(msg string) { panic(msg) } Gitea = giteaConfig{ - Url: r.GetString("gitea.url"), - Token: NewToken(r, "gitea", errCallback), + Url: r.GetString("gitea.url"), + Token: NewToken(r, "gitea", errCallback), Webhook: NewWebhook(r, "gitea", errCallback), } SonarQube = sonarQubeConfig{ - Url: r.GetString("sonarqube.url"), - Token: NewToken(r, "sonarqube", errCallback), + Url: r.GetString("sonarqube.url"), + Token: NewToken(r, "sonarqube", errCallback), Webhook: NewWebhook(r, "sonarqube", errCallback), } } diff --git a/internal/settings/settings_test.go b/internal/settings/settings_test.go index fa67a9d..82cd6cd 100644 --- a/internal/settings/settings_test.go +++ b/internal/settings/settings_test.go @@ -10,7 +10,7 @@ import ( ) var defaultConfig []byte = []byte( -`gitea: + `gitea: url: https://example.com/gitea token: value: d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565 @@ -38,7 +38,7 @@ func WriteConfigFile(t *testing.T, content []byte) { os.Remove(config) }) - _ = ioutil.WriteFile(config, content,0444) + _ = ioutil.WriteFile(config, content, 0444) } func TestLoadWithMissingFile(t *testing.T) { @@ -135,19 +135,19 @@ func TestLoadSonarQubeStructureInjectedEnvs(t *testing.T) { func TestLoadStructureWithFileReferenceResolving(t *testing.T) { giteaWebhookSecretFile := path.Join(os.TempDir(), "webhook-secret-gitea") - _ = ioutil.WriteFile(giteaWebhookSecretFile, []byte(`gitea-totally-secret`),0444) + _ = ioutil.WriteFile(giteaWebhookSecretFile, []byte(`gitea-totally-secret`), 0444) giteaTokenFile := path.Join(os.TempDir(), "token-secret-gitea") - _ = ioutil.WriteFile(giteaTokenFile, []byte(`d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565`),0444) + _ = ioutil.WriteFile(giteaTokenFile, []byte(`d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565`), 0444) sonarqubeWebhookSecretFile := path.Join(os.TempDir(), "webhook-secret-sonarqube") - _ = ioutil.WriteFile(sonarqubeWebhookSecretFile, []byte(`sonarqube-totally-secret`),0444) + _ = ioutil.WriteFile(sonarqubeWebhookSecretFile, []byte(`sonarqube-totally-secret`), 0444) sonarqubeTokenFile := path.Join(os.TempDir(), "token-secret-sonarqube") - _ = ioutil.WriteFile(sonarqubeTokenFile, []byte(`a09eb5785b25bb2cbacf48808a677a0709f02d8e`),0444) + _ = ioutil.WriteFile(sonarqubeTokenFile, []byte(`a09eb5785b25bb2cbacf48808a677a0709f02d8e`), 0444) WriteConfigFile(t, []byte( -`gitea: + `gitea: url: https://example.com/gitea token: value: fake-gitea-token @@ -171,10 +171,10 @@ projects: Url: "https://example.com/gitea", Token: &token{ Value: "d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565", - file: giteaTokenFile, + file: giteaTokenFile, }, Webhook: &webhook{ - Secret: "gitea-totally-secret", + Secret: "gitea-totally-secret", secretFile: giteaWebhookSecretFile, }, } @@ -183,10 +183,10 @@ projects: Url: "https://example.com/sonarqube", Token: &token{ Value: "a09eb5785b25bb2cbacf48808a677a0709f02d8e", - file: sonarqubeTokenFile, + file: sonarqubeTokenFile, }, Webhook: &webhook{ - Secret: "sonarqube-totally-secret", + Secret: "sonarqube-totally-secret", secretFile: sonarqubeWebhookSecretFile, }, } @@ -213,12 +213,12 @@ func TestLoadProjectsStructure(t *testing.T) { expectedProjects := []Project{ Project{ - SonarQube: struct {Key string}{ + SonarQube: struct{ Key string }{ Key: "gitea-sonarqube-pr-bot", }, Gitea: GiteaRepository{ Owner: "example-organization", - Name: "pr-bot", + Name: "pr-bot", }, }, } @@ -228,7 +228,7 @@ func TestLoadProjectsStructure(t *testing.T) { func TestLoadProjectsStructureWithNoMapping(t *testing.T) { invalidConfig := []byte( -`gitea: + `gitea: url: https://example.com/gitea token: value: d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565 diff --git a/internal/settings/token.go b/internal/settings/token.go index 447c1dc..be7ce93 100644 --- a/internal/settings/token.go +++ b/internal/settings/token.go @@ -9,7 +9,7 @@ import ( type token struct { Value string - file string + file string } func (t *token) lookupSecret(errCallback func(string)) { @@ -29,7 +29,7 @@ func (t *token) lookupSecret(errCallback func(string)) { func NewToken(v *viper.Viper, confContainer string, errCallback func(string)) *token { t := &token{ Value: v.GetString(fmt.Sprintf("%s.token.value", confContainer)), - file: v.GetString(fmt.Sprintf("%s.token.file", confContainer)), + file: v.GetString(fmt.Sprintf("%s.token.file", confContainer)), } t.lookupSecret(errCallback) diff --git a/internal/settings/webhook.go b/internal/settings/webhook.go index a52d495..64333f4 100644 --- a/internal/settings/webhook.go +++ b/internal/settings/webhook.go @@ -8,7 +8,7 @@ import ( ) type webhook struct { - Secret string + Secret string secretFile string } @@ -28,7 +28,7 @@ func (w *webhook) lookupSecret(errCallback func(string)) { func NewWebhook(v *viper.Viper, confContainer string, errCallback func(string)) *webhook { w := &webhook{ - Secret: v.GetString(fmt.Sprintf("%s.webhook.secret", confContainer)), + Secret: v.GetString(fmt.Sprintf("%s.webhook.secret", confContainer)), secretFile: v.GetString(fmt.Sprintf("%s.webhook.secretFile", confContainer)), } diff --git a/internal/webhook_handler/main.go b/internal/webhook_handler/main.go index 8a2b3b8..db4c492 100644 --- a/internal/webhook_handler/main.go +++ b/internal/webhook_handler/main.go @@ -20,7 +20,7 @@ func Serve(c *cli.Context) error { fmt.Println("Hi! I'm the Gitea-SonarQube-PR bot. At your service.") var wait time.Duration - flag.DurationVar(&wait, "graceful-timeout", time.Second * 15, "the duration for which the server gracefully wait for existing connections to finish") + flag.DurationVar(&wait, "graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish") flag.Parse() r := mux.NewRouter() @@ -30,9 +30,9 @@ func Serve(c *cli.Context) error { Addr: "0.0.0.0:8080", // Good practice to set timeouts to avoid Slowloris attacks. WriteTimeout: time.Second * 15, - ReadTimeout: time.Second * 15, - IdleTimeout: time.Second * 60, - Handler: r, + ReadTimeout: time.Second * 15, + IdleTimeout: time.Second * 60, + Handler: r, } go func() { diff --git a/internal/webhook_handler/main_test.go b/internal/webhook_handler/main_test.go index 9814e29..04d9c4e 100644 --- a/internal/webhook_handler/main_test.go +++ b/internal/webhook_handler/main_test.go @@ -1,8 +1,8 @@ package webhook_handler import ( - "log" "io/ioutil" + "log" "os" "testing" ) diff --git a/internal/webhook_handler/sonarqube.go b/internal/webhook_handler/sonarqube.go index 9aa1cb9..439ed9d 100644 --- a/internal/webhook_handler/sonarqube.go +++ b/internal/webhook_handler/sonarqube.go @@ -2,22 +2,22 @@ package webhook_handler import ( "fmt" - "log" "io" "io/ioutil" + "log" "net/http" "strings" - "gitea-sonarqube-pr-bot/internal/settings" giteaSdk "gitea-sonarqube-pr-bot/internal/clients/gitea_sdk" sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube_sdk" + "gitea-sonarqube-pr-bot/internal/settings" webhook "gitea-sonarqube-pr-bot/internal/webhooks/sonarqube" ) type SonarQubeWebhookHandler struct { fetchDetails func(w *webhook.Webhook) - giteaSdk giteaSdk.GiteaSdkInterface - sqSdk sqSdk.SonarQubeSdkInterface + giteaSdk giteaSdk.GiteaSdkInterface + sqSdk sqSdk.SonarQubeSdkInterface } func (h *SonarQubeWebhookHandler) composeGiteaComment(w *webhook.Webhook) string { @@ -114,7 +114,7 @@ func fetchDetails(w *webhook.Webhook) { func NewSonarQubeWebhookHandler(g giteaSdk.GiteaSdkInterface, sq sqSdk.SonarQubeSdkInterface) *SonarQubeWebhookHandler { return &SonarQubeWebhookHandler{ fetchDetails: fetchDetails, - giteaSdk: g, - sqSdk: sq, + giteaSdk: g, + sqSdk: sq, } } diff --git a/internal/webhooks/sonarqube/main_test.go b/internal/webhooks/sonarqube/main_test.go index f3d2576..40fa850 100644 --- a/internal/webhooks/sonarqube/main_test.go +++ b/internal/webhooks/sonarqube/main_test.go @@ -1,8 +1,8 @@ package sonarqube import ( - "log" "io/ioutil" + "log" "os" "testing" ) diff --git a/internal/webhooks/sonarqube/webhook.go b/internal/webhooks/sonarqube/webhook.go index 5dd0cc9..4ccb3bf 100644 --- a/internal/webhooks/sonarqube/webhook.go +++ b/internal/webhooks/sonarqube/webhook.go @@ -12,19 +12,19 @@ import ( type Webhook struct { ServerUrl string `mapstructure:"serverUrl"` - Revision string - Project struct { - Key string + Revision string + Project struct { + Key string Name string - Url string + Url string } Branch struct { Name string Type string - Url string - } + Url string + } QualityGate struct { - Status string + Status string Conditions []struct { Metric string Status string @@ -42,7 +42,7 @@ func New(raw []byte) (*Webhook, bool) { err := v.Unmarshal(&w) if err != nil { - log.Printf("Error parsing SonarQube webhook: %s", err.Error()) + log.Printf("Error parsing SonarQube webhook: %s", err.Error()) return w, false }