Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(pipeline): add ruledata flags for vela validate pipeline #547

Merged
merged 6 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 Expand Up @@ -442,12 +476,12 @@
t.Error(err)
}
}
got, err := validateFile(tt.args.path)

Check failure on line 479 in action/pipeline/validate_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/pipeline/validate_test.go#L479

assignments should only be cuddled with other assignments (wsl)
Raw output
action/pipeline/validate_test.go:479:4: assignments should only be cuddled with other assignments (wsl)
			got, err := validateFile(tt.args.path)
			^
if (err != nil) != tt.wantErr {

Check failure on line 480 in action/pipeline/validate_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/pipeline/validate_test.go#L480

only one cuddle assignment allowed before if statement (wsl)
Raw output
action/pipeline/validate_test.go:480:4: only one cuddle assignment allowed before if statement (wsl)
			if (err != nil) != tt.wantErr {
			^
t.Errorf("validateFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {

Check failure on line 484 in action/pipeline/validate_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/pipeline/validate_test.go#L484

if statements should only be cuddled with assignments (wsl)
Raw output
action/pipeline/validate_test.go:484:4: if statements should only be cuddled with assignments (wsl)
			if got != tt.want {
			^
t.Errorf("validateFile() got = %v, want %v", got, tt.want)
}
})
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
Loading