From dc184d2737ff15e76a953cb76b34576e055e3ee9 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Sat, 24 Oct 2020 11:40:27 -0500 Subject: [PATCH] Fix OrRule logic on parameter lookup failures Fixes #471 --- internal/hook/hook.go | 4 ++- internal/hook/hook_test.go | 4 +-- test/hooks.json.tmpl | 70 ++++++++++++++++++++++++++++++++++++++ test/hooks.yaml.tmpl | 36 ++++++++++++++++++++ webhook_test.go | 26 ++++++++++++++ 5 files changed, 137 insertions(+), 3 deletions(-) diff --git a/internal/hook/hook.go b/internal/hook/hook.go index 157c457..c446aee 100644 --- a/internal/hook/hook.go +++ b/internal/hook/hook.go @@ -848,7 +848,9 @@ func (r OrRule) Evaluate(req *Request) (bool, error) { for _, v := range r { rv, err := v.Evaluate(req) if err != nil { - return false, err + if !IsParameterNodeError(err) { + return false, err + } } res = res || rv diff --git a/internal/hook/hook_test.go b/internal/hook/hook_test.go index 32bc848..9a548d7 100644 --- a/internal/hook/hook_test.go +++ b/internal/hook/hook_test.go @@ -657,13 +657,13 @@ var orRuleTests = []struct { }, // failures { - "invalid rule", + "missing parameter node", OrRule{ {Match: &MatchRule{"value", "", "", "z", Argument{"header", "a", "", false}, ""}}, }, map[string]interface{}{"Y": "Z"}, nil, nil, []byte{}, - false, true, + false, false, }, } diff --git a/test/hooks.json.tmpl b/test/hooks.json.tmpl index 3075762..b94ed1a 100644 --- a/test/hooks.json.tmpl +++ b/test/hooks.json.tmpl @@ -276,6 +276,76 @@ } ], }, + { + "id": "issue-471", + "execute-command": "{{ .Hookecho }}", + "response-message": "success", + "trigger-rule": + { + "or": + [ + { + "match": + { + "parameter": + { + "source": "payload", + "name": "foo" + }, + "type": "value", + "value": "bar" + } + }, + { + "match": + { + "parameter": + { + "source": "payload", + "name": "exists" + }, + "type": "value", + "value": 1 + } + } + ] + } + }, + { + "id": "issue-471-and", + "execute-command": "{{ .Hookecho }}", + "response-message": "success", + "trigger-rule": + { + "and": + [ + { + "match": + { + "parameter": + { + "source": "payload", + "name": "foo" + }, + "type": "value", + "value": "bar" + } + }, + { + "match": + { + "parameter": + { + "source": "payload", + "name": "exists" + }, + "type": "value", + "value": 1 + } + } + ] + } + }, { "id": "empty-payload-signature", "execute-command": "{{ .Hookecho }}", diff --git a/test/hooks.yaml.tmpl b/test/hooks.yaml.tmpl index 16aa8c1..325ef27 100644 --- a/test/hooks.yaml.tmpl +++ b/test/hooks.yaml.tmpl @@ -163,6 +163,42 @@ execute-command: '{{ .Hookecho }} foo' include-command-output-in-response: true +- id: issue-471 + execute-command: '{{ .Hookecho }}' + response-message: success + trigger-rule: + or: + - match: + parameter: + source: payload + name: foo + type: value + value: bar + - match: + parameter: + source: payload + name: exists + type: value + value: 1 + +- id: issue-471-and + execute-command: '{{ .Hookecho }}' + response-message: success + trigger-rule: + and: + - match: + parameter: + source: payload + name: foo + type: value + value: bar + - match: + parameter: + source: payload + name: exists + type: value + value: 1 + - id: empty-payload-signature include-command-output-in-response: true execute-command: '{{ .Hookecho }}' diff --git a/webhook_test.go b/webhook_test.go index c8a46cc..d12826d 100644 --- a/webhook_test.go +++ b/webhook_test.go @@ -606,6 +606,32 @@ binary data ``, }, + { + "issue-471", + "issue-471", + nil, + "POST", + nil, + "application/json", + `{"exists": 1}`, + http.StatusOK, + `success`, + ``, + }, + + { + "issue-471-and", + "issue-471-and", + nil, + "POST", + nil, + "application/json", + `{"exists": 1}`, + http.StatusOK, + `Hook rules were not satisfied.`, + `parameter node not found`, + }, + { "missing-cmd-arg", // missing head_commit.author.email "github",