From 63e61cf83d0ca747cc9f65211e06ba0ff4ecf3d2 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Wed, 8 Feb 2023 10:14:07 +0100 Subject: [PATCH] refactor: add more linters and fix findings (#100) --- .drone.yml | 10 ++-- .golangci.yml | 93 ++++++++++++++++++++++++++++++++------ Makefile | 2 +- cmd/drone-matrix/config.go | 7 +-- cmd/drone-matrix/main.go | 1 + go.mod | 2 +- plugin/impl.go | 25 ++++++---- plugin/plugin.go | 2 +- 8 files changed, 109 insertions(+), 33 deletions(-) diff --git a/.drone.yml b/.drone.yml index 1417e3b..dd7cd57 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,7 +8,7 @@ platform: steps: - name: deps - image: golang:1.19 + image: golang:1.20 commands: - make deps volumes: @@ -16,7 +16,7 @@ steps: path: /go - name: lint - image: golang:1.19 + image: golang:1.20 commands: - make lint volumes: @@ -24,7 +24,7 @@ steps: path: /go - name: test - image: golang:1.19 + image: golang:1.20 commands: - make test volumes: @@ -51,7 +51,7 @@ platform: steps: - name: build - image: techknowlogick/xgo:go-1.19.x + image: techknowlogick/xgo:go-1.20.x commands: - ln -s /drone/src /source - make release @@ -292,6 +292,6 @@ depends_on: --- kind: signature -hmac: e3e2e1002f50516f1eb241ff22916f509547348fbfb819b050f61f772c463291 +hmac: 23c3365c3e827c02c0c57ab6d98e5bca2780471061b7976dad14578982404ad8 ... diff --git a/.golangci.yml b/.golangci.yml index 7bb18ea..2faa799 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,25 +1,92 @@ linters: + enable-all: false + disable-all: true enable: + - errcheck - gosimple - - deadcode - - typecheck - govet - - errcheck + - ineffassign - staticcheck + - typecheck - unused - - structcheck - - varcheck + - asasalint + - asciicheck + - bidichk + - bodyclose + - containedctx + - contextcheck + - decorder + - depguard + - dogsled - dupl + - dupword + - durationcheck + - errchkjson + - errname + - errorlint + - execinquery + - exhaustive + - exportloopref + - forcetypeassert + - ginkgolinter + - gocheckcompilerdirectives + - gochecknoglobals + - gochecknoinits + - gocognit + - goconst + - gocritic + - gocyclo + - godot + - godox + - goerr113 - gofmt + - gofumpt + - goheader + - goimports + - gomnd + - gomoddirectives + - gomodguard + - goprintffuncname + - gosec + - grouper + - importas + - interfacebloat + - ireturn + - lll + - loggercheck + - maintidx + - makezero - misspell - - gocritic - - bidichk - - ineffassign + - musttag + - nakedret + - nestif + - nilerr + - nilnil + - nlreturn + - noctx + - nolintlint + - nonamedreturns + - nosprintfhostport + - prealloc + - predeclared + - promlinter + - reassign - revive - - gofumpt - - depguard - enable-all: false - disable-all: true + # - rowserrcheck + # - sqlclosecheck + # - structcheck + - stylecheck + - tagliatelle + - tenv + - testableexamples + - thelper + - tparallel + - unconvert + - unparam + - usestdlibvars + # - wastedassign + - whitespace + - wsl fast: false run: @@ -28,4 +95,4 @@ run: linters-settings: gofumpt: extra-rules: true - lang-version: "1.18" + lang-version: "1.20" diff --git a/Makefile b/Makefile index f207915..cf6a416 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/cmd/golangci-lint@$(G XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest GENERATE ?= -XGO_VERSION := go-1.19.x +XGO_VERSION := go-1.20.x XGO_TARGETS ?= linux/amd64,linux/arm-6,linux/arm-7,linux/arm64 TARGETOS ?= linux diff --git a/cmd/drone-matrix/config.go b/cmd/drone-matrix/config.go index a9c0246..3553322 100644 --- a/cmd/drone-matrix/config.go +++ b/cmd/drone-matrix/config.go @@ -58,9 +58,10 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { Category: category, }, &cli.StringFlag{ - Name: "template", - EnvVars: []string{"PLUGIN_TEMPLATE", "MATRIX_TEMPLATE"}, - Usage: "message template", + Name: "template", + EnvVars: []string{"PLUGIN_TEMPLATE", "MATRIX_TEMPLATE"}, + Usage: "message template", + //nolint:lll Value: "Build {{ build.Status }} [{{ repo.Owner }}/{{ repo.Name }}#{{ truncate commit.SHA 8 }}]({{ build.Link }}) ({{ build.Branch }}) by {{ commit.Author }}", Destination: &settings.Template, Category: category, diff --git a/cmd/drone-matrix/main.go b/cmd/drone-matrix/main.go index 9acde7d..d2aded4 100644 --- a/cmd/drone-matrix/main.go +++ b/cmd/drone-matrix/main.go @@ -17,6 +17,7 @@ import ( "github.com/urfave/cli/v2" ) +//nolint:gochecknoglobals var ( BuildVersion = "devel" BuildDate = "00000000" diff --git a/go.mod b/go.mod index be069f0..d47d229 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/drone-plugins/drone-matrix -go 1.19 +go 1.20 require ( github.com/joho/godotenv v1.4.0 diff --git a/plugin/impl.go b/plugin/impl.go index f91cae2..19ab291 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -7,6 +7,7 @@ package plugin import ( + "errors" "fmt" "strings" @@ -31,10 +32,13 @@ type Settings struct { Template string } +var ErrAuthSourceNotSet = errors.New("either username and password or userid and accesstoken are required") + // Validate handles the settings validation of the plugin. func (p *Plugin) Validate() error { - if (p.settings.Username == "" || p.settings.Password == "") && (p.settings.UserID == "" || p.settings.AccessToken == "") { - return fmt.Errorf("either username and password or userid and accesstoken are required") + if (p.settings.Username == "" || p.settings.Password == "") && + (p.settings.UserID == "" || p.settings.AccessToken == "") { + return ErrAuthSourceNotSet } return nil @@ -43,6 +47,7 @@ func (p *Plugin) Validate() error { // Execute provides the implementation of the plugin. func (p *Plugin) Execute() error { muid := id.NewUserID(prepend("@", p.settings.UserID), p.settings.Homeserver) + client, err := mautrix.NewClient(p.settings.Homeserver, muid, p.settings.AccessToken) if err != nil { return fmt.Errorf("failed to initialize client: %w", err) @@ -60,7 +65,8 @@ func (p *Plugin) Execute() error { return fmt.Errorf("failed to authenticate user: %w", err) } } - logrus.Info("successfully logged in") + + logrus.Info("logged in successfully") joined, err := client.JoinRoom(prepend("!", p.settings.RoomID), "", nil) if err != nil { @@ -86,19 +92,20 @@ func (p *Plugin) Execute() error { if _, err := client.SendMessageEvent(joined.RoomID, event.EventMessage, content); err != nil { return fmt.Errorf("failed to submit message: %w", err) } + logrus.Info("message sent successfully") return nil } -func prepend(prefix, s string) string { - if s == "" { - return s +func prepend(prefix, input string) string { + if strings.TrimSpace(input) == "" { + return input } - if strings.HasPrefix(s, prefix) { - return s + if strings.HasPrefix(input, prefix) { + return input } - return prefix + s + return prefix + input } diff --git a/plugin/plugin.go b/plugin/plugin.go index b6d0056..68884c7 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -18,7 +18,7 @@ type Plugin struct { } // New initializes a plugin from the given Settings, Pipeline, and Network. -func New(settings Settings, pipeline drone.Pipeline, network drone.Network) drone.Plugin { +func New(settings Settings, pipeline drone.Pipeline, network drone.Network) *Plugin { return &Plugin{ settings: settings, pipeline: pipeline,