mirror of
https://github.com/adnanh/webhook.git
synced 2025-10-04 21:51:02 +00:00
Proposal: Remove side-effects from hook package
This commit removes logging from the hook package and relies on returning errors to convey...errors. Encountered errors are returned immediately. This commit may alter the behavior of hook. If errors were logged in the past but the given function did not return immediately, this commit would change that behavior. Uses named errors and custom error types. You may be able to consolidate ArgumentError and SourceError, but I need to think about it some more. These changes should be transparent to the caller if they're expecting standard "error" types. Tests have been updated to validate error return values and provide test coverage for a few new lines of code introduced by this commit.
This commit is contained in:
parent
2afc6e6a54
commit
ee64968d00
3 changed files with 185 additions and 86 deletions
30
webhook.go
30
webhook.go
|
@ -185,8 +185,25 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
// handle hook
|
||||
for _, h := range matchedHooks {
|
||||
h.ParseJSONParameters(&headers, &query, &payload)
|
||||
if h.TriggerRule == nil || h.TriggerRule != nil && h.TriggerRule.Evaluate(&headers, &query, &payload, &body) {
|
||||
err := h.ParseJSONParameters(&headers, &query, &payload)
|
||||
if err != nil {
|
||||
log.Printf("error parsing JSON: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
var ok bool
|
||||
|
||||
if h.TriggerRule == nil {
|
||||
ok = true
|
||||
} else {
|
||||
ok, err = h.TriggerRule.Evaluate(&headers, &query, &payload, &body)
|
||||
if err != nil {
|
||||
log.Printf("error evaluating hook: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if ok && err == nil {
|
||||
log.Printf("%s hook triggered successfully\n", h.ID)
|
||||
|
||||
if h.CaptureCommandOutput {
|
||||
|
@ -213,10 +230,17 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func handleHook(h *hook.Hook, headers, query, payload *map[string]interface{}, body *[]byte) string {
|
||||
var err error
|
||||
|
||||
cmd := exec.Command(h.ExecuteCommand)
|
||||
cmd.Args = h.ExtractCommandArguments(headers, query, payload)
|
||||
cmd.Dir = h.CommandWorkingDirectory
|
||||
|
||||
cmd.Args, err = h.ExtractCommandArguments(headers, query, payload)
|
||||
if err != nil {
|
||||
log.Printf("error extracting command arguments: %s", err)
|
||||
return ""
|
||||
}
|
||||
|
||||
log.Printf("executing %s (%s) with arguments %s using %s as cwd\n", h.ExecuteCommand, cmd.Path, cmd.Args, cmd.Dir)
|
||||
|
||||
out, err := cmd.CombinedOutput()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue