Skip to content

Commit

Permalink
fix(status): only match when status exists (#72)
Browse files Browse the repository at this point in the history
* bug: only match when status exists

* fix: add additional test cases
  • Loading branch information
Neal committed May 21, 2020
1 parent 1f27570 commit 840634e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pipeline/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
62 changes: 62 additions & 0 deletions pipeline/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions pipeline/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions pipeline/ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down

0 comments on commit 840634e

Please sign in to comment.