Skip to content

Commit

Permalink
enhance(pipeline): add ruledata flags for vela validate pipeline (#547
Browse files Browse the repository at this point in the history
)

* init commit

* fixing things

* add tests

* remove conflicting t
  • Loading branch information
ecrupper authored Apr 8, 2024
1 parent 63d5835 commit c24173e
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 16 deletions.
1 change: 1 addition & 0 deletions action/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Config struct {
Branch string
Comment string
Event string
Status string
Tag string
Target string
Org string
Expand Down
23 changes: 23 additions & 0 deletions action/pipeline/testdata/ruleset.yml
Original file line number Diff line number Diff line change
@@ -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"
50 changes: 44 additions & 6 deletions action/pipeline/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions action/pipeline/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion command/pipeline/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
49 changes: 49 additions & 0 deletions command/pipeline/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions command/pipeline/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit c24173e

Please sign in to comment.