-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix bug in evaluation of filters with filtersLogicalOperator=or
Signed-off-by: jsvk <[email protected]>
- Loading branch information
Showing
2 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ limitations under the License. | |
package dependencies | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -623,6 +624,74 @@ func TestFilter(t *testing.T) { | |
assert.True(t, pass) | ||
}) | ||
|
||
t.Run("filtersLogicalOperator == 'or' with only a subset of filters specified", func(t *testing.T) { | ||
filter := &v1alpha1.EventDependencyFilter{ | ||
Exprs: []v1alpha1.ExprFilter{ | ||
{ | ||
// C | ||
Expr: `committer_email == "definitely-wrong"`, // this will be wrong | ||
Fields: []v1alpha1.PayloadField{ | ||
{ | ||
Path: "body.head_commit.committer.email", | ||
Name: "committer_email", | ||
}, | ||
}, | ||
}, | ||
}, | ||
Data: []v1alpha1.DataFilter{ // these evaluate to false | ||
{ | ||
Path: "body.ref", | ||
Type: "string", | ||
Value: []string{"definitely-wrong"}, | ||
}, | ||
{ | ||
Path: "body.repository.full_name", | ||
Type: "string", | ||
Value: []string{"foo/bar"}, | ||
}, | ||
{ | ||
Path: "[body.commits.#.modified.#()#,body.commits.#.added.#()#,body.commits.#.removed.#()#]|@flatten|@flatten", | ||
Type: "string", | ||
Value: []string{"^.*README.*$", "^.*service_metadata.*$"}, | ||
}, | ||
}, | ||
} | ||
|
||
eventDataBytes, err := json.Marshal(map[string]interface{}{ | ||
"body": map[string]interface{}{ | ||
"ref": "foo", | ||
"head_commit": map[string]interface{}{ | ||
"committer": map[string]interface{}{ | ||
"email": "[email protected]", | ||
}, | ||
}, | ||
"repository": map[string]interface{}{ | ||
"full_name": "anything/anything", | ||
}, | ||
}, | ||
}) | ||
|
||
assert.NoError(t, err) | ||
|
||
// should return false because the two filters above evaluate to false | ||
filtersLogicalOperator := v1alpha1.OrLogicalOperator | ||
|
||
now := time.Now().UTC() | ||
event := &v1alpha1.Event{ | ||
Context: &v1alpha1.EventContext{ | ||
Time: metav1.Time{ | ||
Time: time.Date(now.Year(), now.Month(), now.Day(), 16, 36, 34, 0, time.UTC), | ||
}, | ||
}, | ||
Data: eventDataBytes, | ||
} | ||
|
||
pass, err := filterEvent(filter, filtersLogicalOperator, event) | ||
|
||
assert.NoError(t, err) | ||
assert.False(t, pass) | ||
}) | ||
|
||
t.Run("test advanced logic: (A || B) || (C || D)", func(t *testing.T) { | ||
// data filter: A || B == false | ||
// expr filter: C || D == true | ||
|