1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2026-06-03 16:35:37 +02:00

Improve linter warning when step has no when block (#6314)

This commit is contained in:
Sim-hu
2026-03-24 23:05:49 +09:00
committed by GitHub
parent efd464e277
commit f842ce2cc2
2 changed files with 9 additions and 2 deletions
+4 -1
View File
@@ -342,8 +342,10 @@ func (l *Linter) lintBadHabits(config *WorkflowConfig) (err error) {
// root whens do not necessarily have an event filter, check steps // root whens do not necessarily have an event filter, check steps
for _, step := range parsed.Steps.ContainerList { for _, step := range parsed.Steps.ContainerList {
var field string var field string
var msg string
if len(step.When.Constraints) == 0 { if len(step.When.Constraints) == 0 {
field = fmt.Sprintf("steps.%s", step.Name) 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 { } else {
stepEventIndex := -1 stepEventIndex := -1
for i, c := range step.When.Constraints { for i, c := range step.When.Constraints {
@@ -354,12 +356,13 @@ func (l *Linter) lintBadHabits(config *WorkflowConfig) (err error) {
} }
if stepEventIndex > -1 { if stepEventIndex > -1 {
field = fmt.Sprintf("steps.%s.when[%d]", step.Name, stepEventIndex) 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 != "" { if field != "" {
err = multierr.Append(err, &pipeline_errors.PipelineError{ err = multierr.Append(err, &pipeline_errors.PipelineError{
Type: pipeline_errors.PipelineErrorTypeBadHabit, 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{ Data: pipeline_errors.BadHabitErrorData{
File: config.File, File: config.File,
Field: field, Field: field,
+5 -1
View File
@@ -214,10 +214,14 @@ func TestBadHabits(t *testing.T) {
}{ }{
{ {
from: "steps: { build: { image: golang } }", 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 } }", 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", want: "Set an event filter for all steps or the entire workflow on all items of the `when` block",
}, },
} }