diff --git a/hook/hook.go b/hook/hook.go index d23993e..f640590 100644 --- a/hook/hook.go +++ b/hook/hook.go @@ -384,13 +384,26 @@ func (h *Hooks) LoadFromFile(path string) error { // parse hook file for hooks file, e := ioutil.ReadFile(path) - if e != nil { return e } - e = json.Unmarshal(file, h) - return e + if e = json.Unmarshal(file, h); e != nil { + return e + } + + h.checkDeprecations() + return nil +} + +// checkDeprecations crawls through a Hooks definition checking for and handling +// deprecated values. +func (h Hooks) checkDeprecations() { + for i := range h { + if h[i].TriggerRule != nil && h[i].TriggerRule.Match != nil && h[i].TriggerRule.Match.Type == MatchRegex && h[i].TriggerRule.Match.Regex != "" && h[i].TriggerRule.Match.Value == "" { + h[i].TriggerRule.Match.Value = h[i].TriggerRule.Match.Regex + } + } } // Match iterates through Hooks and returns first one that matches the given ID, @@ -503,7 +516,7 @@ func (r NotRule) Evaluate(headers, query, payload *map[string]interface{}, body // MatchRule will evaluate to true based on the type type MatchRule struct { Type string `json:"type,omitempty"` - Regex string `json:"regex,omitempty"` + Regex string `json:"regex,omitempty"` // Regex is deprecated. Use Value instead. Secret string `json:"secret,omitempty"` Value string `json:"value,omitempty"` Parameter Argument `json:"parameter,omitempty"` @@ -523,7 +536,7 @@ func (r MatchRule) Evaluate(headers, query, payload *map[string]interface{}, bod case MatchValue: return arg == r.Value, nil case MatchRegex: - return regexp.MatchString(r.Regex, arg) + return regexp.MatchString(r.Value, arg) case MatchHashSHA1: _, err := CheckPayloadSignature(*body, r.Secret, arg) return err == nil, err diff --git a/hook/hook_test.go b/hook/hook_test.go index f4d5b8f..4844ff1 100644 --- a/hook/hook_test.go +++ b/hook/hook_test.go @@ -183,14 +183,14 @@ var matchRuleTests = []struct { err bool }{ {"value", "", "", "z", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, true, false}, - {"regex", "^z", "", "z", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, true, false}, + {"regex", "", "", "^z", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, true, false}, {"payload-hash-sha1", "", "secret", "", Argument{"header", "a"}, &map[string]interface{}{"a": "b17e04cbb22afa8ffbff8796fc1894ed27badd9e"}, nil, nil, []byte(`{"a": "z"}`), true, false}, // failures {"value", "", "", "X", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false, false}, - {"regex", "^X", "", "", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false, false}, + {"regex", "", "", "^X", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false, false}, {"value", "", "2", "X", Argument{"header", "a"}, &map[string]interface{}{"y": "z"}, nil, nil, []byte{}, false, false}, // reference invalid header // errors - {"regex", "*", "", "", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false, true}, // invalid regex + {"regex", "", "", "*", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false, true}, // invalid regex {"payload-hash-sha1", "", "secret", "", Argument{"header", "a"}, &map[string]interface{}{"a": ""}, nil, nil, []byte{}, false, true}, // invalid hmac } diff --git a/test/hooks.json.tmpl b/test/hooks.json.tmpl index 1839a18..45258d5 100644 --- a/test/hooks.json.tmpl +++ b/test/hooks.json.tmpl @@ -49,6 +49,18 @@ "name": "ref" } } + }, + { + "match": + { + "type": "regex", + "value": "refs.*", + "parameter": + { + "source": "payload", + "name": "ref" + } + } } ] }