Skip to content

Commit cbd8cd0

Browse files
authored
Merge pull request #156 from buildkite-plugins/SUP-5894-fix-notify-group-step
Fix `notify` attribute when step is inside `group` step
2 parents c2feab3 + 2221d96 commit cbd8cd0

3 files changed

Lines changed: 84 additions & 9 deletions

File tree

pipeline_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,39 @@ func TestGeneratePipelineWithSecretsInGroup(t *testing.T) {
826826
assert.Contains(t, string(got), "- PROD_DB_PASS")
827827
}
828828

829+
func TestGeneratePipelineWithNotifyInGroup(t *testing.T) {
830+
steps := []Step{{
831+
Group: "Test Group",
832+
Steps: []Step{{
833+
Label: "Run Tests",
834+
Command: "echo 'test'",
835+
Notify: []StepNotify{{
836+
GithubStatus: GithubStatusNotification{
837+
Context: "buildkite/test/status",
838+
},
839+
}},
840+
}},
841+
}}
842+
843+
plugin := Plugin{}
844+
845+
tmp, hasPipeline, err := generatePipeline(steps, plugin)
846+
assert.NoError(t, err)
847+
assert.True(t, hasPipeline)
848+
defer os.Remove(tmp.Name())
849+
850+
content, err := os.ReadFile(tmp.Name())
851+
assert.NoError(t, err)
852+
853+
output := string(content)
854+
855+
// Verify correct notify syntax (not rawnotify)
856+
assert.Contains(t, output, "notify:")
857+
assert.NotContains(t, output, "rawnotify")
858+
assert.Contains(t, output, "github_commit_status:")
859+
assert.Contains(t, output, "context: buildkite/test/status")
860+
}
861+
829862
func TestFilterValidSteps_AllValid(t *testing.T) {
830863
steps := []Step{
831864
{Command: "echo valid 1"},

plugin.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,18 @@ func setBuild(build *Build) {
421421
}
422422
}
423423

424-
// processNestedStepsEnv recursively processes environment variables for nested steps
425-
func processNestedStepsEnv(steps []Step, env map[string]string) {
424+
// processNestedSteps recursively processes nested steps, handling environment variables and notify configurations
425+
func processNestedSteps(steps []Step, env map[string]string) {
426426
for i := range steps {
427427
// Parse the step's own env
428428
steps[i].Env, _ = parseEnv(steps[i].RawEnv)
429429
steps[i].Build.Env, _ = parseEnv(steps[i].Build.RawEnv)
430430

431+
// Process notify for nested steps
432+
if steps[i].RawNotify != nil {
433+
setNotify(&steps[i].Notify, &steps[i].RawNotify)
434+
}
435+
431436
// Append top-level env to this step
432437
for key, value := range env {
433438
if steps[i].Command != nil || steps[i].Commands != nil {
@@ -449,7 +454,7 @@ func processNestedStepsEnv(steps []Step, env map[string]string) {
449454

450455
// Recursively process any nested steps
451456
if len(steps[i].Steps) > 0 {
452-
processNestedStepsEnv(steps[i].Steps, env)
457+
processNestedSteps(steps[i].Steps, env)
453458
}
454459
}
455460
}
@@ -480,9 +485,9 @@ func appendEnv(watch *WatchConfig, env map[string]string) {
480485

481486
watch.Step.RawEnv = nil
482487
watch.Step.Build.RawEnv = nil
483-
// Process nested steps' environment variables
488+
// Process nested steps
484489
if len(watch.Step.Steps) > 0 {
485-
processNestedStepsEnv(watch.Step.Steps, env)
490+
processNestedSteps(watch.Step.Steps, env)
486491
}
487492
watch.RawPath = nil
488493
watch.RawSkipPath = nil

plugin_test.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,43 @@ func TestPluginShouldClearRawEnvFromNestedSteps(t *testing.T) {
943943
assert.Nil(t, secondStep.Build.RawEnv, "Second nested step Build.RawEnv should be nil")
944944
}
945945

946+
func TestPluginShouldProcessNotifyInNestedSteps(t *testing.T) {
947+
param := `[{
948+
"github.com/buildkite-plugins/monorepo-diff-buildkite-plugin#v1.0.0": {
949+
"watch": [{
950+
"path": "foo/**",
951+
"config": {
952+
"group": "Foo",
953+
"steps": [{
954+
"command": "./scripts/a.sh",
955+
"label": "Run A",
956+
"notify": [{
957+
"github_commit_status": {
958+
"context": "buildkite/foo/a"
959+
}
960+
}]
961+
}]
962+
}
963+
}]
964+
}
965+
}]`
966+
967+
got, err := initializePlugin(param)
968+
assert.NoError(t, err)
969+
970+
// Verify nested step exists
971+
assert.Equal(t, 1, len(got.Watch[0].Step.Steps))
972+
973+
nestedStep := got.Watch[0].Step.Steps[0]
974+
975+
// Verify RawNotify is cleared
976+
assert.Nil(t, nestedStep.RawNotify, "RawNotify should be nil after processing")
977+
978+
// Verify Notify is populated
979+
assert.Equal(t, 1, len(nestedStep.Notify), "Notify should have 1 entry")
980+
assert.Equal(t, "buildkite/foo/a", nestedStep.Notify[0].GithubStatus.Context)
981+
}
982+
946983
func TestPluginShouldPreserveDependsOnString(t *testing.T) {
947984
param := `[{
948985
"github.com/buildkite-plugins/monorepo-diff-buildkite-plugin#commit": {
@@ -1331,8 +1368,8 @@ func TestParseEnvMapEmptyStringPreserved(t *testing.T) {
13311368
result, err := parseEnv(input)
13321369

13331370
assert.NoError(t, err)
1334-
assert.Equal(t, "", result["TEST_VAR"]) // Empty string preserved
1335-
assert.Equal(t, "", result["ANOTHER_VAR"]) // Empty string preserved
1371+
assert.Equal(t, "", result["TEST_VAR"]) // Empty string preserved
1372+
assert.Equal(t, "", result["ANOTHER_VAR"]) // Empty string preserved
13361373
assert.Equal(t, "value", result["EXPLICIT"])
13371374
}
13381375

@@ -1663,8 +1700,8 @@ func TestMapEnvWithOSEnvReading(t *testing.T) {
16631700
got, err := initializePlugin(param)
16641701

16651702
assert.NoError(t, err)
1666-
assert.Equal(t, "us-west-2", got.Env["AWS_REGION"]) // Null reads from OS env
1667-
assert.Equal(t, "", got.Env["EMPTY_VAR"]) // Empty string is literal
1703+
assert.Equal(t, "us-west-2", got.Env["AWS_REGION"]) // Null reads from OS env
1704+
assert.Equal(t, "", got.Env["EMPTY_VAR"]) // Empty string is literal
16681705
assert.Equal(t, "explicit-value", got.Env["EXPLICIT"])
16691706
}
16701707

0 commit comments

Comments
 (0)