Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable more go linters [ready to split] #2960

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,51 +124,57 @@ linters-settings:
linters:
disable-all: true
enable:
- bidichk
- errcheck
- gofmt
- goimports
- gosimple
- govet
- ineffassign
- misspell
- revive
- staticcheck
- typecheck
- unused
- whitespace
- gofumpt
- errorlint
- forbidigo
- zerologlint
- depguard
- asciicheck
- bidichk
- bodyclose
- contextcheck
- depguard
- dogsled
- durationcheck
- errcheck
- errchkjson
- errorlint
- forbidigo
- forcetypeassert
- gochecknoinits
- goconst
- gocritic
- gofmt
- gofumpt
- goheader
- goimports
- gomnd
- gomoddirectives
- gomodguard
- goprintffuncname
- gosimple
- govet
- importas
- ineffassign
- makezero
- misspell
- nolintlint
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- stylecheck
- tenv
- typecheck
- unconvert
- unparam
- unused
- wastedassign
- whitespace
- zerologlint

run:
timeout: 15m

issues:
exclude-rules:
# gin force us to use string as context key
- path: server/store/context.go
- path: 'server/store/context.go'
linters:
- staticcheck
- revive
Expand All @@ -186,3 +192,11 @@ issues:
- path: '_test.go'
linters:
- forcetypeassert

- path: 'cmd/agent/flags.go|cmd/server/flags.go|pipeline/backend/kubernetes/flags.go|_test.go'
linters:
- gomnd

- path: '_test.go'
linters:
- goconst
6 changes: 3 additions & 3 deletions agent/rpc/auth_client_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"context"
"time"

"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc/proto"

"google.golang.org/grpc"

"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc/proto"
)

type AuthClient struct {
Expand All @@ -40,7 +40,7 @@ func NewAuthGrpcClient(conn *grpc.ClientConn, agentToken string, agentID int64)
}

func (c *AuthClient) Auth() (string, int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) //nolint: gomnd
defer cancel()

