From c24173eb0b58f6e8ad2e58b88c63c4be354482a5 Mon Sep 17 00:00:00 2001 From: Easton Crupper <65553218+ecrupper@users.noreply.github.com> Date: Mon, 8 Apr 2024 18:05:42 -0400 Subject: [PATCH] enhance(pipeline): add ruledata flags for `vela validate pipeline` (#547) * init commit * fixing things * add tests * remove conflicting t --- action/pipeline/pipeline.go | 1 + action/pipeline/testdata/ruleset.yml | 23 +++++++++++++ action/pipeline/validate.go | 50 ++++++++++++++++++++++++---- action/pipeline/validate_test.go | 34 +++++++++++++++++++ command/pipeline/exec.go | 1 - command/pipeline/validate.go | 49 +++++++++++++++++++++++++++ command/pipeline/validate_test.go | 7 ++++ go.mod | 6 ++-- go.sum | 12 +++---- 9 files changed, 167 insertions(+), 16 deletions(-) create mode 100644 action/pipeline/testdata/ruleset.yml diff --git a/action/pipeline/pipeline.go b/action/pipeline/pipeline.go index 22b2bdb1..2e647f59 100644 --- a/action/pipeline/pipeline.go +++ b/action/pipeline/pipeline.go @@ -9,6 +9,7 @@ type Config struct { Branch string Comment string Event string + Status string Tag string Target string Org string diff --git a/action/pipeline/testdata/ruleset.yml b/action/pipeline/testdata/ruleset.yml new file mode 100644 index 00000000..d99dde15 --- /dev/null +++ b/action/pipeline/testdata/ruleset.yml @@ -0,0 +1,23 @@ +version: "1" + +steps: + - name: always + image: alpine + commands: + - echo always + + - name: push to main + image: alpine + ruleset: + event: push + branch: main + commands: + - echo "push to main" + + - name: tag of v1 + image: alpine + ruleset: + event: push + tag: v1 + commands: + - echo "tag of v1" \ No newline at end of file diff --git a/action/pipeline/validate.go b/action/pipeline/validate.go index 55c5b851..46a592a2 100644 --- a/action/pipeline/validate.go +++ b/action/pipeline/validate.go @@ -12,6 +12,8 @@ import ( "github.com/go-vela/sdk-go/vela" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" + "github.com/go-vela/types/pipeline" + "github.com/go-vela/types/yaml" "github.com/go-vela/server/compiler" @@ -107,12 +109,40 @@ func (c *Config) ValidateLocal(client compiler.Engine) error { // set pipelineType within client client.WithRepo(&library.Repo{PipelineType: &c.PipelineType}) - logrus.Tracef("compiling pipeline %s", path) + var p *yaml.Build + + if len(c.Branch) > 0 || + len(c.Comment) > 0 || + len(c.Event) > 0 || + len(c.FileChangeset) > 0 || + len(c.Tag) > 0 || + len(c.Target) > 0 { + logrus.Debugf("compiling pipeline with ruledata") + + // define ruledata + ruleData := &pipeline.RuleData{ + Branch: c.Branch, + Comment: c.Comment, + Event: c.Event, + Path: c.FileChangeset, + Status: c.Status, + Tag: c.Tag, + Target: c.Target, + } - // compile the object into a pipeline - p, _, err := client.CompileLite(path, false) - if err != nil { - return err + // compile the object into a pipeline with ruledata + p, _, err = client.CompileLite(path, ruleData, false) + if err != nil { + return err + } + } else { + logrus.Debugf("compiling pipeline") + + // compile the object into a pipeline without ruledata + p, _, err = client.CompileLite(path, nil, false) + if err != nil { + return err + } } // check to see if locally provided templates were included in compilation @@ -155,7 +185,15 @@ func (c *Config) ValidateRemote(client *vela.Client) error { // // https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#PipelineOptions opts := &vela.PipelineOptions{ - Output: c.Output, + Output: c.Output, + Branch: c.Branch, + Comment: c.Comment, + Event: c.Event, + Repo: c.Repo, + Status: c.Status, + Tag: c.Tag, + Target: c.Target, + Path: c.FileChangeset, } // send API call to validate a pipeline diff --git a/action/pipeline/validate_test.go b/action/pipeline/validate_test.go index cdaf3b61..f8f53dc3 100644 --- a/action/pipeline/validate_test.go +++ b/action/pipeline/validate_test.go @@ -288,6 +288,40 @@ func TestPipeline_Config_ValidateLocal(t *testing.T) { TemplateFiles: []string{"foo:testdata/templates/template.yml"}, }, }, + { + name: "pipeline with rulesets - no ruledata provided", + failure: false, + config: &Config{ + Action: "validate", + File: "ruleset.yml", + Path: "testdata", + Type: "", + }, + }, + { + name: "pipeline with rulesets - push to main", + failure: false, + config: &Config{ + Action: "validate", + File: "ruleset.yml", + Path: "testdata", + Type: "", + Branch: "main", + Event: "push", + }, + }, + { + name: "pipeline with rulesets - tag of v1", + failure: false, + config: &Config{ + Action: "validate", + File: "ruleset.yml", + Path: "testdata", + Type: "", + Event: "tag", + Tag: "v1", + }, + }, } // run tests diff --git a/command/pipeline/exec.go b/command/pipeline/exec.go index fbec4f58..633d022d 100644 --- a/command/pipeline/exec.go +++ b/command/pipeline/exec.go @@ -50,7 +50,6 @@ var CommandExec = &cli.Command{ &cli.StringFlag{ EnvVars: []string{"VELA_TAG", "PIPELINE_TAG", "VELA_BUILD_TAG"}, Name: "tag", - Aliases: []string{"t"}, Usage: "provide the build tag for the pipeline", }, &cli.StringFlag{ diff --git a/command/pipeline/validate.go b/command/pipeline/validate.go index 42e687ef..28f532cf 100644 --- a/command/pipeline/validate.go +++ b/command/pipeline/validate.go @@ -99,6 +99,48 @@ var CommandValidate = &cli.Command{ Value: false, }, + // RuleData Flags + &cli.StringFlag{ + EnvVars: []string{"VELA_BRANCH", "PIPELINE_BRANCH", "VELA_BUILD_BRANCH"}, + Name: "branch", + Aliases: []string{"b"}, + Usage: "provide the build branch for the pipeline", + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_COMMENT", "PIPELINE_COMMENT", "VELA_BUILD_COMMENT"}, + Name: "comment", + Aliases: []string{"c"}, + Usage: "provide the build comment for the pipeline", + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_EVENT", "PIPELINE_EVENT", "VELA_BUILD_EVENT"}, + Name: "event", + Aliases: []string{"e"}, + Usage: "provide the build event for the pipeline", + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_STATUS", "PIPELINE_STATUS", "VELA_BUILD_STATUS"}, + Name: "status", + Usage: "provide the expected build status for the local validation", + Value: "success", + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_TAG", "PIPELINE_TAG", "VELA_BUILD_TAG"}, + Name: "tag", + Usage: "provide the build tag for the pipeline", + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_TARGET", "PIPELINE_TARGET", "VELA_BUILD_TARGET"}, + Name: "target", + Usage: "provide the build target for the pipeline", + }, + &cli.StringSliceFlag{ + EnvVars: []string{"VELA_FILE_CHANGESET", "FILE_CHANGESET"}, + Name: "file-changeset", + Aliases: []string{"fcs"}, + Usage: "provide a list of files changed for ruleset matching", + }, + // Compiler Flags &cli.StringFlag{ @@ -164,6 +206,13 @@ func validate(c *cli.Context) error { TemplateFiles: c.StringSlice("template-file"), Remote: c.Bool("remote"), PipelineType: c.String("pipeline-type"), + Branch: c.String("branch"), + Comment: c.String("comment"), + Event: c.String("event"), + FileChangeset: c.StringSlice("file-changeset"), + Status: c.String("status"), + Tag: c.String("tag"), + Target: c.String("target"), } // validate pipeline configuration diff --git a/command/pipeline/validate_test.go b/command/pipeline/validate_test.go index a4b8d03c..3585760d 100644 --- a/command/pipeline/validate_test.go +++ b/command/pipeline/validate_test.go @@ -30,6 +30,13 @@ func TestPipeline_Validate(t *testing.T) { fullSet.String("repo", "octocat", "doc") fullSet.String("output", "json", "doc") fullSet.String("pipeline-type", "yaml", "doc") + fullSet.String("branch", "main", "doc") + fullSet.String("comment", "comment", "doc") + fullSet.String("event", "push", "doc") + fullSet.String("status", "success", "doc") + fullSet.String("tag", "v0.0.0", "doc") + fullSet.String("target", "production", "doc") + fullSet.String("file-changeset", "README.md,main,go", "doc") fullSet.Uint64("compiler-starlark-exec-limit", 10000, "doc") fullSet.Bool("remote", true, "doc") diff --git a/go.mod b/go.mod index 876d00f0..da1332d2 100644 --- a/go.mod +++ b/go.mod @@ -10,9 +10,9 @@ require ( github.com/dustin/go-humanize v1.0.1 github.com/gin-gonic/gin v1.9.1 github.com/go-git/go-git/v5 v5.11.0 - github.com/go-vela/sdk-go v0.23.3-0.20240319181130-4a7c245c93ae - github.com/go-vela/server v0.23.4-0.20240401175144-f591935d2fc9 - github.com/go-vela/types v0.23.4-0.20240405190958-ea618bf19708 + github.com/go-vela/sdk-go v0.23.3-0.20240408141059-b3581213c0f2 + github.com/go-vela/server v0.23.4-0.20240405160933-fb31ea5a6e96 + github.com/go-vela/types v0.23.4-0.20240405205548-f24f795ac0b7 github.com/go-vela/worker v0.23.2 github.com/golang-jwt/jwt/v5 v5.2.1 github.com/gosuri/uitable v0.0.4 diff --git a/go.sum b/go.sum index 689c18ea..c470f2be 100644 --- a/go.sum +++ b/go.sum @@ -116,12 +116,12 @@ github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= -github.com/go-vela/sdk-go v0.23.3-0.20240319181130-4a7c245c93ae h1:E5sgPxZsuiB00hcAgR/TpzRcovy+GVFzTSZfPHMIfPY= -github.com/go-vela/sdk-go v0.23.3-0.20240319181130-4a7c245c93ae/go.mod h1:4iOo5uuh4S3V//7ipZ9ZxvXc1wR2EGxDCeTqpom8rfU= -github.com/go-vela/server v0.23.4-0.20240401175144-f591935d2fc9 h1:R3TxguOk3JsIRoZn0oQBTLZRDIj4Xpeon+L/YJOF2Vw= -github.com/go-vela/server v0.23.4-0.20240401175144-f591935d2fc9/go.mod h1:Rbe6vgYe3gao8sBcALlhrM2YH4yu2cxJAFpWdDUbdZY= -github.com/go-vela/types v0.23.4-0.20240405190958-ea618bf19708 h1:ETr/ZW0Z+cXT2leqY6iQUxGX2q/QGHW6QME5DcsTiHw= -github.com/go-vela/types v0.23.4-0.20240405190958-ea618bf19708/go.mod h1:mEF9dLkk00rUXf/t39n2WvXZgJbxnPEEWy+DHqIlRUo= +github.com/go-vela/sdk-go v0.23.3-0.20240408141059-b3581213c0f2 h1:Pc3CIjccAeocrgYIxcDqw3gNfY/7dF74Ex1zkHUuLXw= +github.com/go-vela/sdk-go v0.23.3-0.20240408141059-b3581213c0f2/go.mod h1:K/cbiN9wdkV5UYLZWDX3f66XSfERW6FOVlE/K9r/Q7g= +github.com/go-vela/server v0.23.4-0.20240405160933-fb31ea5a6e96 h1:MI0NwjyqvRBXaTY8mlE5EeDTZ4k3zeaL6hJJBxgE7TA= +github.com/go-vela/server v0.23.4-0.20240405160933-fb31ea5a6e96/go.mod h1:Rbe6vgYe3gao8sBcALlhrM2YH4yu2cxJAFpWdDUbdZY= +github.com/go-vela/types v0.23.4-0.20240405205548-f24f795ac0b7 h1:3mN7ej69dMH3Vis3G/tPLzLL0Rfp8nR5qd0gpj5ejRM= +github.com/go-vela/types v0.23.4-0.20240405205548-f24f795ac0b7/go.mod h1:mEF9dLkk00rUXf/t39n2WvXZgJbxnPEEWy+DHqIlRUo= github.com/go-vela/worker v0.23.2 h1:ypYzhLk94xkAUbpBb9d+QU8rw4jVQC9rIOCIZ2XNb90= github.com/go-vela/worker v0.23.2/go.mod h1:M1GzqlX6bIi/p+Fb6HuJ/uGHzPbaQBS6KVreClInZKg= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=