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

feat(rulesets): add instance key to ruleset #384

Merged
merged 2 commits into from
Aug 1, 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
3 changes: 3 additions & 0 deletions pipeline/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ func (c *Container) Execute(r *RuleData) (bool, error) {
c.Ruleset.If.Label = []string{}
c.Ruleset.Unless.Label = []string{}

c.Ruleset.If.Instance = []string{}
c.Ruleset.Unless.Instance = []string{}

// check if the build is in a running state
if strings.EqualFold(r.Status, constants.StatusRunning) {
// treat the ruleset status as success
Expand Down
54 changes: 31 additions & 23 deletions pipeline/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@
//
// swagger:model PipelineRules
Rules struct {
Branch Ruletype `json:"branch,omitempty" yaml:"branch,omitempty"`
Comment Ruletype `json:"comment,omitempty" yaml:"comment,omitempty"`
Event Ruletype `json:"event,omitempty" yaml:"event,omitempty"`
Path Ruletype `json:"path,omitempty" yaml:"path,omitempty"`
Repo Ruletype `json:"repo,omitempty" yaml:"repo,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"`
Label Ruletype `json:"label,omitempty" yaml:"label,omitempty"`
Parallel bool `json:"-" yaml:"-"`
Branch Ruletype `json:"branch,omitempty" yaml:"branch,omitempty"`
Comment Ruletype `json:"comment,omitempty" yaml:"comment,omitempty"`
Event Ruletype `json:"event,omitempty" yaml:"event,omitempty"`
Path Ruletype `json:"path,omitempty" yaml:"path,omitempty"`
Repo Ruletype `json:"repo,omitempty" yaml:"repo,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"`
Label Ruletype `json:"label,omitempty" yaml:"label,omitempty"`
Instance Ruletype `json:"instance,omitempty" yaml:"instance,omitempty"`
Parallel bool `json:"-" yaml:"-"`
}

// Ruletype is the pipeline representation of an element
Expand All @@ -50,16 +51,17 @@
// RuleData is the data to check our ruleset
// against for a step in a pipeline.
RuleData struct {
Branch string `json:"branch,omitempty" yaml:"branch,omitempty"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
Event string `json:"event,omitempty" yaml:"event,omitempty"`
Path []string `json:"path,omitempty" yaml:"path,omitempty"`
Repo string `json:"repo,omitempty" yaml:"repo,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"`
Label []string `json:"label,omitempty" yaml:"label,omitempty"`
Parallel bool `json:"-" yaml:"-"`
Branch string `json:"branch,omitempty" yaml:"branch,omitempty"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
Event string `json:"event,omitempty" yaml:"event,omitempty"`
Path []string `json:"path,omitempty" yaml:"path,omitempty"`
Repo string `json:"repo,omitempty" yaml:"repo,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"`
Label []string `json:"label,omitempty" yaml:"label,omitempty"`
Instance string `json:"instance,omitempty" yaml:"instance,omitempty"`
Parallel bool `json:"-" yaml:"-"`
}
)

Expand Down Expand Up @@ -114,7 +116,8 @@
len(r.Status) == 0 &&
len(r.Tag) == 0 &&
len(r.Target) == 0 &&
len(r.Label) == 0 {
len(r.Label) == 0 &&
len(r.Instance) == 0 {
return true
}

Expand All @@ -128,7 +131,7 @@
// ruletypes from the rules match the provided ruledata. For
// both operators, when none of the ruletypes from the rules
// match the provided ruledata, the function returns false.
func (r *Rules) Match(from *RuleData, matcher, op string) (bool, error) {

Check failure on line 134 in pipeline/ruleset.go

View workflow job for this annotation

GitHub Actions / full-review

cyclomatic complexity 31 of func `(*Rules).Match` is high (> 30) (gocyclo)

Check failure on line 134 in pipeline/ruleset.go

View workflow job for this annotation

GitHub Actions / diff-review

cyclomatic complexity 31 of func `(*Rules).Match` is high (> 30) (gocyclo)

Check failure on line 134 in pipeline/ruleset.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pipeline/ruleset.go#L134

cyclomatic complexity 31 of func `(*Rules).Match` is high (> 30) (gocyclo)
Raw output
pipeline/ruleset.go:134:1: cyclomatic complexity 31 of func `(*Rules).Match` is high (> 30) (gocyclo)
func (r *Rules) Match(from *RuleData, matcher, op string) (bool, error) {
^
status := true

var err error
Expand Down Expand Up @@ -180,11 +183,16 @@
return false, err
}

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

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

Expand Down
18 changes: 18 additions & 0 deletions pipeline/ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,18 @@ func TestPipeline_Rules_Match(t *testing.T) {
operator: "or",
want: true,
},
{
rules: &Rules{Event: []string{"push"}, Instance: []string{"http://localhost:8080"}},
data: &RuleData{Branch: "main", Event: "push", Repo: "octocat/hello-world", Status: "pending", Instance: "http://localhost:5432"},
operator: "and",
want: false,
},
{
rules: &Rules{Event: []string{"push"}, Instance: []string{"http://localhost:8080"}},
data: &RuleData{Branch: "main", Event: "push", Repo: "octocat/hello-world", Status: "pending", Instance: "http://localhost:8080"},
operator: "and",
want: true,
},
}

// run test
Expand Down Expand Up @@ -567,6 +579,9 @@ func TestPipeline_Ruletype_MatchAnd(t *testing.T) {
// Label with regexp matcher
{matcher: "regexp", rule: []string{"enhancement", "documentation"}, pattern: "documentation", want: true},
{matcher: "regexp", rule: []string{"enhancement", "documentation"}, pattern: "question", want: false},
// Instance with regexp matcher
{matcher: "regexp", rule: []string{"http://localhost:8080", "http://localhost:1234"}, pattern: "http://localhost:5432", want: false},
{matcher: "regexp", rule: []string{"http://localhost:8080", "http://localhost:1234"}, pattern: "http://localhost:8080", want: true},
}

// run test
Expand Down Expand Up @@ -647,6 +662,9 @@ func TestPipeline_Ruletype_MatchOr(t *testing.T) {
// Label with regexp matcher
{matcher: "regexp", rule: []string{"enhancement", "documentation"}, pattern: "documentation", want: true},
{matcher: "regexp", rule: []string{"enhancement", "documentation"}, pattern: "question", want: false},
// Instance with regexp matcher
{matcher: "regexp", rule: []string{"http://localhost:8080", "http://localhost:1234"}, pattern: "http://localhost:5432", want: false},
{matcher: "regexp", rule: []string{"http://localhost:8080", "http://localhost:1234"}, pattern: "http://localhost:8080", want: true},
}

// run test
Expand Down
59 changes: 32 additions & 27 deletions yaml/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ type (
// Rules is the yaml representation of the ruletypes
// from a ruleset block for a step in a pipeline.
Rules struct {
Branch []string `yaml:"branch,omitempty,flow" json:"branch,omitempty" jsonschema:"description=Limits the execution of a step to matching build branches.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Comment []string `yaml:"comment,omitempty,flow" json:"comment,omitempty" jsonschema:"description=Limits the execution of a step to matching a pull request comment.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
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-tag"`
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-tag"`
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-tag"`
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-tag"`
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-tag"`
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-tag"`
Label []string `yaml:"label,omitempty,flow" json:"label,omitempty" jsonschema:"description=Limits step execution to match on pull requests labels.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Branch []string `yaml:"branch,omitempty,flow" json:"branch,omitempty" jsonschema:"description=Limits the execution of a step to matching build branches.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Comment []string `yaml:"comment,omitempty,flow" json:"comment,omitempty" jsonschema:"description=Limits the execution of a step to matching a pull request comment.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
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-tag"`
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-tag"`
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-tag"`
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-tag"`
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-tag"`
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-tag"`
Label []string `yaml:"label,omitempty,flow" json:"label,omitempty" jsonschema:"description=Limits step execution to match on pull requests labels.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Instance []string `yaml:"instance,omitempty,flow" json:"instance,omitempty" jsonschema:"description=Limits step execution to match on certain instances.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
}
)

Expand Down Expand Up @@ -86,6 +87,7 @@ func (r *Ruleset) UnmarshalYAML(unmarshal func(interface{}) error) error {
advanced.If.Tag = append(advanced.If.Tag, simple.Tag...)
advanced.If.Target = append(advanced.If.Target, simple.Target...)
advanced.If.Label = append(advanced.If.Label, simple.Label...)
advanced.If.Instance = append(advanced.If.Instance, simple.Instance...)

// set ruleset `if` to advanced `if` rules
r.If = advanced.If
Expand All @@ -107,31 +109,33 @@ func (r *Ruleset) UnmarshalYAML(unmarshal func(interface{}) error) error {
// type to a pipeline Rules type.
func (r *Rules) ToPipeline() *pipeline.Rules {
return &pipeline.Rules{
Branch: r.Branch,
Comment: r.Comment,
Event: r.Event,
Path: r.Path,
Repo: r.Repo,
Status: r.Status,
Tag: r.Tag,
Target: r.Target,
Label: r.Label,
Branch: r.Branch,
Comment: r.Comment,
Event: r.Event,
Path: r.Path,
Repo: r.Repo,
Status: r.Status,
Tag: r.Tag,
Target: r.Target,
Label: r.Label,
Instance: r.Instance,
}
}

// UnmarshalYAML implements the Unmarshaler interface for the Rules type.
func (r *Rules) UnmarshalYAML(unmarshal func(interface{}) error) error {
// rules struct we try unmarshalling to
rules := new(struct {
Branch raw.StringSlice
Comment raw.StringSlice
Event raw.StringSlice
Path raw.StringSlice
Repo raw.StringSlice
Status raw.StringSlice
Tag raw.StringSlice
Target raw.StringSlice
Label raw.StringSlice
Branch raw.StringSlice
Comment raw.StringSlice
Event raw.StringSlice
Path raw.StringSlice
Repo raw.StringSlice
Status raw.StringSlice
Tag raw.StringSlice
Target raw.StringSlice
Label raw.StringSlice
Instance raw.StringSlice
})

// attempt to unmarshal rules
Expand All @@ -145,6 +149,7 @@ func (r *Rules) UnmarshalYAML(unmarshal func(interface{}) error) error {
r.Tag = rules.Tag
r.Target = rules.Target
r.Label = rules.Label
r.Instance = rules.Instance

// account for users who use non-scoped pull_request event
events := []string{}
Expand Down
72 changes: 38 additions & 34 deletions yaml/ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,55 @@ func TestYaml_Ruleset_ToPipeline(t *testing.T) {
{
ruleset: &Ruleset{
If: 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"},
Path: []string{"foo.txt"},
Repo: []string{"github/octocat"},
Status: []string{"success"},
Tag: []string{"v0.1.0"},
Target: []string{"production"},
Label: []string{"enhancement"},
Instance: []string{"http://localhost:8080"},
},
Unless: Rules{
Branch: []string{"main"},
Comment: []string{"real comment"},
Event: []string{"pull_request"},
Path: []string{"bar.txt"},
Repo: []string{"github/octocat"},
Status: []string{"failure"},
Tag: []string{"v0.2.0"},
Target: []string{"production"},
Branch: []string{"main"},
Comment: []string{"real comment"},
Event: []string{"pull_request"},
Path: []string{"bar.txt"},
Repo: []string{"github/octocat"},
Status: []string{"failure"},
Tag: []string{"v0.2.0"},
Target: []string{"production"},
Instance: []string{"http://localhost:8080"},
},
Matcher: "filepath",
Operator: "and",
Continue: false,
},
want: &pipeline.Ruleset{
If: 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"},
Path: []string{"foo.txt"},
Repo: []string{"github/octocat"},
Status: []string{"success"},
Tag: []string{"v0.1.0"},
Target: []string{"production"},
Label: []string{"enhancement"},
Instance: []string{"http://localhost:8080"},
},
Unless: pipeline.Rules{
Branch: []string{"main"},
Comment: []string{"real comment"},
Event: []string{"pull_request"},
Path: []string{"bar.txt"},
Repo: []string{"github/octocat"},
Status: []string{"failure"},
Tag: []string{"v0.2.0"},
Target: []string{"production"},
Branch: []string{"main"},
Comment: []string{"real comment"},
Event: []string{"pull_request"},
Path: []string{"bar.txt"},
Repo: []string{"github/octocat"},
Status: []string{"failure"},
Tag: []string{"v0.2.0"},
Target: []string{"production"},
Instance: []string{"http://localhost:8080"},
},
Matcher: "filepath",
Operator: "and",
Expand Down
Loading