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

This commit is contained in:
Robert Kaussow 2023-02-08 10:13:43 +01:00 committed by GitHub
parent a02b3b3fbc
commit c5514b1d3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 129 additions and 44 deletions

View File

@ -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: c5eae238c271670e51fb5c1b72eaa9d1824225d6c16de564bf8a5cbb57117d61
hmac: 02262ad1b4da3f7b339e9eb03bf0672634bcb0084b95a8eab127e881f0cb774a
...

View File

@ -1,25 +1,92 @@
linters:
enable:
- gosimple
- deadcode
- typecheck
- govet
- errcheck
- staticcheck
- unused
- structcheck
- varcheck
- dupl
- gofmt
- misspell
- gocritic
- bidichk
- ineffassign
- revive
- gofumpt
- depguard
enable-all: false
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
run:
@ -28,4 +95,4 @@ run:
linters-settings:
gofumpt:
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
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

View File

@ -19,6 +19,10 @@ Drone plugin to add comments to GitHub Issues and Pull Requests.
## Usage
{{< hint type=note >}}
Only pull request events are supported by this plugin. Running the plugin on other events will result in an error.
{{< /hint >}}
```YAML
kind: pipeline
name: default

View File

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

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/thegeeklab/drone-github-comment
go 1.19
go 1.20
require (
github.com/google/go-github/v50 v50.0.0

View File

@ -18,32 +18,33 @@ type commentClient struct {
IssueNum int
*github.Client
context.Context
}
func (cc *commentClient) issueComment() error {
var err error
var comment *github.IssueComment
var resp *github.Response
func (cc *commentClient) issueComment(ctx context.Context) error {
var (
err error
comment *github.IssueComment
resp *github.Response
)
ic := &github.IssueComment{
issueComment := &github.IssueComment{
Body: &cc.Message,
}
if cc.Update {
// Append plugin comment ID to comment message so we can search for it later
message := fmt.Sprintf("%s\n<!-- id: %s -->\n", cc.Message, cc.Key)
ic.Body = &message
issueComment.Body = &message
comment, err = cc.comment()
comment, err = cc.comment(ctx)
if err == nil && comment != nil {
_, resp, err = cc.Client.Issues.EditComment(cc.Context, cc.Owner, cc.Repo, int64(*comment.ID), ic)
_, resp, err = cc.Client.Issues.EditComment(ctx, cc.Owner, cc.Repo, *comment.ID, issueComment)
}
}
if err == nil && resp == nil {
_, _, err = cc.Client.Issues.CreateComment(cc.Context, cc.Owner, cc.Repo, cc.IssueNum, ic)
_, _, err = cc.Client.Issues.CreateComment(ctx, cc.Owner, cc.Repo, cc.IssueNum, issueComment)
}
if err != nil {
@ -53,21 +54,23 @@ func (cc *commentClient) issueComment() error {
return nil
}
func (cc *commentClient) comment() (*github.IssueComment, error) {
func (cc *commentClient) comment(ctx context.Context) (*github.IssueComment, error) {
var allComments []*github.IssueComment
opts := &github.IssueListCommentsOptions{}
for {
comments, resp, err := cc.Client.Issues.ListComments(cc.Context, cc.Owner, cc.Repo, cc.IssueNum, opts)
comments, resp, err := cc.Client.Issues.ListComments(ctx, cc.Owner, cc.Repo, cc.IssueNum, opts)
if err != nil {
return nil, err
}
allComments = append(allComments, comments...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
@ -77,5 +80,6 @@ func (cc *commentClient) comment() (*github.IssueComment, error) {
}
}
//nolint:nilnil
return nil, nil
}

View File

@ -3,6 +3,7 @@ package plugin
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"net/url"
"strings"
@ -26,12 +27,14 @@ type Settings struct {
baseURL *url.URL
}
var ErrPluginEventNotSupported = errors.New("event not supported")
// Validate handles the settings validation of the plugin.
func (p *Plugin) Validate() error {
var err error
if p.pipeline.Build.Event != "pull_request" {
return fmt.Errorf("github comment plugin is only available for pull requests")
return fmt.Errorf("%w: %s", ErrPluginEventNotSupported, p.pipeline.Build.Event)
}
if p.settings.Message != "" {
@ -43,6 +46,7 @@ func (p *Plugin) Validate() error {
if !strings.HasSuffix(p.settings.BaseURL, "/") {
p.settings.BaseURL += "/"
}
p.settings.baseURL, err = url.Parse(p.settings.BaseURL)
if err != nil {
return fmt.Errorf("failed to parse base url: %w", err)
@ -53,6 +57,7 @@ func (p *Plugin) Validate() error {
hash := sha256.Sum256([]byte(key))
p.settings.Key = fmt.Sprintf("%x", hash)
}
if p.settings.Key, _, err = readStringOrFile(p.settings.Key); err != nil {
return fmt.Errorf("error while reading %s: %w", p.settings.Key, err)
}
@ -71,9 +76,8 @@ func (p *Plugin) Execute() error {
client := github.NewClient(tc)
client.BaseURL = p.settings.baseURL
cc := commentClient{
commentClient := commentClient{
Client: client,
Context: p.network.Context,
Repo: p.pipeline.Repo.Name,
Owner: p.pipeline.Repo.Owner,
Message: p.settings.Message,
@ -84,10 +88,11 @@ func (p *Plugin) Execute() error {
if p.settings.SkipMissing && !p.settings.IsFile {
logrus.Infof("comment skipped: 'message' is not a valid path or file does not exist while 'skip-missing' is enabled")
return nil
}
err := cc.issueComment()
err := commentClient.issueComment(p.network.Context)
if err != nil {
return fmt.Errorf("failed to create or update comment: %w", err)
}

View File

@ -12,7 +12,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,

View File

@ -5,9 +5,11 @@ import (
)
func readStringOrFile(input string) (string, bool, error) {
//nolint:gomnd
if len(input) > 255 {
return input, false, nil
}
// Check if input is a file path
if _, err := os.Stat(input); err != nil && os.IsNotExist(err) {
// No file found => use input as result
@ -15,9 +17,11 @@ func readStringOrFile(input string) (string, bool, error) {
} else if err != nil {
return "", false, err
}
result, err := os.ReadFile(input)
if err != nil {
return "", true, err
}
return string(result), true, nil
}