Skip to content

Commit

Permalink
feat: add sender rule for pipelines (#1206)
Browse files Browse the repository at this point in the history
* refactor(pipeline): use server API types for pipeline and migrate compiler types

* gci

* feat: add sender rule for pipelines

---------

Co-authored-by: David May <[email protected]>
  • Loading branch information
ecrupper and wass3rw3rk authored Oct 25, 2024
1 parent a2b0d91 commit 237ac81
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 60 deletions.
28 changes: 20 additions & 8 deletions api/pipeline/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ func prepareRuleData(c *gin.Context) *pipeline.RuleData {
comment := c.Query("comment")
// capture the event type parameter
event := c.Query("event")
// capture the instance parameter
instance := c.Query("instance")
// capture the label parameter
labelSet := c.QueryArray("label")
// capture the repo parameter
ruleDataRepo := c.Query("repo")
// capture the sender parameter
sender := c.Query("sender")
// capture the status type parameter
status := c.Query("status")
// capture the tag parameter
Expand All @@ -134,20 +140,26 @@ func prepareRuleData(c *gin.Context) *pipeline.RuleData {
if len(branch) > 0 ||
len(comment) > 0 ||
len(event) > 0 ||
len(instance) > 0 ||
len(labelSet) > 0 ||
len(pathSet) > 0 ||
len(ruleDataRepo) > 0 ||
len(sender) > 0 ||
len(status) > 0 ||
len(tag) > 0 ||
len(target) > 0 {
return &pipeline.RuleData{
Branch: branch,
Comment: comment,
Event: event,
Path: pathSet,
Repo: ruleDataRepo,
Status: status,
Tag: tag,
Target: target,
Branch: branch,
Comment: comment,
Event: event,
Instance: instance,
Label: labelSet,
Path: pathSet,
Repo: ruleDataRepo,
Sender: sender,
Status: status,
Tag: tag,
Target: target,
}
}

Expand Down
38 changes: 22 additions & 16 deletions api/pipeline/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,30 @@ func TestPrepareRuleData(t *testing.T) {
{
name: "all params provided",
parameters: map[string]string{
"branch": "main",
"comment": "Test comment",
"event": "push",
"repo": "my-repo",
"status": "success",
"tag": "v1.0.0",
"target": "production",
"path": "README.md",
"branch": "main",
"comment": "Test comment",
"event": "push",
"instance": "vela-server",
"label": "bug",
"repo": "my-repo",
"sender": "octocat",
"status": "success",
"tag": "v1.0.0",
"target": "production",
"path": "README.md",
},
want: &pipeline.RuleData{
Branch: "main",
Comment: "Test comment",
Event: "push",
Repo: "my-repo",
Status: "success",
Tag: "v1.0.0",
Target: "production",
Path: []string{"README.md"},
Branch: "main",
Comment: "Test comment",
Event: "push",
Instance: "vela-server",
Label: []string{"bug"},
Repo: "my-repo",
Sender: "octocat",
Status: "success",
Tag: "v1.0.0",
Target: "production",
Path: []string{"README.md"},
},
},
{
Expand Down
1 change: 1 addition & 0 deletions compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (c *client) Compile(ctx context.Context, v interface{}) (*pipeline.Build, *
Event: event,
Path: c.files,
Repo: c.repo.GetFullName(),
Sender: c.build.GetSender(),
Tag: strings.TrimPrefix(c.build.GetRef(), "refs/tags/"),
Target: c.build.GetDeploy(),
Label: c.labels,
Expand Down
12 changes: 10 additions & 2 deletions compiler/types/pipeline/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type (
Event Ruletype `json:"event,omitempty" yaml:"event,omitempty"`
Path Ruletype `json:"path,omitempty" yaml:"path,omitempty"`
Repo Ruletype `json:"repo,omitempty" yaml:"repo,omitempty"`
Sender Ruletype `json:"sender,omitempty" yaml:"sender,omitempty"`
Status Ruletype `json:"status,omitempty" yaml:"status,omitempty"`
Tag Ruletype `json:"tag,omitempty" yaml:"tag,omitempty"`
Target Ruletype `json:"target,omitempty" yaml:"target,omitempty"`
Expand All @@ -56,6 +57,7 @@ type (
Event string `json:"event,omitempty" yaml:"event,omitempty"`
Path []string `json:"path,omitempty" yaml:"path,omitempty"`
Repo string `json:"repo,omitempty" yaml:"repo,omitempty"`
Sender string `json:"sender,omitempty" yaml:"sender,omitempty"`
Status string `json:"status,omitempty" yaml:"status,omitempty"`
Tag string `json:"tag,omitempty" yaml:"tag,omitempty"`
Target string `json:"target,omitempty" yaml:"target,omitempty"`
Expand Down Expand Up @@ -113,6 +115,7 @@ func (r *Rules) Empty() bool {
len(r.Event) == 0 &&
len(r.Path) == 0 &&
len(r.Repo) == 0 &&
len(r.Sender) == 0 &&
len(r.Status) == 0 &&
len(r.Tag) == 0 &&
len(r.Target) == 0 &&
Expand Down Expand Up @@ -168,6 +171,11 @@ func (r *Rules) Match(from *RuleData, matcher, op string) (bool, error) {
return false, err
}

matchSender, err := r.Sender.MatchSingle(from.Sender, matcher, op)
if err != nil {
return false, err
}

matchTag, err := r.Tag.MatchSingle(from.Tag, matcher, op)
if err != nil {
return false, err
Expand All @@ -190,9 +198,9 @@ func (r *Rules) Match(from *RuleData, matcher, op string) (bool, error) {

switch op {
case constants.OperatorOr:
return (matchBranch || matchComment || matchEvent || matchPath || matchRepo || matchTag || matchTarget || matchLabel || matchInstance || status), nil
return (matchBranch || matchComment || matchEvent || matchPath || matchRepo || matchSender || matchTag || matchTarget || matchLabel || matchInstance || status), nil
default:
return (matchBranch && matchComment && matchEvent && matchPath && matchRepo && matchTag && matchTarget && matchLabel && matchInstance && status), nil
return (matchBranch && matchComment && matchEvent && matchPath && matchRepo && matchSender && matchTag && matchTarget && matchLabel && matchInstance && status), nil
}
}

Expand Down
5 changes: 5 additions & 0 deletions compiler/types/pipeline/ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func TestPipeline_Ruleset_Match(t *testing.T) {
data: &RuleData{Branch: "dev", Comment: "ok to test", Event: "push", Repo: "octocat/hello-world", Status: "failure", Tag: "refs/heads/main", Target: ""},
want: true,
},
{
ruleset: &Ruleset{If: Rules{Sender: []string{"octocat"}}, Operator: "and"},
data: &RuleData{Branch: "dev", Comment: "ok to test", Event: "push", Repo: "octocat/hello-world", Sender: "octocat", Status: "pending", Tag: "refs/heads/main", Target: ""},
want: true,
},
// If with or operator
{
ruleset: &Ruleset{If: Rules{Branch: []string{"main"}, Event: []string{"push"}}, Operator: "or"},
Expand Down
5 changes: 5 additions & 0 deletions compiler/types/yaml/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type (
Event []string `yaml:"event,omitempty,flow" json:"event,omitempty" jsonschema:"description=Limits the execution of a step to matching build events.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-key"`
Path []string `yaml:"path,omitempty,flow" json:"path,omitempty" jsonschema:"description=Limits the execution of a step to matching files changed in a repository.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-key"`
Repo []string `yaml:"repo,omitempty,flow" json:"repo,omitempty" jsonschema:"description=Limits the execution of a step to matching repos.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-key"`
Sender []string `yaml:"sender,omitempty,flow" json:"sender,omitempty" jsonschema:"description=Limits the execution of a step to matching build senders.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-key"`
Status []string `yaml:"status,omitempty,flow" json:"status,omitempty" jsonschema:"enum=[failure],enum=[success],description=Limits the execution of a step to matching build statuses.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-key"`
Tag []string `yaml:"tag,omitempty,flow" json:"tag,omitempty" jsonschema:"description=Limits the execution of a step to matching build tag references.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-key"`
Target []string `yaml:"target,omitempty,flow" json:"target,omitempty" jsonschema:"description=Limits the execution of a step to matching build deployment targets.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-key"`
Expand Down Expand Up @@ -83,6 +84,7 @@ func (r *Ruleset) UnmarshalYAML(unmarshal func(interface{}) error) error {
advanced.If.Event = append(advanced.If.Event, simple.Event...)
advanced.If.Path = append(advanced.If.Path, simple.Path...)
advanced.If.Repo = append(advanced.If.Repo, simple.Repo...)
advanced.If.Sender = append(advanced.If.Sender, simple.Sender...)
advanced.If.Status = append(advanced.If.Status, simple.Status...)
advanced.If.Tag = append(advanced.If.Tag, simple.Tag...)
advanced.If.Target = append(advanced.If.Target, simple.Target...)
Expand Down Expand Up @@ -114,6 +116,7 @@ func (r *Rules) ToPipeline() *pipeline.Rules {
Event: r.Event,
Path: r.Path,
Repo: r.Repo,
Sender: r.Sender,
Status: r.Status,
Tag: r.Tag,
Target: r.Target,
Expand All @@ -131,6 +134,7 @@ func (r *Rules) UnmarshalYAML(unmarshal func(interface{}) error) error {
Event raw.StringSlice
Path raw.StringSlice
Repo raw.StringSlice
Sender raw.StringSlice
Status raw.StringSlice
Tag raw.StringSlice
Target raw.StringSlice
Expand All @@ -145,6 +149,7 @@ func (r *Rules) UnmarshalYAML(unmarshal func(interface{}) error) error {
r.Comment = rules.Comment
r.Path = rules.Path
r.Repo = rules.Repo
r.Sender = rules.Sender
r.Status = rules.Status
r.Tag = rules.Tag
r.Target = rules.Target
Expand Down
82 changes: 48 additions & 34 deletions compiler/types/yaml/ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestYaml_Ruleset_ToPipeline(t *testing.T) {
Event: []string{"push", "pull_request:labeled"},
Path: []string{"foo.txt"},
Repo: []string{"github/octocat"},
Sender: []string{"octocat"},
Status: []string{"success"},
Tag: []string{"v0.1.0"},
Target: []string{"production"},
Expand All @@ -38,6 +39,7 @@ func TestYaml_Ruleset_ToPipeline(t *testing.T) {
Event: []string{"pull_request"},
Path: []string{"bar.txt"},
Repo: []string{"github/octocat"},
Sender: []string{"octokitty"},
Status: []string{"failure"},
Tag: []string{"v0.2.0"},
Target: []string{"production"},
Expand All @@ -54,6 +56,7 @@ func TestYaml_Ruleset_ToPipeline(t *testing.T) {
Event: []string{"push", "pull_request:labeled"},
Path: []string{"foo.txt"},
Repo: []string{"github/octocat"},
Sender: []string{"octocat"},
Status: []string{"success"},
Tag: []string{"v0.1.0"},
Target: []string{"production"},
Expand All @@ -66,6 +69,7 @@ func TestYaml_Ruleset_ToPipeline(t *testing.T) {
Event: []string{"pull_request"},
Path: []string{"bar.txt"},
Repo: []string{"github/octocat"},
Sender: []string{"octokitty"},
Status: []string{"failure"},
Tag: []string{"v0.2.0"},
Target: []string{"production"},
Expand Down Expand Up @@ -98,14 +102,17 @@ func TestYaml_Ruleset_UnmarshalYAML(t *testing.T) {
file: "testdata/ruleset_simple.yml",
want: &Ruleset{
If: Rules{
Branch: []string{"main"},
Comment: []string{"test comment"},
Event: []string{"push"},
Path: []string{"foo.txt"},
Repo: []string{"github/octocat"},
Status: []string{"success"},
Tag: []string{"v0.1.0"},
Target: []string{"production"},
Branch: []string{"main"},
Comment: []string{"test comment"},
Event: []string{"push"},
Instance: []string{"vela-server"},
Label: []string{"bug"},
Path: []string{"foo.txt"},
Repo: []string{"github/octocat"},
Sender: []string{"octocat"},
Status: []string{"success"},
Tag: []string{"v0.1.0"},
Target: []string{"production"},
},
Matcher: "filepath",
Operator: "and",
Expand Down Expand Up @@ -172,26 +179,30 @@ func TestYaml_Rules_ToPipeline(t *testing.T) {
}{
{
rules: &Rules{
Branch: []string{"main"},
Comment: []string{"test comment"},
Event: []string{"push", "pull_request:labeled"},
Path: []string{"foo.txt"},
Repo: []string{"github/octocat"},
Status: []string{"success"},
Tag: []string{"v0.1.0"},
Target: []string{"production"},
Label: []string{"enhancement"},
Branch: []string{"main"},
Comment: []string{"test comment"},
Event: []string{"push", "pull_request:labeled"},
Instance: []string{"vela-server"},
Path: []string{"foo.txt"},
Repo: []string{"github/octocat"},
Sender: []string{"octocat"},
Status: []string{"success"},
Tag: []string{"v0.1.0"},
Target: []string{"production"},
Label: []string{"enhancement"},
},
want: &pipeline.Rules{
Branch: []string{"main"},
Comment: []string{"test comment"},
Event: []string{"push", "pull_request:labeled"},
Path: []string{"foo.txt"},
Repo: []string{"github/octocat"},
Status: []string{"success"},
Tag: []string{"v0.1.0"},
Target: []string{"production"},
Label: []string{"enhancement"},
Branch: []string{"main"},
Comment: []string{"test comment"},
Event: []string{"push", "pull_request:labeled"},
Instance: []string{"vela-server"},
Path: []string{"foo.txt"},
Repo: []string{"github/octocat"},
Sender: []string{"octocat"},
Status: []string{"success"},
Tag: []string{"v0.1.0"},
Target: []string{"production"},
Label: []string{"enhancement"},
},
},
}
Expand Down Expand Up @@ -223,14 +234,17 @@ func TestYaml_Rules_UnmarshalYAML(t *testing.T) {
failure: false,
file: "testdata/ruleset_simple.yml",
want: &Rules{
Branch: []string{"main"},
Comment: []string{"test comment"},
Event: []string{"push"},
Path: []string{"foo.txt"},
Repo: []string{"github/octocat"},
Status: []string{"success"},
Tag: []string{"v0.1.0"},
Target: []string{"production"},
Branch: []string{"main"},
Comment: []string{"test comment"},
Event: []string{"push"},
Instance: []string{"vela-server"},
Label: []string{"bug"},
Path: []string{"foo.txt"},
Repo: []string{"github/octocat"},
Sender: []string{"octocat"},
Status: []string{"success"},
Tag: []string{"v0.1.0"},
Target: []string{"production"},
},
},
{
Expand Down
3 changes: 3 additions & 0 deletions compiler/types/yaml/testdata/ruleset_simple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ branch: main
comment: "test comment"
continue: true
event: push
instance: vela-server
label: bug
path: foo.txt
repo: github/octocat
sender: octocat
status: success
tag: v0.1.0
target: production

0 comments on commit 237ac81

Please sign in to comment.