refactor: add more linters and fix findings (#100)

This commit is contained in:
Robert Kaussow 2023-02-08 10:14:07 +01:00 committed by GitHub
parent 8a36a90279
commit 63e61cf83d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 116 additions and 40 deletions

View File

@ -8,7 +8,7 @@ platform:
steps: steps:
- name: deps - name: deps
image: golang:1.19 image: golang:1.20
commands: commands:
- make deps - make deps
volumes: volumes:
@ -16,7 +16,7 @@ steps:
path: /go path: /go
- name: lint - name: lint
image: golang:1.19 image: golang:1.20
commands: commands:
- make lint - make lint
volumes: volumes:
@ -24,7 +24,7 @@ steps:
path: /go path: /go
- name: test - name: test
image: golang:1.19 image: golang:1.20
commands: commands:
- make test - make test
volumes: volumes:
@ -51,7 +51,7 @@ platform:
steps: steps:
- name: build - name: build
image: techknowlogick/xgo:go-1.19.x image: techknowlogick/xgo:go-1.20.x
commands: commands:
- ln -s /drone/src /source - ln -s /drone/src /source
- make release - make release
@ -292,6 +292,6 @@ depends_on:
--- ---
kind: signature kind: signature
hmac: e3e2e1002f50516f1eb241ff22916f509547348fbfb819b050f61f772c463291 hmac: 23c3365c3e827c02c0c57ab6d98e5bca2780471061b7976dad14578982404ad8
... ...

View File

@ -1,25 +1,92 @@
linters: linters:
enable:
- gosimple
- deadcode
- typecheck
- govet
- errcheck
- staticcheck
- unused
- structcheck
- varcheck
- dupl
- gofmt
- misspell
- gocritic
- bidichk
- ineffassign
- revive
- gofumpt
- depguard
enable-all: false enable-all: false
disable-all: true disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- 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
- musttag
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- nonamedreturns
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
# - rowserrcheck
# - sqlclosecheck
# - structcheck
- stylecheck
- tagliatelle
- tenv
- testableexamples
- thelper
- tparallel
- unconvert
- unparam
- usestdlibvars
# - wastedassign
- whitespace
- wsl
fast: false fast: false
run: run:
@ -28,4 +95,4 @@ run:
linters-settings: linters-settings:
gofumpt: gofumpt:
extra-rules: true extra-rules: true
lang-version: "1.18" lang-version: "1.20"

View File

@ -19,7 +19,7 @@ GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/cmd/golangci-lint@$(G
XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest
GENERATE ?= 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 XGO_TARGETS ?= linux/amd64,linux/arm-6,linux/arm-7,linux/arm64
TARGETOS ?= linux TARGETOS ?= linux

View File

@ -61,6 +61,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Name: "template", Name: "template",
EnvVars: []string{"PLUGIN_TEMPLATE", "MATRIX_TEMPLATE"}, EnvVars: []string{"PLUGIN_TEMPLATE", "MATRIX_TEMPLATE"},
Usage: "message 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 }}", Value: "Build {{ build.Status }} [{{ repo.Owner }}/{{ repo.Name }}#{{ truncate commit.SHA 8 }}]({{ build.Link }}) ({{ build.Branch }}) by {{ commit.Author }}",
Destination: &settings.Template, Destination: &settings.Template,
Category: category, Category: category,

View File

@ -17,6 +17,7 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
//nolint:gochecknoglobals
var ( var (
BuildVersion = "devel" BuildVersion = "devel"
BuildDate = "00000000" BuildDate = "00000000"

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/drone-plugins/drone-matrix module github.com/drone-plugins/drone-matrix
go 1.19 go 1.20
require ( require (
github.com/joho/godotenv v1.4.0 github.com/joho/godotenv v1.4.0

View File

@ -7,6 +7,7 @@
package plugin package plugin
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
@ -31,10 +32,13 @@ type Settings struct {
Template string Template string
} }
var ErrAuthSourceNotSet = errors.New("either username and password or userid and accesstoken are required")
// Validate handles the settings validation of the plugin. // Validate handles the settings validation of the plugin.
func (p *Plugin) Validate() error { func (p *Plugin) Validate() error {
if (p.settings.Username == "" || p.settings.Password == "") && (p.settings.UserID == "" || p.settings.AccessToken == "") { if (p.settings.Username == "" || p.settings.Password == "") &&
return fmt.Errorf("either username and password or userid and accesstoken are required") (p.settings.UserID == "" || p.settings.AccessToken == "") {
return ErrAuthSourceNotSet
} }
return nil return nil
@ -43,6 +47,7 @@ func (p *Plugin) Validate() error {
// Execute provides the implementation of the plugin. // Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error { func (p *Plugin) Execute() error {
muid := id.NewUserID(prepend("@", p.settings.UserID), p.settings.Homeserver) muid := id.NewUserID(prepend("@", p.settings.UserID), p.settings.Homeserver)
client, err := mautrix.NewClient(p.settings.Homeserver, muid, p.settings.AccessToken) client, err := mautrix.NewClient(p.settings.Homeserver, muid, p.settings.AccessToken)
if err != nil { if err != nil {
return fmt.Errorf("failed to initialize client: %w", err) 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) 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) joined, err := client.JoinRoom(prepend("!", p.settings.RoomID), "", nil)
if err != nil { if err != nil {
@ -86,19 +92,20 @@ func (p *Plugin) Execute() error {
if _, err := client.SendMessageEvent(joined.RoomID, event.EventMessage, content); err != nil { if _, err := client.SendMessageEvent(joined.RoomID, event.EventMessage, content); err != nil {
return fmt.Errorf("failed to submit message: %w", err) return fmt.Errorf("failed to submit message: %w", err)
} }
logrus.Info("message sent successfully") logrus.Info("message sent successfully")
return nil return nil
} }
func prepend(prefix, s string) string { func prepend(prefix, input string) string {
if s == "" { if strings.TrimSpace(input) == "" {
return s return input
} }
if strings.HasPrefix(s, prefix) { if strings.HasPrefix(input, prefix) {
return s return input
} }
return prefix + s return prefix + input
} }

View File

@ -18,7 +18,7 @@ type Plugin struct {
} }
// New initializes a plugin from the given Settings, Pipeline, and Network. // 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{ return &Plugin{
settings: settings, settings: settings,
pipeline: pipeline, pipeline: pipeline,