Skip to content

Commit

Permalink
Add Key and Label fields (+aliases)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrJosh9000 committed Nov 28, 2023
1 parent be758a5 commit e7ed78f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
28 changes: 11 additions & 17 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ steps:
want := &Pipeline{
Steps: Steps{
&CommandStep{
Label: ":docker: building image",
Command: "docker build .",
RemainingFields: map[string]any{
"agents": ordered.MapFromItems(
ordered.TupleSA{Key: "queue", Value: "default"},
),
"name": ":docker: building image",
"type": "script",
"agent_query_rules": []any{"queue=default"},
},
Expand Down Expand Up @@ -148,7 +148,7 @@ steps:
"queue": "default"
},
"command": "docker build .",
"name": ":docker: building image",
"label": ":docker: building image",
"type": "script"
}
]
Expand Down Expand Up @@ -207,12 +207,12 @@ steps:
want := &Pipeline{
Steps: Steps{
&CommandStep{
Label: ":docker: building image",
Command: "docker build .",
RemainingFields: map[string]any{
"agents": ordered.MapFromItems(
ordered.TupleSA{Key: "queue", Value: "default"},
),
"name": ":docker: building image",
"type": "script",
"agent_query_rules": []any{"queue=default"},
},
Expand Down Expand Up @@ -256,7 +256,7 @@ steps:
"queue": "default"
},
"command": "docker build .",
"name": ":docker: building image",
"label": ":docker: building image",
"type": "script"
}
]
Expand Down Expand Up @@ -588,9 +588,7 @@ func TestParserParsesTopLevelSteps(t *testing.T) {
Steps: Steps{
&CommandStep{
Command: "echo hello world",
RemainingFields: map[string]any{
"name": "Build",
},
Label: "Build",
},
&WaitStep{Scalar: "wait"},
},
Expand All @@ -607,7 +605,7 @@ func TestParserParsesTopLevelSteps(t *testing.T) {
"steps": [
{
"command": "echo hello world",
"name": "Build"
"label": "Build"
},
"wait"
]
Expand Down Expand Up @@ -925,6 +923,7 @@ steps:
),
Steps: Steps{
&CommandStep{
Label: ":docker: Docker Build",
Command: "echo foo",
Plugins: Plugins{
{
Expand All @@ -935,9 +934,6 @@ steps:
},
},
},
RemainingFields: map[string]any{
"label": string(":docker: Docker Build"),
},
},
},
}
Expand Down Expand Up @@ -1013,6 +1009,7 @@ steps:
want := &Pipeline{
Steps: Steps{
&CommandStep{
Label: ":s3: xxx",
Command: "script/buildkite/xxx.sh",
Plugins: Plugins{
{
Expand Down Expand Up @@ -1041,7 +1038,6 @@ steps:
},
},
RemainingFields: map[string]any{
"name": ":s3: xxx",
"agents": ordered.MapFromItems(
ordered.TupleSA{Key: "queue", Value: "xxx"},
),
Expand All @@ -1064,7 +1060,7 @@ steps:
"queue": "xxx"
},
"command": "script/buildkite/xxx.sh",
"name": ":s3: xxx",
"label": ":s3: xxx",
"plugins": [
{
"github.com/xxx/aws-assume-role-buildkite-plugin#v0.1.0": {
Expand Down Expand Up @@ -1118,6 +1114,7 @@ func TestParserParsesScalarPlugins(t *testing.T) {
want := &Pipeline{
Steps: Steps{
&CommandStep{
Label: ":s3: xxx",
Command: "script/buildkite/xxx.sh",
Plugins: Plugins{
{
Expand All @@ -1133,9 +1130,6 @@ func TestParserParsesScalarPlugins(t *testing.T) {
},
},
},
RemainingFields: map[string]any{
"name": ":s3: xxx",
},
},
},
}
Expand All @@ -1151,7 +1145,7 @@ func TestParserParsesScalarPlugins(t *testing.T) {
"steps": [
{
"command": "script/buildkite/xxx.sh",
"name": ":s3: xxx",
"label": ":s3: xxx",
"plugins": [
{
"github.com/buildkite-plugins/example-plugin-buildkite-plugin#v1.0.0": null
Expand Down
32 changes: 21 additions & 11 deletions step_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type Signature struct {
//
// Standard caveats apply - see the package comment.
type CommandStep struct {
// Fields common to various step types
Key string `yaml:"key,omitempty" aliases:"id,identifier"`
Label string `yaml:"label,omitempty" aliases:"name"`

// Fields that are meaningful specifically for command steps
Command string `yaml:"command"`
Plugins Plugins `yaml:"plugins,omitempty"`
Env map[string]string `yaml:"env,omitempty"`
Expand Down Expand Up @@ -92,36 +97,41 @@ func (c *CommandStep) InterpolateMatrixPermutation(mp MatrixPermutation) error {
}

func (c *CommandStep) interpolate(tf stringTransformer) error {
cmd, err := tf.Transform(c.Command)
if err != nil {
return err
// Fields that are interpolated with env vars and matrix tokens:
// command, plugins
if err := interpolateString(tf, &c.Command); err != nil {
return fmt.Errorf("interpolating command: %w", err)
}
c.Command = cmd

if err := interpolateSlice(tf, c.Plugins); err != nil {
return err
return fmt.Errorf("interpolating plugins: %w", err)
}

switch tf.(type) {
case envInterpolator:
// Env interpolation applies to nearly everything:
// key, depends_on, env (keys and values), matrix
if err := interpolateString(tf, &c.Key); err != nil {
return fmt.Errorf("interpolating key: %w", err)
}
if err := interpolateMap(tf, c.Env); err != nil {
return err
return fmt.Errorf("interpolating env: %w", err)
}
if err := c.Matrix.interpolate(tf); err != nil {
return err
return fmt.Errorf("interpolating matrix: %w", err)
}

case matrixInterpolator:
// Matrix interpolation doesn't apply to env keys.
// Matrix interpolation applies only to some things, but particularly
// only affects env values (not env keys).
if err := interpolateMapValues(tf, c.Env); err != nil {
return err
return fmt.Errorf("interpolating env values: %w", err)
}
}

// NB: Do not interpolate Signature.

if err := interpolateMap(tf, c.RemainingFields); err != nil {
return err
return fmt.Errorf("interpolating remaining fields: %w", err)
}

return nil
Expand Down
17 changes: 10 additions & 7 deletions step_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import (
//
// Standard caveats apply - see the package comment.
type GroupStep struct {
// Group is typically a key with no value. Since it must always exist in
// a group step, here it is.
Group *string `yaml:"group"`
// Fields common to various step types
Key string `yaml:"key,omitempty" aliases:"id,identifier"`

// Group must always exist in a group step (so that we know it is a group).
// If it has a value, it is treated as equivalent to the label or name.
Group *string `yaml:"group" aliases:"label,name"`

Steps Steps `yaml:"steps"`

Expand All @@ -36,12 +39,12 @@ func (g *GroupStep) UnmarshalOrdered(src any) error {
}

func (g *GroupStep) interpolate(tf stringTransformer) error {
grp, err := interpolateAny(tf, g.Group)
if err != nil {
if err := interpolateString(tf, &g.Key); err != nil {
return err
}
if err := interpolateString(tf, g.Group); err != nil {
return err
}
g.Group = grp

if err := g.Steps.interpolate(tf); err != nil {
return err
}
Expand Down

0 comments on commit e7ed78f

Please sign in to comment.