diff --git a/compiler/engine.go b/compiler/engine.go index 7801dc1d..59dd16a5 100644 --- a/compiler/engine.go +++ b/compiler/engine.go @@ -23,11 +23,7 @@ type Engine interface { // Parse defines a function that converts // an object to a yaml configuration. - Parse(interface{}) (*yaml.Build, error) - - // Validate defines a function that verifies - // the yaml configuration is accurate. - Validate(*yaml.Build) error + Parse(interface{}) (*yaml.Build, []byte, error) // Clone Compiler Interface Functions diff --git a/compiler/native/compile.go b/compiler/native/compile.go index b14ba83c..734f53fe 100644 --- a/compiler/native/compile.go +++ b/compiler/native/compile.go @@ -14,6 +14,8 @@ import ( "strings" "time" + yml "github.com/goccy/go-yaml" + "github.com/go-vela/types/library" "github.com/go-vela/types/pipeline" "github.com/go-vela/types/yaml" @@ -32,15 +34,15 @@ type ModifyRequest struct { // Compile produces an executable pipeline from a yaml configuration. func (c *client) Compile(v interface{}) (*pipeline.Build, error) { // parse the object into a yaml configuration - p, err := c.Parse(v) + p, raw, err := c.Parse(v) if err != nil { return nil, err } // validate the yaml configuration - err = c.Validate(p) + err = p.Validate(raw) if err != nil { - return nil, fmt.Errorf("invalid pipeline: %w", err) + return nil, err } // create map of templates for easy lookup @@ -94,12 +96,17 @@ func (c *client) Compile(v interface{}) (*pipeline.Build, error) { if err != nil { return nil, err } + + raw, err = yml.Marshal(p) + if err != nil { + return nil, err + } } // validate the yaml configuration - err = c.Validate(p) + err = p.Validate(raw) if err != nil { - return nil, fmt.Errorf("invalid pipeline: %w", err) + return nil, err } // inject the environment variables into the stages @@ -148,12 +155,17 @@ func (c *client) Compile(v interface{}) (*pipeline.Build, error) { if err != nil { return nil, err } + + raw, err = yml.Marshal(p) + if err != nil { + return nil, err + } } // validate the yaml configuration - err = c.Validate(p) + err = p.Validate(raw) if err != nil { - return nil, fmt.Errorf("invalid pipeline: %w", err) + return nil, err } // inject the environment variables into the steps diff --git a/compiler/native/compile_test.go b/compiler/native/compile_test.go index b7cc60cf..7d1141be 100644 --- a/compiler/native/compile_test.go +++ b/compiler/native/compile_test.go @@ -79,6 +79,7 @@ func TestNative_Compile_StagesPipeline(t *testing.T) { Version: "1", ID: "__0", Metadata: pipeline.Metadata{ + Clone: true, Template: false, }, Stages: pipeline.StageSlice{ @@ -427,6 +428,7 @@ func TestNative_Compile_StepsPipeline(t *testing.T) { Version: "1", ID: "__0", Metadata: pipeline.Metadata{ + Clone: true, Template: false, }, Steps: pipeline.ContainerSlice{ @@ -616,6 +618,7 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) { Version: "1", ID: "__0", Metadata: pipeline.Metadata{ + Clone: true, Template: false, }, Stages: pipeline.StageSlice{ @@ -839,6 +842,7 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) { Version: "1", ID: "__0", Metadata: pipeline.Metadata{ + Clone: true, Template: false, }, Steps: pipeline.ContainerSlice{ @@ -1011,6 +1015,7 @@ func TestNative_Compile_InvalidType(t *testing.T) { Version: "1", ID: "__0", Metadata: pipeline.Metadata{ + Clone: true, Template: false, }, Steps: pipeline.ContainerSlice{ diff --git a/compiler/native/parse.go b/compiler/native/parse.go index f29cd1fc..71803d10 100644 --- a/compiler/native/parse.go +++ b/compiler/native/parse.go @@ -12,11 +12,11 @@ import ( types "github.com/go-vela/types/yaml" - "github.com/buildkite/yaml" + "github.com/goccy/go-yaml" ) // Parse converts an object to a yaml configuration. -func (c *client) Parse(v interface{}) (*types.Build, error) { +func (c *client) Parse(v interface{}) (*types.Build, []byte, error) { switch v := v.(type) { case []byte: return ParseBytes(v) @@ -35,34 +35,34 @@ func (c *client) Parse(v interface{}) (*types.Build, error) { // parse string as yaml configuration return ParseString(v) default: - return nil, fmt.Errorf("unable to parse yaml: unrecognized type %T", v) + return nil, nil, fmt.Errorf("unable to parse yaml: unrecognized type %T", v) } } // ParseBytes converts a byte slice to a yaml configuration. -func ParseBytes(b []byte) (*types.Build, error) { +func ParseBytes(b []byte) (*types.Build, []byte, error) { config := new(types.Build) // unmarshal the bytes into the yaml configuration err := yaml.Unmarshal(b, config) if err != nil { - return nil, fmt.Errorf("unable to unmarshal yaml: %v", err) + return nil, nil, fmt.Errorf("unable to unmarshal yaml: %v", err) } - return config, nil + return config, b, nil } // ParseFile converts an os.File into a yaml configuration. -func ParseFile(f *os.File) (*types.Build, error) { +func ParseFile(f *os.File) (*types.Build, []byte, error) { return ParseReader(f) } // ParsePath converts a file path into a yaml configuration. -func ParsePath(p string) (*types.Build, error) { +func ParsePath(p string) (*types.Build, []byte, error) { // open the file for reading f, err := os.Open(p) if err != nil { - return nil, fmt.Errorf("unable to open yaml file %s: %v", p, err) + return nil, nil, fmt.Errorf("unable to open yaml file %s: %v", p, err) } defer f.Close() @@ -71,17 +71,17 @@ func ParsePath(p string) (*types.Build, error) { } // ParseReader converts an io.Reader into a yaml configuration. -func ParseReader(r io.Reader) (*types.Build, error) { +func ParseReader(r io.Reader) (*types.Build, []byte, error) { // read all the bytes from the reader b, err := ioutil.ReadAll(r) if err != nil { - return nil, fmt.Errorf("unable to read bytes for yaml: %v", err) + return nil, nil, fmt.Errorf("unable to read bytes for yaml: %v", err) } return ParseBytes(b) } // ParseString converts a string into a yaml configuration. -func ParseString(s string) (*types.Build, error) { +func ParseString(s string) (*types.Build, []byte, error) { return ParseBytes([]byte(s)) } diff --git a/compiler/native/parse_test.go b/compiler/native/parse_test.go index b387631f..6d05f031 100644 --- a/compiler/native/parse_test.go +++ b/compiler/native/parse_test.go @@ -35,7 +35,7 @@ func TestNative_Parse_Metadata_Bytes(t *testing.T) { t.Errorf("Reading file returned err: %v", err) } - got, err := client.Parse(b) + got, _, err := client.Parse(b) if err != nil { t.Errorf("Parse returned err: %v", err) } @@ -63,7 +63,7 @@ func TestNative_Parse_Metadata_File(t *testing.T) { defer f.Close() - got, err := client.Parse(f) + got, _, err := client.Parse(f) if err != nil { t.Errorf("Parse returned err: %v", err) } @@ -78,7 +78,7 @@ func TestNative_Parse_Metadata_Invalid(t *testing.T) { client, _ := New(cli.NewContext(nil, flag.NewFlagSet("test", 0), nil)) // run test - got, err := client.Parse(nil) + got, _, err := client.Parse(nil) if err == nil { t.Error("Parse should have returned err") @@ -100,7 +100,7 @@ func TestNative_Parse_Metadata_Path(t *testing.T) { } // run test - got, err := client.Parse("testdata/metadata.yml") + got, _, err := client.Parse("testdata/metadata.yml") if err != nil { t.Errorf("Parse returned err: %v", err) } @@ -126,7 +126,7 @@ func TestNative_Parse_Metadata_Reader(t *testing.T) { t.Errorf("Reading file returned err: %v", err) } - got, err := client.Parse(bytes.NewReader(b)) + got, _, err := client.Parse(bytes.NewReader(b)) if err != nil { t.Errorf("Parse returned err: %v", err) } @@ -152,7 +152,7 @@ func TestNative_Parse_Metadata_String(t *testing.T) { t.Errorf("Reading file returned err: %v", err) } - got, err := client.Parse(string(b)) + got, _, err := client.Parse(string(b)) if err != nil { t.Errorf("Parse returned err: %v", err) } @@ -196,7 +196,7 @@ func TestNative_Parse_Parameters(t *testing.T) { t.Errorf("Reading file returned err: %v", err) } - got, err := client.Parse(b) + got, _, err := client.Parse(b) if err != nil { t.Errorf("Parse returned err: %v", err) } @@ -312,7 +312,7 @@ func TestNative_Parse_StagesPipeline(t *testing.T) { t.Errorf("Reading file returned err: %v", err) } - got, err := client.Parse(b) + got, _, err := client.Parse(b) if err != nil { t.Errorf("Parse returned err: %v", err) } @@ -404,7 +404,7 @@ func TestNative_Parse_StepsPipeline(t *testing.T) { t.Errorf("Reading file returned err: %v", err) } - got, err := client.Parse(b) + got, _, err := client.Parse(b) if err != nil { t.Errorf("Parse returned err: %v", err) } @@ -464,7 +464,7 @@ func TestNative_Parse_Secrets(t *testing.T) { t.Errorf("Reading file returned err: %v", err) } - got, err := client.Parse(b) + got, _, err := client.Parse(b) if err != nil { t.Errorf("Parse returned err: %v", err) @@ -537,7 +537,7 @@ func TestNative_Parse_Stages(t *testing.T) { t.Errorf("Reading file returned err: %v", err) } - got, err := client.Parse(b) + got, _, err := client.Parse(b) if err != nil { t.Errorf("Parse returned err: %v", err) @@ -592,7 +592,7 @@ func TestNative_Parse_Steps(t *testing.T) { t.Errorf("Reading file returned err: %v", err) } - got, err := client.Parse(b) + got, _, err := client.Parse(b) if err != nil { t.Errorf("Parse returned err: %v", err) @@ -618,7 +618,7 @@ func TestNative_ParseBytes_Metadata(t *testing.T) { t.Errorf("Reading file returned err: %v", err) } - got, err := ParseBytes(b) + got, _, err := ParseBytes(b) if err != nil { t.Errorf("ParseBytes returned err: %v", err) @@ -636,7 +636,7 @@ func TestNative_ParseBytes_Invalid(t *testing.T) { t.Errorf("Reading file returned err: %v", err) } - got, err := ParseBytes(b) + got, _, err := ParseBytes(b) if err == nil { t.Error("ParseBytes should have returned err") @@ -664,7 +664,7 @@ func TestNative_ParseFile_Metadata(t *testing.T) { defer f.Close() - got, err := ParseFile(f) + got, _, err := ParseFile(f) if err != nil { t.Errorf("ParseFile returned err: %v", err) @@ -684,7 +684,7 @@ func TestNative_ParseFile_Invalid(t *testing.T) { f.Close() - got, err := ParseFile(f) + got, _, err := ParseFile(f) if err == nil { t.Error("ParseFile should have returned err") @@ -705,7 +705,7 @@ func TestNative_ParsePath_Metadata(t *testing.T) { } // run test - got, err := ParsePath("testdata/metadata.yml") + got, _, err := ParsePath("testdata/metadata.yml") if err != nil { t.Errorf("ParsePath returned err: %v", err) @@ -718,7 +718,7 @@ func TestNative_ParsePath_Metadata(t *testing.T) { func TestNative_ParsePath_Invalid(t *testing.T) { // run test - got, err := ParsePath("testdata/foobar.yml") + got, _, err := ParsePath("testdata/foobar.yml") if err == nil { t.Error("ParsePath should have returned err") @@ -744,7 +744,7 @@ func TestNative_ParseReader_Metadata(t *testing.T) { t.Errorf("Reading file returned err: %v", err) } - got, err := ParseReader(bytes.NewReader(b)) + got, _, err := ParseReader(bytes.NewReader(b)) if err != nil { t.Errorf("ParseReader returned err: %v", err) @@ -757,7 +757,7 @@ func TestNative_ParseReader_Metadata(t *testing.T) { func TestNative_ParseReader_Invalid(t *testing.T) { // run test - got, err := ParseReader(FailReader{}) + got, _, err := ParseReader(FailReader{}) if err == nil { t.Error("ParseFile should have returned err") @@ -783,7 +783,7 @@ func TestNative_ParseString_Metadata(t *testing.T) { t.Errorf("Reading file returned err: %v", err) } - got, err := ParseString(string(b)) + got, _, err := ParseString(string(b)) if err != nil { t.Errorf("ParseString returned err: %v", err) diff --git a/compiler/native/substitute.go b/compiler/native/substitute.go index bdbd4de0..dd71486d 100644 --- a/compiler/native/substitute.go +++ b/compiler/native/substitute.go @@ -8,7 +8,7 @@ import ( "fmt" "strings" - "github.com/buildkite/yaml" + "github.com/goccy/go-yaml" "github.com/drone/envsubst" diff --git a/compiler/native/testdata/invalid.yml b/compiler/native/testdata/invalid.yml index f849b8d2..cf637de5 100644 --- a/compiler/native/testdata/invalid.yml +++ b/compiler/native/testdata/invalid.yml @@ -1,2 +1 @@ ---- -!@#$%^&*() +`` diff --git a/compiler/native/transform_test.go b/compiler/native/transform_test.go index fd82eba6..c1933e59 100644 --- a/compiler/native/transform_test.go +++ b/compiler/native/transform_test.go @@ -102,6 +102,9 @@ func TestNative_TransformStages(t *testing.T) { want := &pipeline.Build{ ID: "__0", Version: "v1", + Metadata: pipeline.Metadata{ + Clone: true, + }, Services: pipeline.ContainerSlice{ &pipeline.Container{ ID: "service___0_postgres backend", @@ -252,6 +255,9 @@ func TestNative_TransformSteps(t *testing.T) { want := &pipeline.Build{ ID: "__0", Version: "v1", + Metadata: pipeline.Metadata{ + Clone: true, + }, Services: pipeline.ContainerSlice{ &pipeline.Container{ ID: "service___0_postgres backend", diff --git a/compiler/native/validate.go b/compiler/native/validate.go deleted file mode 100644 index 224a8f90..00000000 --- a/compiler/native/validate.go +++ /dev/null @@ -1,123 +0,0 @@ -// Copyright (c) 2020 Target Brands, Inc. All rights reserved. -// -// Use of this source code is governed by the LICENSE file in this repository. - -package native - -import ( - "fmt" - - "github.com/go-vela/types/yaml" -) - -// Validate verifies the the yaml configuration is valid. -func (c *client) Validate(p *yaml.Build) error { - // check a version is provided - if len(p.Version) == 0 { - return fmt.Errorf("no version provided") - } - - // check that stages or steps are provided - if len(p.Stages) == 0 && len(p.Steps) == 0 { - return fmt.Errorf("no stages or steps provided") - } - - // check that stages and steps aren't provided - if len(p.Stages) > 0 && len(p.Steps) > 0 { - return fmt.Errorf("stages and steps provided") - } - - // validate the services block provided - err := validateServices(p.Services) - if err != nil { - return err - } - - // validate the stages block provided - err = validateStages(p.Stages) - if err != nil { - return err - } - - // validate the steps block provided - err = validateSteps(p.Steps) - if err != nil { - return err - } - - return nil -} - -// validateServices is a helper function that verifies the -// services block in the yaml configuration is valid. -func validateServices(s yaml.ServiceSlice) error { - for _, service := range s { - if len(service.Name) == 0 { - return fmt.Errorf("no name provided for service") - } - - if len(service.Image) == 0 { - return fmt.Errorf("no image provided for service %s", service.Name) - } - } - - return nil -} - -// validateStages is a helper function that verifies the -// stages block in the yaml configuration is valid. -func validateStages(s yaml.StageSlice) error { - for _, stage := range s { - if len(stage.Name) == 0 { - return fmt.Errorf("no name provided for stage") - } - - for _, step := range stage.Steps { - if len(step.Name) == 0 { - return fmt.Errorf("no name provided for step for stage %s", stage.Name) - } - - if len(step.Image) == 0 && len(step.Template.Name) == 0 { - return fmt.Errorf("no image or template provided for step %s for stage %s", step.Name, stage.Name) - } - - if step.Name == "clone" || step.Name == "init" { - continue - } - - if len(step.Commands) == 0 && len(step.Environment) == 0 && - len(step.Parameters) == 0 && len(step.Secrets) == 0 && - len(step.Template.Name) == 0 && !step.Detach { - return fmt.Errorf("no commands, environment, parameters, secrets or template provided for step %s for stage %s", step.Name, stage.Name) - } - } - } - - return nil -} - -// validateSteps is a helper function that verifies the -// steps block in the yaml configuration is valid. -func validateSteps(s yaml.StepSlice) error { - for _, step := range s { - if len(step.Name) == 0 { - return fmt.Errorf("no name provided for step") - } - - if len(step.Image) == 0 && len(step.Template.Name) == 0 { - return fmt.Errorf("no image or template provided for step %s", step.Name) - } - - if step.Name == "clone" || step.Name == "init" { - continue - } - - if len(step.Commands) == 0 && len(step.Environment) == 0 && - len(step.Parameters) == 0 && len(step.Secrets) == 0 && - len(step.Template.Name) == 0 && !step.Detach { - return fmt.Errorf("no commands, environment, parameters, secrets or template provided for step %s", step.Name) - } - } - - return nil -} diff --git a/compiler/native/validate_test.go b/compiler/native/validate_test.go deleted file mode 100644 index 02dab130..00000000 --- a/compiler/native/validate_test.go +++ /dev/null @@ -1,496 +0,0 @@ -// Copyright (c) 2020 Target Brands, Inc. All rights reserved. -// -// Use of this source code is governed by the LICENSE file in this repository. - -package native - -import ( - "flag" - "testing" - - "github.com/go-vela/types/raw" - "github.com/go-vela/types/yaml" - - "github.com/urfave/cli/v2" -) - -func TestNative_Validate_NoVersion(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - p := &yaml.Build{} - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} - -func TestNative_Validate_NoStagesOrSteps(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - p := &yaml.Build{ - Version: "v1", - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} - -func TestNative_Validate_StagesAndSteps(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Stages: yaml.StageSlice{ - &yaml.Stage{ - Name: str, - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Image: "alpine", - Name: str, - Pull: "always", - }, - }, - }, - }, - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Image: "alpine", - Name: str, - Pull: "always", - }, - }, - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} - -func TestNative_Validate_Services(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Services: yaml.ServiceSlice{ - &yaml.Service{ - Image: "postgres", - Name: str, - Ports: raw.StringSlice{"8080:8080"}, - }, - }, - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Image: "alpine", - Name: str, - Pull: "always", - }, - }, - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err != nil { - t.Errorf("Validate returned err: %v", err) - } -} - -func TestNative_Validate_Services_NoName(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Services: yaml.ServiceSlice{ - &yaml.Service{ - Image: "postgres", - Name: "", - Ports: raw.StringSlice{"8080:8080"}, - }, - }, - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Image: "alpine", - Name: str, - Pull: "always", - }, - }, - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} - -func TestNative_Validate_Services_NoImage(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Services: yaml.ServiceSlice{ - &yaml.Service{ - Image: "", - Name: str, - Ports: raw.StringSlice{"8080:8080"}, - }, - }, - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Image: "alpine", - Name: str, - Pull: "always", - }, - }, - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} - -func TestNative_Validate_Stages(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Stages: yaml.StageSlice{ - &yaml.Stage{ - Name: str, - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Image: "alpine", - Name: str, - Pull: "always", - }, - }, - }, - }, - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err != nil { - t.Errorf("Validate returned err: %v", err) - } -} - -func TestNative_Validate_Stages_NoName(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Stages: yaml.StageSlice{ - &yaml.Stage{ - Name: "", - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Name: str, - Pull: "always", - }, - }, - }, - }, - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} - -func TestNative_Validate_Stages_NoStepName(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Stages: yaml.StageSlice{ - &yaml.Stage{ - Name: str, - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Name: "", - Pull: "always", - }, - }, - }, - }, - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} - -func TestNative_Validate_Stages_NoImage(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Stages: yaml.StageSlice{ - &yaml.Stage{ - Name: str, - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Name: str, - Pull: "always", - }, - }, - }, - }, - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} - -func TestNative_Validate_Stages_NoCommands(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Stages: yaml.StageSlice{ - &yaml.Stage{ - Name: str, - Steps: yaml.StepSlice{ - &yaml.Step{ - Image: "alpine", - Name: str, - Pull: "always", - }, - }, - }, - }, - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} - -func TestNative_Validate_Steps(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Image: "alpine", - Name: str, - Pull: "always", - }, - }, - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err != nil { - t.Errorf("Validate returned err: %v", err) - } -} - -func TestNative_Validate_Steps_NoName(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - p := &yaml.Build{ - Version: "v1", - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Name: "", - Pull: "always", - }, - }, - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} - -func TestNative_Validate_Steps_NoImage(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Steps: yaml.StepSlice{ - &yaml.Step{ - Commands: raw.StringSlice{"echo hello"}, - Name: str, - Pull: "always", - }, - }, - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} - -func TestNative_Validate_Steps_NoCommands(t *testing.T) { - // setup types - set := flag.NewFlagSet("test", 0) - c := cli.NewContext(nil, set, nil) - - str := "foo" - p := &yaml.Build{ - Version: "v1", - Steps: yaml.StepSlice{ - &yaml.Step{ - Image: "alpine", - Name: str, - Pull: "always", - }, - }, - } - - // run test - compiler, err := New(c) - if err != nil { - t.Errorf("Unable to create new compiler: %v", err) - } - - err = compiler.Validate(p) - if err == nil { - t.Errorf("Validate should have returned err") - } -} diff --git a/go.mod b/go.mod index 4639b5c4..a46c0db5 100644 --- a/go.mod +++ b/go.mod @@ -4,12 +4,11 @@ go 1.15 require ( github.com/Masterminds/sprig/v3 v3.2.0 - github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3 github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/drone/envsubst v1.0.2 github.com/gin-gonic/gin v1.6.3 - github.com/go-playground/validator/v10 v10.4.0 // indirect - github.com/go-vela/types v0.6.1-0.20201113143106-92e626cf6b5b + github.com/go-vela/types v0.6.1-0.20210111181528-d3bb371e9ec6 + github.com/goccy/go-yaml v1.8.4 github.com/google/go-cmp v0.5.4 github.com/google/go-github/v24 v24.0.1 github.com/google/uuid v1.1.4 // indirect @@ -24,6 +23,7 @@ require ( go.starlark.net v0.0.0-20201014215153-dff0ae5b4820 golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5 - gopkg.in/yaml.v2 v2.3.0 + golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect k8s.io/apimachinery v0.20.1 ) diff --git a/go.sum b/go.sum index 8e8e5bd5..4fab4d72 100644 --- a/go.sum +++ b/go.sum @@ -45,8 +45,6 @@ github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= -github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3 h1:q+sMKdA6L8LyGVudTkpGoC73h6ak2iWSPFiFo/pFOU8= -github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3/go.mod h1:5hCug3EZaHXU3FdCA3gJm0YTNi+V+ooA2qNTiVpky4A= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= @@ -60,6 +58,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug= +github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM= github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= github.com/drone/envsubst v1.0.2 h1:dpYLMAspQHW0a8dZpLRKe9jCNvIGZPhCPrycZzIHdqo= @@ -71,6 +71,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= +github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= +github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= @@ -101,10 +103,12 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87 github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= -github.com/go-playground/validator/v10 v10.4.0 h1:72qIR/m8ybvL8L5TIyfgrigqkrw7kVYAvjEvpT85l70= -github.com/go-playground/validator/v10 v10.4.0/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= -github.com/go-vela/types v0.6.1-0.20201113143106-92e626cf6b5b h1:IyJb9Y3feA/d3/c+XLjo0+aT+fsXpVJ2rpC0oFOem9E= -github.com/go-vela/types v0.6.1-0.20201113143106-92e626cf6b5b/go.mod h1:6r6mWIPrTANBpHwAFAIii64VKtzlAzVagbm/wX5bHHk= +github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= +github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= +github.com/go-vela/types v0.6.1-0.20210111181528-d3bb371e9ec6 h1:zmdwRI81qWFTw7H37nIzu/Y3UoSD5cih0UwjeO3kI3o= +github.com/go-vela/types v0.6.1-0.20210111181528-d3bb371e9ec6/go.mod h1:ATtwTwp2l4jI4GUmw840xHZgHmg8FbZPo4g0azImggA= +github.com/goccy/go-yaml v1.8.4 h1:AOEdR7aQgbgwHznGe3BLkDQVujxCPUpHOZZcQcp8Y3M= +github.com/goccy/go-yaml v1.8.4/go.mod h1:U/jl18uSupI5rdI2jmuCswEA2htH9eXfferR3KfscvA= github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= @@ -214,9 +218,11 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= -github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= +github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ= @@ -239,6 +245,8 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -411,6 +419,8 @@ golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201112073958-5cba982894dd h1:5CtCZbICpIOFdgO940moixOPjc0178IU44m4EjOO5IY= golang.org/x/sys v0.0.0-20201112073958-5cba982894dd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 h1:DQmQoKxQWtyybCtX/3dIuDBcAhFszqq8YiNeS6sNu1c= +golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= @@ -565,6 +575,8 @@ gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/template/native/render.go b/template/native/render.go index 45ddee6f..0f29a785 100644 --- a/template/native/render.go +++ b/template/native/render.go @@ -9,7 +9,7 @@ import ( "github.com/Masterminds/sprig/v3" - yaml "gopkg.in/yaml.v2" + "github.com/goccy/go-yaml" ) // Render combines the template with the step in the yaml pipeline. diff --git a/template/native/render_test.go b/template/native/render_test.go index f0a42199..6a05ecab 100644 --- a/template/native/render_test.go +++ b/template/native/render_test.go @@ -8,11 +8,10 @@ import ( "io/ioutil" "testing" - "github.com/go-vela/types/raw" + yml "github.com/goccy/go-yaml" "github.com/google/go-cmp/cmp" - goyaml "gopkg.in/yaml.v2" - + "github.com/go-vela/types/raw" "github.com/go-vela/types/yaml" ) @@ -46,7 +45,7 @@ func TestNative_Render(t *testing.T) { t.Error(err) } b := &yaml.Build{} - err = goyaml.Unmarshal(sFile, b) + err = yml.Unmarshal(sFile, b) if err != nil { t.Error(err) } @@ -71,7 +70,7 @@ func TestNative_Render(t *testing.T) { t.Error(err) } w := &yaml.Build{} - err = goyaml.Unmarshal(wFile, w) + err = yml.Unmarshal(wFile, w) if err != nil { t.Error(err) } diff --git a/template/native/testdata/invalid.yml b/template/native/testdata/invalid.yml index f849b8d2..935e6d84 100644 --- a/template/native/testdata/invalid.yml +++ b/template/native/testdata/invalid.yml @@ -1,2 +1 @@ ---- -!@#$%^&*() +`` \ No newline at end of file diff --git a/template/starlark/render.go b/template/starlark/render.go index 3510f5e0..40b2cdd9 100644 --- a/template/starlark/render.go +++ b/template/starlark/render.go @@ -10,8 +10,8 @@ import ( "fmt" types "github.com/go-vela/types/yaml" + "github.com/goccy/go-yaml" "go.starlark.net/starlark" - "gopkg.in/yaml.v2" ) var ( diff --git a/template/starlark/render_test.go b/template/starlark/render_test.go index aeec9a6b..b96fe467 100644 --- a/template/starlark/render_test.go +++ b/template/starlark/render_test.go @@ -10,8 +10,8 @@ import ( "github.com/go-vela/types/raw" "github.com/go-vela/types/yaml" + goyaml "github.com/goccy/go-yaml" "github.com/google/go-cmp/cmp" - goyaml "gopkg.in/yaml.v2" ) func TestStarlark_Render(t *testing.T) {