mirror of
https://github.com/thegeeklab/drone-github-comment.git
synced 2024-11-26 20:40:35 +00:00
refactor: add more linters and fix findings (#168)
This commit is contained in:
parent
a02b3b3fbc
commit
c5514b1d3b
10
.drone.yml
10
.drone.yml
@ -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: c5eae238c271670e51fb5c1b72eaa9d1824225d6c16de564bf8a5cbb57117d61
|
hmac: 02262ad1b4da3f7b339e9eb03bf0672634bcb0084b95a8eab127e881f0cb774a
|
||||||
|
|
||||||
...
|
...
|
||||||
|
107
.golangci.yml
107
.golangci.yml
@ -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"
|
||||||
|
2
Makefile
2
Makefile
@ -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
|
||||||
|
@ -19,6 +19,10 @@ Drone plugin to add comments to GitHub Issues and Pull Requests.
|
|||||||
|
|
||||||
## Usage
|
## 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
|
```YAML
|
||||||
kind: pipeline
|
kind: pipeline
|
||||||
name: default
|
name: default
|
||||||
|
@ -11,6 +11,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
2
go.mod
@ -1,6 +1,6 @@
|
|||||||
module github.com/thegeeklab/drone-github-comment
|
module github.com/thegeeklab/drone-github-comment
|
||||||
|
|
||||||
go 1.19
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/google/go-github/v50 v50.0.0
|
github.com/google/go-github/v50 v50.0.0
|
||||||
|
@ -18,32 +18,33 @@ type commentClient struct {
|
|||||||
IssueNum int
|
IssueNum int
|
||||||
|
|
||||||
*github.Client
|
*github.Client
|
||||||
context.Context
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cc *commentClient) issueComment() error {
|
func (cc *commentClient) issueComment(ctx context.Context) error {
|
||||||
var err error
|
var (
|
||||||
var comment *github.IssueComment
|
err error
|
||||||
var resp *github.Response
|
comment *github.IssueComment
|
||||||
|
resp *github.Response
|
||||||
|
)
|
||||||
|
|
||||||
ic := &github.IssueComment{
|
issueComment := &github.IssueComment{
|
||||||
Body: &cc.Message,
|
Body: &cc.Message,
|
||||||
}
|
}
|
||||||
|
|
||||||
if cc.Update {
|
if cc.Update {
|
||||||
// Append plugin comment ID to comment message so we can search for it later
|
// 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)
|
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 {
|
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 {
|
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 {
|
if err != nil {
|
||||||
@ -53,21 +54,23 @@ func (cc *commentClient) issueComment() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cc *commentClient) comment() (*github.IssueComment, error) {
|
func (cc *commentClient) comment(ctx context.Context) (*github.IssueComment, error) {
|
||||||
var allComments []*github.IssueComment
|
var allComments []*github.IssueComment
|
||||||
|
|
||||||
opts := &github.IssueListCommentsOptions{}
|
opts := &github.IssueListCommentsOptions{}
|
||||||
|
|
||||||
for {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
allComments = append(allComments, comments...)
|
allComments = append(allComments, comments...)
|
||||||
|
|
||||||
if resp.NextPage == 0 {
|
if resp.NextPage == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
opts.Page = resp.NextPage
|
opts.Page = resp.NextPage
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,5 +80,6 @@ func (cc *commentClient) comment() (*github.IssueComment, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//nolint:nilnil
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package plugin
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
@ -26,12 +27,14 @@ type Settings struct {
|
|||||||
baseURL *url.URL
|
baseURL *url.URL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ErrPluginEventNotSupported = errors.New("event not supported")
|
||||||
|
|
||||||
// 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 {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if p.pipeline.Build.Event != "pull_request" {
|
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 != "" {
|
if p.settings.Message != "" {
|
||||||
@ -43,6 +46,7 @@ func (p *Plugin) Validate() error {
|
|||||||
if !strings.HasSuffix(p.settings.BaseURL, "/") {
|
if !strings.HasSuffix(p.settings.BaseURL, "/") {
|
||||||
p.settings.BaseURL += "/"
|
p.settings.BaseURL += "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
p.settings.baseURL, err = url.Parse(p.settings.BaseURL)
|
p.settings.baseURL, err = url.Parse(p.settings.BaseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to parse base url: %w", err)
|
return fmt.Errorf("failed to parse base url: %w", err)
|
||||||
@ -53,6 +57,7 @@ func (p *Plugin) Validate() error {
|
|||||||
hash := sha256.Sum256([]byte(key))
|
hash := sha256.Sum256([]byte(key))
|
||||||
p.settings.Key = fmt.Sprintf("%x", hash)
|
p.settings.Key = fmt.Sprintf("%x", hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.settings.Key, _, err = readStringOrFile(p.settings.Key); err != nil {
|
if p.settings.Key, _, err = readStringOrFile(p.settings.Key); err != nil {
|
||||||
return fmt.Errorf("error while reading %s: %w", p.settings.Key, err)
|
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 := github.NewClient(tc)
|
||||||
client.BaseURL = p.settings.baseURL
|
client.BaseURL = p.settings.baseURL
|
||||||
|
|
||||||
cc := commentClient{
|
commentClient := commentClient{
|
||||||
Client: client,
|
Client: client,
|
||||||
Context: p.network.Context,
|
|
||||||
Repo: p.pipeline.Repo.Name,
|
Repo: p.pipeline.Repo.Name,
|
||||||
Owner: p.pipeline.Repo.Owner,
|
Owner: p.pipeline.Repo.Owner,
|
||||||
Message: p.settings.Message,
|
Message: p.settings.Message,
|
||||||
@ -84,10 +88,11 @@ func (p *Plugin) Execute() error {
|
|||||||
|
|
||||||
if p.settings.SkipMissing && !p.settings.IsFile {
|
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")
|
logrus.Infof("comment skipped: 'message' is not a valid path or file does not exist while 'skip-missing' is enabled")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err := cc.issueComment()
|
err := commentClient.issueComment(p.network.Context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create or update comment: %w", err)
|
return fmt.Errorf("failed to create or update comment: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,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,
|
||||||
|
@ -5,9 +5,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func readStringOrFile(input string) (string, bool, error) {
|
func readStringOrFile(input string) (string, bool, error) {
|
||||||
|
//nolint:gomnd
|
||||||
if len(input) > 255 {
|
if len(input) > 255 {
|
||||||
return input, false, nil
|
return input, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if input is a file path
|
// Check if input is a file path
|
||||||
if _, err := os.Stat(input); err != nil && os.IsNotExist(err) {
|
if _, err := os.Stat(input); err != nil && os.IsNotExist(err) {
|
||||||
// No file found => use input as result
|
// No file found => use input as result
|
||||||
@ -15,9 +17,11 @@ func readStringOrFile(input string) (string, bool, error) {
|
|||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return "", false, err
|
return "", false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := os.ReadFile(input)
|
result, err := os.ReadFile(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", true, err
|
return "", true, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(result), true, nil
|
return string(result), true, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user