Ignore warnings for cli exec (#3868)

This commit is contained in:
Anbraten 2024-07-03 16:22:09 +02:00 committed by GitHub
parent 38b8a55714
commit e118f8d980
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 73 additions and 37 deletions

View file

@ -29,13 +29,14 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.woodpecker-ci.org/woodpecker/v2/cli/common" "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"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend" "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/docker"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/dummy" "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/kubernetes"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/local" "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"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/compiler" "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/compiler"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/linter" "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/linter"
@ -179,12 +180,17 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error
} }
// lint the yaml file // 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), File: path.Base(file),
RawConfig: confStr, RawConfig: confStr,
Workflow: conf, 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 // compiles the yaml file
@ -224,8 +230,8 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error
return err return err
} }
backendCtx := context.WithValue(c.Context, backendTypes.CliContext, c) backendCtx := context.WithValue(c.Context, backend_types.CliContext, c)
backends := []backendTypes.Backend{ backends := []backend_types.Backend{
kubernetes.New(), kubernetes.New(),
docker.New(), docker.New(),
local.New(), local.New(),
@ -277,7 +283,7 @@ func convertPathForWindows(path string) string {
} }
const maxLogLineLength = 1024 * 1024 // 1mb 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) logWriter := NewLineWriter(step.Name, step.UUID)
return pipelineLog.CopyLineByLine(logWriter, rc, maxLogLineLength) return pipelineLog.CopyLineByLine(logWriter, rc, maxLogLineLength)
}) })

View file

@ -15,18 +15,15 @@
package lint package lint
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
term_env "github.com/muesli/termenv"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.woodpecker-ci.org/woodpecker/v2/cli/common" "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"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/linter" "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/linter"
) )
@ -72,8 +69,6 @@ func lintDir(c *cli.Context, dir string) error {
} }
func lintFile(_ *cli.Context, file string) error { func lintFile(_ *cli.Context, file string) error {
output := term_env.NewOutput(os.Stdout)
fi, err := os.Open(file) fi, err := os.Open(file)
if err != nil { if err != nil {
return err return err
@ -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 // 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}) err = linter.New(linter.WithTrusted(true)).Lint([]*linter.WorkflowConfig{config})
if err != nil { if err != nil {
fmt.Printf("🔥 %s has warnings / errors:\n", output.String(config.File).Underline()) str, err := FormatLintError(config.File, err)
hasErrors := false if str != "" {
for _, err := range pipeline_errors.GetPipelineErrors(err) { fmt.Print(str)
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)
} }
if hasErrors { return err
return errors.New("config has errors")
}
return nil
} }
fmt.Println("✅ Config is valid") fmt.Println("✅ Config is valid")

56
cli/lint/utils.go Normal file
View file

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