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

Ignore warnings for cli exec #3868

Merged
merged 5 commits into from
Jul 3, 2024
Merged
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
20 changes: 13 additions & 7 deletions cli/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
"github.com/urfave/cli/v2"

"go.woodpecker-ci.org/woodpecker/v2/cli/common"
"go.woodpecker-ci.org/woodpecker/v2/cli/lint"
"go.woodpecker-ci.org/woodpecker/v2/pipeline"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/docker"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/dummy"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/kubernetes"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/local"
backendTypes "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
backend_types "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/compiler"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/linter"
Expand Down Expand Up @@ -179,12 +180,17 @@
}

// lint the yaml file
if err := linter.New(linter.WithTrusted(true)).Lint([]*linter.WorkflowConfig{{
err = linter.New(linter.WithTrusted(true)).Lint([]*linter.WorkflowConfig{{

Check warning on line 183 in cli/exec/exec.go

View check run for this annotation

Codecov / codecov/patch

cli/exec/exec.go#L183

Added line #L183 was not covered by tests
File: path.Base(file),
RawConfig: confStr,
Workflow: conf,
}}); err != nil {
return err
}})
if err != nil {
str, err := lint.FormatLintError(file, err)
fmt.Print(str)
if err != nil {
return err
}

Check warning on line 193 in cli/exec/exec.go

View check run for this annotation

Codecov / codecov/patch

cli/exec/exec.go#L187-L193

Added lines #L187 - L193 were not covered by tests
}

// compiles the yaml file
Expand Down Expand Up @@ -224,8 +230,8 @@
return err
}