req := &proto.AuthRequest{
Expand Down
4 changes: 2 additions & 2 deletions agent/rpc/client_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (c *client) Close() error {

func (c *client) newBackOff() backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.MaxInterval = 10 * time.Second
b.InitialInterval = 10 * time.Millisecond
b.MaxInterval = 10 * time.Second //nolint: gomnd
b.InitialInterval = 10 * time.Millisecond //nolint: gomnd
return b
}

Expand Down
16 changes: 9 additions & 7 deletions agent/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,13 @@ func (r *Runner) Run(runnerCtx context.Context) error {
state := rpc.State{}
state.Started = time.Now().Unix()

err = r.client.Init(ctxmeta, work.ID, state)
err = r.client.Init(ctxmeta, work.ID, state) //nolint:contextcheck
if err != nil {
logger.Error().Err(err).Msg("pipeline initialization failed")
}

var uploads sync.WaitGroup
//nolint:contextcheck
err = pipeline.New(work.Config,
pipeline.WithContext(workflowCtx),
pipeline.WithTaskUUID(fmt.Sprint(work.ID)),
Expand All @@ -157,16 +158,17 @@ func (r *Runner) Run(runnerCtx context.Context) error {

if canceled.IsSet() {
state.Error = ""
state.ExitCode = 137
state.ExitCode = pipeline.ExitCodeKilled
} else if err != nil {
pExitError := &pipeline.ExitError{}
if errors.As(err, &pExitError) {
switch {
case errors.As(err, &pExitError):
state.ExitCode = pExitError.Code
} else if errors.Is(err, pipeline.ErrCancel) {
case errors.Is(err, pipeline.ErrCancel):
state.Error = ""
state.ExitCode = 137
state.ExitCode = pipeline.ExitCodeKilled
canceled.SetTo(true)
} else {
default:
state.ExitCode = 1
state.Error = err.Error()
}
Expand All @@ -187,7 +189,7 @@ func (r *Runner) Run(runnerCtx context.Context) error {
Int("exit_code", state.ExitCode).
Msg("updating pipeline status")

if err := r.client.Done(ctxmeta, work.ID, state); err != nil {
if err := r.client.Done(ctxmeta, work.ID, state); err != nil { //nolint:contextcheck
logger.Error().Err(err).Msg("updating pipeline status failed")
} else {
logger.Debug().Msg("updating pipeline status complete")
Expand Down
6 changes: 3 additions & 3 deletions agent/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (r *Runner) createTracer(ctxmeta context.Context, logger zerolog.Logger, wo
// TODO: find better way to update this state and move it to pipeline to have the same env in cli-exec
state.Pipeline.Step.Environment["CI_MACHINE"] = r.hostname

state.Pipeline.Step.Environment["CI_PIPELINE_STATUS"] = "success"
state.Pipeline.Step.Environment["CI_PIPELINE_STATUS"] = string(pipeline.StatusSuccess)
state.Pipeline.Step.Environment["CI_PIPELINE_STARTED"] = strconv.FormatInt(state.Pipeline.Time, 10)
state.Pipeline.Step.Environment["CI_PIPELINE_FINISHED"] = strconv.FormatInt(time.Now().Unix(), 10)

Expand All @@ -79,8 +79,8 @@ func (r *Runner) createTracer(ctxmeta context.Context, logger zerolog.Logger, wo
state.Pipeline.Step.Environment["CI_SYSTEM_PLATFORM"] = runtime.GOOS + "/" + runtime.GOARCH

if state.Pipeline.Error != nil {
state.Pipeline.Step.Environment["CI_PIPELINE_STATUS"] = "failure"
state.Pipeline.Step.Environment["CI_STEP_STATUS"] = "failure"
state.Pipeline.Step.Environment["CI_PIPELINE_STATUS"] = string(pipeline.StatusFailure)
state.Pipeline.Step.Environment["CI_STEP_STATUS"] = string(pipeline.StatusFailure)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cli/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func FormatFlag(tmpl string, hidden ...bool) *cli.StringFlag {
}
}

// specify repository
// RepoFlag specifies repository
var RepoFlag = &cli.StringFlag{
Name: "repository",
Aliases: []string{"repo"},
Expand Down
7 changes: 4 additions & 3 deletions cli/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func deploy(c *cli.Context) error {
}
}
if number == 0 {
return fmt.Errorf("Cannot deploy failure pipeline")
return fmt.Errorf("cannot deploy failure pipeline")
}
} else {
number, err = strconv.ParseInt(pipelineArg, 10, 64)
Expand All @@ -106,9 +106,10 @@ func deploy(c *cli.Context) error {
}
}

env := c.Args().Get(2)
envArgIndex := 2
env := c.Args().Get(envArgIndex)
if env == "" {
return fmt.Errorf("Please specify the target environment (ie production)")
return fmt.Errorf("please specify the target environment (ie production)")
}

params := internal.ParseKeyPair(c.StringSlice("param"))
Expand Down
8 changes: 4 additions & 4 deletions cli/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error

droneEnv := make(map[string]string)
for _, env := range c.StringSlice("env") {
envs := strings.SplitN(env, "=", 2)
droneEnv[envs[0]] = envs[1]
if _, exists := environ[envs[0]]; exists {
before, after, _ := strings.Cut(env, "=")
droneEnv[before] = after
if _, exists := environ[before]; exists {
// don't override existing values
continue
}
environ[envs[0]] = envs[1]
environ[before] = before
}

tmpl, err := envsubst.ParseFile(file)
Expand Down
10 changes: 5 additions & 5 deletions cli/internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func NewClient(c *cli.Context) (woodpecker.Client, error) {
// if no server url is provided we can default
// to the hosted Woodpecker service.
if len(server) == 0 {
return nil, fmt.Errorf("Error: you must provide the Woodpecker server address")
return nil, fmt.Errorf("you must provide the Woodpecker server address")
}
if len(token) == 0 {
return nil, fmt.Errorf("Error: you must provide your Woodpecker access token")
return nil, fmt.Errorf("you must provide your Woodpecker access token")
}

// attempt to find system CA certs
Expand Down Expand Up @@ -107,11 +107,11 @@ func ParseRepo(client woodpecker.Client, str string) (repoID int64, err error) {
func ParseKeyPair(p []string) map[string]string {
params := map[string]string{}
for _, i := range p {
parts := strings.SplitN(i, "=", 2)
if len(parts) != 2 {
before, after, ok := strings.Cut(i, "=")
if !ok || before == "" {
continue
}
params[parts[0]] = parts[1]
params[before] = after
}
return params
}
9 changes: 4 additions & 5 deletions cli/pipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import (
"strings"
"text/template"

"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"

"github.com/urfave/cli/v2"

"go.woodpecker-ci.org/woodpecker/v2/cli/common"
"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
)

var pipelineCreateCmd = &cli.Command{
Expand Down Expand Up @@ -61,9 +60,9 @@ func pipelineCreate(c *cli.Context) error {
variables := make(map[string]string)

for _, vaz := range c.StringSlice("var") {
sp := strings.SplitN(vaz, "=", 2)
if len(sp) == 2 {
variables[sp[0]] = sp[1]
before, after, _ := strings.Cut(vaz, "=")
if before != "" && after != "" {
variables[before] = after
}
}

Expand Down
2 changes: 1 addition & 1 deletion cli/pipeline/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func pipelineInfo(c *cli.Context) error {
pipelineArg := c.Args().Get(1)

var number int64
if pipelineArg == "last" || len(pipelineArg) == 0 {
if pipelineArg == "last" || len(pipelineArg) == 0 { //nolint:goconst
// Fetch the pipeline number from the last pipeline
pipeline, err := client.PipelineLast(repoID, "")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cli/pipeline/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
)

//nolint:gomnd
var pipelineListCmd = &cli.Command{
Name: "ls",
Usage: "show pipeline history",
Expand Down
6 changes: 4 additions & 2 deletions cli/pipeline/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ func pipelineLogs(c *cli.Context) error {
return err
}

number, err := strconv.ParseInt(c.Args().Get(1), 10, 64)
numberArgIndex := 1
number, err := strconv.ParseInt(c.Args().Get(numberArgIndex), 10, 64)
if err != nil {
return err
}

step, err := strconv.ParseInt(c.Args().Get(2), 10, 64)
stepArgIndex := 2
step, err := strconv.ParseInt(c.Args().Get(stepArgIndex), 10, 64)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions cli/secret/secret_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,18 @@ func secretInfo(c *cli.Context) error {
}

var secret *woodpecker.Secret
if global {
switch {
case global:
secret, err = client.GlobalSecret(secretName)
if err != nil {
return err
}
} else if orgID != -1 {
case orgID != -1:
secret, err = client.OrgSecret(orgID, secretName)
if err != nil {
return err
}
} else {
default:
secret, err = client.Secret(repoID, secretName)
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions cli/secret/secret_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,18 @@ func secretList(c *cli.Context) error {
}

var list []*woodpecker.Secret
if global {
switch {
case global:
list, err = client.GlobalSecretList()
if err != nil {
return err
}
} else if orgID != -1 {
case orgID != -1:
list, err = client.OrgSecretList(orgID)
if err != nil {
return err
}
} else {
default:
list, err = client.SecretList(repoID)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cli/user/user_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func userInfo(c *cli.Context) error {

login := c.Args().First()
if len(login) == 0 {
return fmt.Errorf("Missing or invalid user login")
return fmt.Errorf("missing or invalid user login")
}

user, err := client.User(login)
Expand Down
Loading