diff --git a/hook/hook.go b/hook/hook.go index 0cc2740..926a651 100644 --- a/hook/hook.go +++ b/hook/hook.go @@ -300,6 +300,7 @@ type Hook struct { PassArgumentsToCommand []Argument `json:"pass-arguments-to-command,omitempty"` JSONStringParameters []Argument `json:"parse-parameters-as-json,omitempty"` TriggerRule *Rules `json:"trigger-rule,omitempty"` + TriggerRuleMismatchCode int `json:"trigger-rule-mismatch-code,omitempty"` } // ParseJSONParameters decodes specified arguments to JSON objects and replaces the diff --git a/test/hooks.json.tmpl b/test/hooks.json.tmpl index 4d94449..c731c9a 100644 --- a/test/hooks.json.tmpl +++ b/test/hooks.json.tmpl @@ -4,6 +4,7 @@ "execute-command": "{{ .Hookecho }}", "command-working-directory": "/", "include-command-output-in-response": true, + "trigger-rule-mismatch-code": 400, "pass-environment-to-command": [ { @@ -59,6 +60,7 @@ "command-working-directory": "/", "include-command-output-in-response": false, "response-message": "success", + "trigger-rule-mismatch-code": 999, "parse-parameters-as-json": [ { "source": "payload", diff --git a/webhook.go b/webhook.go index d4908de..92f49b6 100644 --- a/webhook.go +++ b/webhook.go @@ -242,6 +242,17 @@ func hookHandler(w http.ResponseWriter, r *http.Request) { return } + // Check if a return code is configured for the hook + if matchedHook.TriggerRuleMismatchCode != 0 { + // Check if the configured return code is supported by the http package + // by testing if there is a StatusText for this code. + if len(http.StatusText(matchedHook.TriggerRuleMismatchCode)) > 0 { + w.WriteHeader(matchedHook.TriggerRuleMismatchCode) + } else { + log.Printf("%s got matched, but the configured return code %d is unknown - defaulting to 200\n", matchedHook.ID, matchedHook.TriggerRuleMismatchCode) + } + } + // if none of the hooks got triggered log.Printf("%s got matched, but didn't get triggered because the trigger rules were not satisfied\n", matchedHook.ID) diff --git a/webhook_test.go b/webhook_test.go index 0b25de9..eef1e0a 100644 --- a/webhook_test.go +++ b/webhook_test.go @@ -515,5 +515,10 @@ env: HOOK_head_commit.timestamp=2013-03-12T08:14:29-07:00 `, }, - {"empty payload", "github", nil, `{}`, false, http.StatusOK, `Hook rules were not satisfied.`}, + // test with custom return code + {"empty payload", "github", nil, `{}`, false, http.StatusBadRequest, `Hook rules were not satisfied.`}, + // test with custom invalid http code, should default to 200 OK + {"empty payload", "bitbucket", nil, `{}`, false, http.StatusOK, `Hook rules were not satisfied.`}, + // test with no configured http return code, should default to 200 OK + {"empty payload", "gitlab", nil, `{}`, false, http.StatusOK, `Hook rules were not satisfied.`}, }