Skip to content

Commit

Permalink
fix(metadata): moving metadata unmarshaler override to build (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Mar 11, 2022
1 parent 3bcdda2 commit f050547
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 81 deletions.
40 changes: 40 additions & 0 deletions yaml/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,43 @@ type Build struct {
Steps StepSlice `yaml:"steps,omitempty" json:"steps,omitempty" jsonschema:"oneof_required=steps,description=Provide sequential execution instructions.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/"`
Templates TemplateSlice `yaml:"templates,omitempty" json:"templates,omitempty" jsonschema:"description=Provide the name of templates to expand.\nReference: https://go-vela.github.io/docs/reference/yaml/templates/"`
}

// UnmarshalYAML implements the Unmarshaler interface for the Build type.
func (b *Build) UnmarshalYAML(unmarshal func(interface{}) error) error {
// build we try unmarshalling to
build := new(struct {
Version string
Metadata Metadata
Environment raw.StringSliceMap
Worker Worker
Secrets SecretSlice
Services ServiceSlice
Stages StageSlice
Steps StepSlice
Templates TemplateSlice
})

// attempt to unmarshal as a build type
err := unmarshal(build)
if err != nil {
return err
}

// give the documented default value to metadata environment
if build.Metadata.Environment == nil {
build.Metadata.Environment = []string{"steps", "services", "secrets"}
}

// override the values
b.Version = build.Version
b.Metadata = build.Metadata
b.Environment = build.Environment
b.Worker = build.Worker
b.Secrets = build.Secrets
b.Services = build.Services
b.Stages = build.Stages
b.Steps = build.Steps
b.Templates = build.Templates

return nil
}
48 changes: 48 additions & 0 deletions yaml/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,54 @@ func TestYaml_Build_UnmarshalYAML(t *testing.T) {
},
},
},
{
file: "testdata/build_empty_env.yml",
want: &Build{
Version: "1",
Metadata: Metadata{
Template: false,
Clone: nil,
Environment: []string{},
},
Environment: raw.StringSliceMap{
"HELLO": "Hello, Global Message",
},
Worker: Worker{
Flavor: "16cpu8gb",
Platform: "gcp"},
Steps: StepSlice{
{
Commands: raw.StringSlice{"./gradlew downloadDependencies"},
Environment: raw.StringSliceMap{
"GRADLE_OPTS": "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false",
"GRADLE_USER_HOME": ".gradle",
},
Image: "openjdk:latest",
Name: "install",
Pull: "always",
Ruleset: Ruleset{
If: Rules{Event: []string{"push", "pull_request"}},
Matcher: "filepath",
Operator: "and",
},
Ulimits: UlimitSlice{
{
Name: "foo",
Soft: 1024,
Hard: 2048,
},
},
Volumes: VolumeSlice{
{
Source: "/foo",
Destination: "/bar",
AccessMode: "ro",
},
},
},
},
},
},
}

// run tests
Expand Down
29 changes: 0 additions & 29 deletions yaml/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,3 @@ func (m *Metadata) HasEnvironment(container string) bool {

return false
}

// UnmarshalYAML implements the Unmarshaler interface for the Metadata type.
func (m *Metadata) UnmarshalYAML(unmarshal func(interface{}) error) error {
// metadata we try unmarshalling to
metadata := new(struct {
Template bool
RenderInline bool `yaml:"render_inline,omitempty" json:"render_inline,omitempty"`
Clone *bool
Environment []string
})

// attempt to unmarshal as a metadata type
err := unmarshal(metadata)
if err != nil {
return err
}

if len(metadata.Environment) == 0 {
metadata.Environment = []string{"steps", "services", "secrets"}
}

// overwrite existing metadata environment details
m.Template = metadata.Template
m.RenderInline = metadata.RenderInline
m.Clone = metadata.Clone
m.Environment = metadata.Environment

return nil
}
49 changes: 0 additions & 49 deletions yaml/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
package yaml

import (
"io/ioutil"
"reflect"
"testing"

"github.com/go-vela/types/pipeline"

"github.com/buildkite/yaml"
)

func TestYaml_Metadata_ToPipeline(t *testing.T) {
Expand Down Expand Up @@ -109,49 +106,3 @@ func TestYaml_Metadata_HasEnvironment(t *testing.T) {
}
}
}

func TestYaml_Metadata_UnmarshalYAML(t *testing.T) {
// setup tests
tests := []struct {
failure bool
file string
want *Metadata
}{
{
failure: false,
file: "testdata/metadata.yml",
want: &Metadata{
Template: false,
Clone: nil,
Environment: []string{"steps", "services", "secrets"},
},
},
{
file: "testdata/metadata_env.yml",
want: &Metadata{
Template: false,
Clone: nil,
Environment: []string{"steps"},
},
},
}

// run tests
for _, test := range tests {
got := new(Metadata)

b, err := ioutil.ReadFile(test.file)
if err != nil {
t.Errorf("Reading file for UnmarshalYAML returned err: %v", err)
}

err = yaml.Unmarshal(b, got)
if err != nil {
t.Errorf("UnmarshalYAML returned err: %v", err)
}

if !reflect.DeepEqual(got, test.want) {
t.Errorf("UnmarshalYAML is %v, want %v", got, test.want)
}
}
}
3 changes: 0 additions & 3 deletions yaml/testdata/build.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
---
version: "1"

metadata:
template: false

environment:
HELLO: "Hello, Global Message"

Expand Down
27 changes: 27 additions & 0 deletions yaml/testdata/build_empty_env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
version: "1"

metadata:
template: false
environment: []

environment:
HELLO: "Hello, Global Message"

worker:
flavor: 16cpu8gb
platform: gcp

steps:
- name: install
commands:
- ./gradlew downloadDependencies
environment:
GRADLE_OPTS: -Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false
GRADLE_USER_HOME: .gradle
image: openjdk:latest
pull: true
ruleset:
event: [ push, pull_request ]
volumes: [ /foo:/bar:ro ]
ulimits: [ foo=1024:2048 ]

0 comments on commit f050547

Please sign in to comment.