diff --git a/pipeline/container.go b/pipeline/container.go index f5428015..e635abde 100644 --- a/pipeline/container.go +++ b/pipeline/container.go @@ -134,6 +134,7 @@ func (c *Container) Execute(r *RuleData) bool { // check if you need to run a status failure ruleset if !(c.Ruleset.If.Empty() && c.Ruleset.Unless.Empty()) && + !(c.Ruleset.If.NoStatus() && c.Ruleset.Unless.NoStatus()) && c.Ruleset.Match(r) { // approve the need to run the container execute = true @@ -144,6 +145,7 @@ func (c *Container) Execute(r *RuleData) bool { // check if you need to skip a status failure ruleset if strings.EqualFold(status, constants.StatusSuccess) && + !(c.Ruleset.If.NoStatus() && c.Ruleset.Unless.NoStatus()) && !(c.Ruleset.If.Empty() && c.Ruleset.Unless.Empty()) && c.Ruleset.Match(r) { r.Status = constants.StatusSuccess diff --git a/pipeline/container_test.go b/pipeline/container_test.go index 17cbda22..1fda2fa3 100644 --- a/pipeline/container_test.go +++ b/pipeline/container_test.go @@ -219,6 +219,26 @@ func TestPipeline_Container_Execute(t *testing.T) { }, want: true, }, + { // no status container with build success + container: &Container{ + Name: "branch/event/status", + Image: "alpine:latest", + Commands: []string{"echo \"Hey Vela\""}, + Ruleset: Ruleset{ + If: Rules{ + Branch: []string{"master"}, + Event: []string{constants.EventPush}, + }, + }, + }, + ruleData: &RuleData{ + Branch: "master", + Event: "push", + Repo: "foo/bar", + Status: "failure", + }, + want: false, + }, { // branch/event/status container with build success container: &Container{ Name: "branch/event/status", @@ -343,6 +363,48 @@ func TestPipeline_Container_Execute(t *testing.T) { }, want: true, }, + { // status unless success container with build success + container: &Container{ + Name: "status unless", + Image: "alpine:latest", + Commands: []string{"echo \"Hey Vela\""}, + Ruleset: Ruleset{ + Unless: Rules{ + Branch: []string{"master"}, + Event: []string{constants.EventPush}, + Status: []string{constants.StatusSuccess}, + }, + }, + }, + ruleData: &RuleData{ + Branch: "master", + Event: "push", + Repo: "foo/bar", + Status: "success", + }, + want: false, + }, + { // status unless success container with build success + container: &Container{ + Name: "status unless", + Image: "alpine:latest", + Commands: []string{"echo \"Hey Vela\""}, + Ruleset: Ruleset{ + Unless: Rules{ + Branch: []string{"dev"}, + Event: []string{constants.EventPush}, + Status: []string{constants.StatusSuccess}, + }, + }, + }, + ruleData: &RuleData{ + Branch: "master", + Event: "pull_request", + Repo: "foo/bar", + Status: "failure", + }, + want: true, + }, } // run tests diff --git a/pipeline/ruleset.go b/pipeline/ruleset.go index 2a22e6a4..ce9ce9f4 100644 --- a/pipeline/ruleset.go +++ b/pipeline/ruleset.go @@ -82,6 +82,17 @@ func (r *Ruleset) Match(from *RuleData) bool { return false } +// NoStatus returns true if the status field is empty +func (r *Rules) NoStatus() bool { + // return true if every ruletype is empty + if len(r.Status) == 0 { + return true + } + + // return false if any of the ruletype is provided + return false +} + // Empty returns true if the provided ruletypes are empty. func (r *Rules) Empty() bool { // return true if every ruletype is empty diff --git a/pipeline/ruleset_test.go b/pipeline/ruleset_test.go index 68e99916..792c8db2 100644 --- a/pipeline/ruleset_test.go +++ b/pipeline/ruleset_test.go @@ -192,6 +192,18 @@ func TestPipeline_Ruleset_Match(t *testing.T) { } } +func TestPipeline_Rules_NoStatus(t *testing.T) { + // setup types + r := Rules{} + + // run test + got := r.Empty() + + if !got { + t.Errorf("Rule NoStatus is %v, want true", got) + } +} + func TestPipeline_Rules_Empty(t *testing.T) { // setup types r := Rules{}