diff --git a/yaml/build.go b/yaml/build.go index ef615910..27958ab0 100644 --- a/yaml/build.go +++ b/yaml/build.go @@ -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 +} diff --git a/yaml/build_test.go b/yaml/build_test.go index 5f4fd033..b94a94e0 100644 --- a/yaml/build_test.go +++ b/yaml/build_test.go @@ -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 diff --git a/yaml/metadata.go b/yaml/metadata.go index 22eb85f7..c927f0a7 100644 --- a/yaml/metadata.go +++ b/yaml/metadata.go @@ -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 -} diff --git a/yaml/metadata_test.go b/yaml/metadata_test.go index 19b8b758..9219f6cb 100644 --- a/yaml/metadata_test.go +++ b/yaml/metadata_test.go @@ -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) { @@ -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) - } - } -} diff --git a/yaml/testdata/build.yml b/yaml/testdata/build.yml index c0cec928..5cdb48fb 100644 --- a/yaml/testdata/build.yml +++ b/yaml/testdata/build.yml @@ -1,9 +1,6 @@ --- version: "1" -metadata: - template: false - environment: HELLO: "Hello, Global Message" diff --git a/yaml/testdata/build_empty_env.yml b/yaml/testdata/build_empty_env.yml new file mode 100644 index 00000000..6b4f7d06 --- /dev/null +++ b/yaml/testdata/build_empty_env.yml @@ -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 ] \ No newline at end of file