backendCtx := context.WithValue(c.Context, backendTypes.CliContext, c)
backends := []backendTypes.Backend{
backendCtx := context.WithValue(c.Context, backend_types.CliContext, c)
backends := []backend_types.Backend{

Check warning on line 234 in cli/exec/exec.go

View check run for this annotation

Codecov / codecov/patch

cli/exec/exec.go#L233-L234

Added lines #L233 - L234 were not covered by tests
kubernetes.New(),
docker.New(),
local.New(),
Expand Down Expand Up @@ -277,7 +283,7 @@
}

const maxLogLineLength = 1024 * 1024 // 1mb
var defaultLogger = pipeline.Logger(func(step *backendTypes.Step, rc io.ReadCloser) error {
var defaultLogger = pipeline.Logger(func(step *backend_types.Step, rc io.ReadCloser) error {

Check warning on line 286 in cli/exec/exec.go

View check run for this annotation

Codecov / codecov/patch

cli/exec/exec.go#L286

Added line #L286 was not covered by tests
logWriter := NewLineWriter(step.Name, step.UUID)
return pipelineLog.CopyLineByLine(logWriter, rc, maxLogLineLength)
})
34 changes: 4 additions & 30 deletions cli/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@
package lint

import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"

term_env "github.com/muesli/termenv"
"github.com/urfave/cli/v2"

"go.woodpecker-ci.org/woodpecker/v2/cli/common"
pipeline_errors "go.woodpecker-ci.org/woodpecker/v2/pipeline/errors"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/linter"
)
Expand Down Expand Up @@ -72,8 +69,6 @@
}

func lintFile(_ *cli.Context, file string) error {
output := term_env.NewOutput(os.Stdout)

fi, err := os.Open(file)
if err != nil {
return err
Expand Down Expand Up @@ -101,34 +96,13 @@
// TODO: lint multiple files at once to allow checks for sth like "depends_on" to work
err = linter.New(linter.WithTrusted(true)).Lint([]*linter.WorkflowConfig{config})
if err != nil {
fmt.Printf("🔥 %s has warnings / errors:\n", output.String(config.File).Underline())

hasErrors := false
for _, err := range pipeline_errors.GetPipelineErrors(err) {
line := " "

if err.IsWarning {
line = fmt.Sprintf("%s ⚠️ ", line)
} else {
line = fmt.Sprintf("%s ❌", line)
hasErrors = true
}

if data := pipeline_errors.GetLinterData(err); data != nil {
line = fmt.Sprintf("%s %s\t%s", line, output.String(data.Field).Bold(), err.Message)
} else {
line = fmt.Sprintf("%s %s", line, err.Message)
}

// TODO: use table output
fmt.Printf("%s\n", line)
}
str, err := FormatLintError(config.File, err)

Check warning on line 99 in cli/lint/lint.go

View check run for this annotation

Codecov / codecov/patch

cli/lint/lint.go#L99

Added line #L99 was not covered by tests

if hasErrors {
return errors.New("config has errors")
if str != "" {
fmt.Print(str)

Check warning on line 102 in cli/lint/lint.go

View check run for this annotation

Codecov / codecov/patch

cli/lint/lint.go#L101-L102

Added lines #L101 - L102 were not covered by tests
}

return nil
return err

Check warning on line 105 in cli/lint/lint.go

View check run for this annotation

Codecov / codecov/patch

cli/lint/lint.go#L105

Added line #L105 was not covered by tests
}

fmt.Println("✅ Config is valid")
Expand Down
56 changes: 56 additions & 0 deletions cli/lint/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package lint

import (
"errors"
"fmt"
"os"

term_env "github.com/muesli/termenv"

pipeline_errors "go.woodpecker-ci.org/woodpecker/v2/pipeline/errors"
)

func FormatLintError(file string, err error) (string, error) {
if err == nil {
return "", nil
}

Check warning on line 16 in cli/lint/utils.go

View check run for this annotation

Codecov / codecov/patch

cli/lint/utils.go#L13-L16

Added lines #L13 - L16 were not covered by tests

output := term_env.NewOutput(os.Stdout)
str := ""

amountErrors := 0
amountWarnings := 0
linterErrors := pipeline_errors.GetPipelineErrors(err)
for _, err := range linterErrors {
line := " "

if err.IsWarning {
line = fmt.Sprintf("%s ⚠️ ", line)
amountWarnings++
} else {
line = fmt.Sprintf("%s ❌", line)
amountErrors++
}

Check warning on line 33 in cli/lint/utils.go

View check run for this annotation

Codecov / codecov/patch

cli/lint/utils.go#L18-L33

Added lines #L18 - L33 were not covered by tests

if data := pipeline_errors.GetLinterData(err); data != nil {
line = fmt.Sprintf("%s %s\t%s", line, output.String(data.Field).Bold(), err.Message)
} else {
line = fmt.Sprintf("%s %s", line, err.Message)
}

Check warning on line 39 in cli/lint/utils.go

View check run for this annotation

Codecov / codecov/patch

cli/lint/utils.go#L35-L39

Added lines #L35 - L39 were not covered by tests

// TODO: use table output
str = fmt.Sprintf("%s%s\n", str, line)

Check warning on line 42 in cli/lint/utils.go

View check run for this annotation

Codecov / codecov/patch

cli/lint/utils.go#L42

Added line #L42 was not covered by tests
}

if amountErrors > 0 {
if amountWarnings > 0 {
str = fmt.Sprintf("🔥 %s has %d errors and warnings:\n%s", output.String(file).Underline(), len(linterErrors), str)
} else {
str = fmt.Sprintf("🔥 %s has %d errors:\n%s", output.String(file).Underline(), len(linterErrors), str)
}
return str, errors.New("config has errors")

Check warning on line 51 in cli/lint/utils.go

View check run for this annotation

Codecov / codecov/patch

cli/lint/utils.go#L45-L51

Added lines #L45 - L51 were not covered by tests
}

str = fmt.Sprintf("⚠️ %s has %d warnings:\n%s", output.String(file).Underline(), len(linterErrors), str)
return str, nil

Check warning on line 55 in cli/lint/utils.go

View check run for this annotation

Codecov / codecov/patch

cli/lint/utils.go#L54-L55

Added lines #L54 - L55 were not covered by tests
}