Skip to content

Commit 0bc7e32

Browse files
committed
add conditional support (if) to steps
1 parent 8a1749e commit 0bc7e32

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
github.com/bmatcuk/doublestar/v4 v4.8.1 h1:54Bopc5c2cAvhLRAzqOGCYHYyhcDHsFF4wWIR5wKP38=
2-
github.com/bmatcuk/doublestar/v4 v4.8.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
3-
github.com/bmatcuk/doublestar/v4 v4.9.0 h1:DBvuZxjdKkRP/dr4GVV4w2fnmrk5Hxc90T51LZjv0JA=
4-
github.com/bmatcuk/doublestar/v4 v4.9.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
51
github.com/bmatcuk/doublestar/v4 v4.9.1 h1:X8jg9rRZmJd4yRy7ZeNDRnM+T3ZfHv15JiBJ/avrEXE=
62
github.com/bmatcuk/doublestar/v4 v4.9.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
73
github.com/buildkite/bintest/v3 v3.3.0 h1:RTWcSaJRlOT6t/K311ejPf+0J3LE/QEODzVG3vlLnWo=

pipeline_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,36 @@ func TestGeneratePipelineWithNoStepsAndNoHooks(t *testing.T) {
547547

548548
assert.Equal(t, want, string(got))
549549
}
550+
551+
func TestGeneratePipelineWithCondition(t *testing.T) {
552+
steps := []Step{
553+
{
554+
Command: "echo deploy to production",
555+
Label: "Deploy",
556+
Condition: "build.branch == 'main' && build.pull_request.id == null",
557+
},
558+
{
559+
Trigger: "test-pipeline",
560+
Condition: "build.message =~ /\\[deploy\\]/",
561+
},
562+
}
563+
564+
want := `steps:
565+
- label: Deploy
566+
if: build.branch == 'main' && build.pull_request.id == null
567+
command: echo deploy to production
568+
- trigger: test-pipeline
569+
if: build.message =~ /\[deploy\]/
570+
`
571+
572+
plugin := Plugin{Wait: false}
573+
574+
pipeline, _, err := generatePipeline(steps, plugin)
575+
require.NoError(t, err)
576+
defer os.Remove(pipeline.Name())
577+
578+
got, err := os.ReadFile(pipeline.Name())
579+
require.NoError(t, err)
580+
581+
assert.Equal(t, want, string(got))
582+
}

plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type Step struct {
8080
Trigger string `yaml:"trigger,omitempty"`
8181
Label string `yaml:"label,omitempty"`
8282
Branches string `yaml:"branches,omitempty"`
83+
Condition string `json:"if,omitempty" yaml:"if,omitempty"`
8384
Build Build `yaml:"build,omitempty"`
8485
Command interface{} `yaml:"command,omitempty"`
8586
Commands interface{} `yaml:"commands,omitempty"`

plugin_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,3 +824,42 @@ func TestPluginLevelMetadataNotAppliedToCommandSteps(t *testing.T) {
824824
assert.Equal(t, "echo Make Changes to Bin", commandStep.Command)
825825
assert.Nil(t, commandStep.Build.Metadata, "Command step should not have metadata applied")
826826
}
827+
828+
func TestPluginShouldPreserveStepCondition(t *testing.T) {
829+
param := `[{
830+
"github.com/buildkite-plugins/monorepo-diff-buildkite-plugin#commit": {
831+
"watch": [
832+
{
833+
"path": "service/**/*",
834+
"config": {
835+
"command": "echo deploy",
836+
"if": "build.branch == 'main' && build.pull_request.id == null"
837+
}
838+
}
839+
]
840+
}
841+
}]`
842+
843+
got, err := initializePlugin(param)
844+
assert.NoError(t, err)
845+
846+
expected := Plugin{
847+
Diff: "git diff --name-only HEAD~1",
848+
Wait: false,
849+
LogLevel: "info",
850+
Interpolation: true,
851+
Watch: []WatchConfig{
852+
{
853+
Paths: []string{"service/**/*"},
854+
Step: Step{
855+
Command: "echo deploy",
856+
Condition: "build.branch == 'main' && build.pull_request.id == null",
857+
},
858+
},
859+
},
860+
}
861+
862+
if diff := cmp.Diff(expected, got); diff != "" {
863+
t.Fatalf("plugin diff (-want +got):\n%s", diff)
864+
}
865+
}

0 commit comments

Comments
 (0)