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

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
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,

View File

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