Skip to content

Commit

Permalink
Ignore warnings for cli exec (woodpecker-ci#3868)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored and 6543 committed Sep 5, 2024
1 parent bc786b3 commit 525cdc0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 37 deletions.
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 @@ import (
"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 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error
}

// lint the yaml file
if err := linter.New(linter.WithTrusted(true)).Lint([]*linter.WorkflowConfig{{
err = linter.New(linter.WithTrusted(true)).Lint([]*linter.WorkflowConfig{{
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
}
}

// compiles the yaml file
Expand Down Expand Up @@ -224,8 +230,8 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error
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{
kubernetes.New(),
docker.New(),
local.New(),
Expand Down Expand Up @@ -277,7 +283,7 @@ func convertPathForWindows(path string) string {
}

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 {
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 lintDir(c *cli.Context, dir string) error {
}

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 @@ func lintFile(_ *cli.Context, file string) error {
// 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)

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

return nil
return err
}

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
}

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++
}

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
str = fmt.Sprintf("%s%s\n", str, line)
}

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")
}

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

0 comments on commit 525cdc0

Please sign in to comment.