mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2026-04-15 01:41:56 +00:00
Improve linter warning when step has no when block (#6314)
This commit is contained in:
@@ -342,8 +342,10 @@ func (l *Linter) lintBadHabits(config *WorkflowConfig) (err error) {
|
||||
// root whens do not necessarily have an event filter, check steps
|
||||
for _, step := range parsed.Steps.ContainerList {
|
||||
var field string
|
||||
var msg string
|
||||
if len(step.When.Constraints) == 0 {
|
||||
field = fmt.Sprintf("steps.%s", step.Name)
|
||||
msg = "Consider adding a `when` block with an `event` filter to this step or the entire workflow"
|
||||
} else {
|
||||
stepEventIndex := -1
|
||||
for i, c := range step.When.Constraints {
|
||||
@@ -354,12 +356,13 @@ func (l *Linter) lintBadHabits(config *WorkflowConfig) (err error) {
|
||||
}
|
||||
if stepEventIndex > -1 {
|
||||
field = fmt.Sprintf("steps.%s.when[%d]", step.Name, stepEventIndex)
|
||||
msg = "Set an event filter for all steps or the entire workflow on all items of the `when` block"
|
||||
}
|
||||
}
|
||||
if field != "" {
|
||||
err = multierr.Append(err, &pipeline_errors.PipelineError{
|
||||
Type: pipeline_errors.PipelineErrorTypeBadHabit,
|
||||
Message: "Set an event filter for all steps or the entire workflow on all items of the `when` block",
|
||||
Message: msg,
|
||||
Data: pipeline_errors.BadHabitErrorData{
|
||||
File: config.File,
|
||||
Field: field,
|
||||
|
||||
@@ -214,10 +214,14 @@ func TestBadHabits(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
from: "steps: { build: { image: golang } }",
|
||||
want: "Set an event filter for all steps or the entire workflow on all items of the `when` block",
|
||||
want: "Consider adding a `when` block with an `event` filter to this step or the entire workflow",
|
||||
},
|
||||
{
|
||||
from: "when: [{branch: xyz}, {event: push}]\nsteps: { build: { image: golang } }",
|
||||
want: "Consider adding a `when` block with an `event` filter to this step or the entire workflow",
|
||||
},
|
||||
{
|
||||
from: "steps: { build: { image: golang, when: [{branch: main}] } }",
|
||||
want: "Set an event filter for all steps or the entire workflow on all items of the `when` block",
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user