mirror of
https://github.com/adnanh/webhook.git
synced 2025-05-23 05:42:30 +00:00
match all hooks with the same id
This commit is contained in:
parent
4350685330
commit
f1ebc440a4
3 changed files with 82 additions and 48 deletions
17
hook/hook.go
17
hook/hook.go
|
@ -260,6 +260,23 @@ func (h *Hooks) Match(id string) *Hook {
|
|||
return nil
|
||||
}
|
||||
|
||||
// MatchAll iterates through Hooks and returns all of the hooks that match the
|
||||
// given ID, if no hook matches the given ID, nil is returned
|
||||
func (h *Hooks) MatchAll(id string) []*Hook {
|
||||
matchedHooks := make([]*Hook, 0)
|
||||
for i := range *h {
|
||||
if (*h)[i].ID == id {
|
||||
matchedHooks = append(matchedHooks, &(*h)[i])
|
||||
}
|
||||
}
|
||||
|
||||
if len(matchedHooks) > 0 {
|
||||
return matchedHooks
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Rules is a structure that contains one of the valid rule types
|
||||
type Rules struct {
|
||||
And *AndRule `json:"and"`
|
||||
|
|
32
webhook.go
32
webhook.go
|
@ -25,7 +25,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
version = "2.3.3"
|
||||
version = "2.3.4"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -144,10 +144,10 @@ func main() {
|
|||
func hookHandler(w http.ResponseWriter, r *http.Request) {
|
||||
id := mux.Vars(r)["id"]
|
||||
|
||||
hook := hooks.Match(id)
|
||||
matchedHooks := hooks.MatchAll(id)
|
||||
|
||||
if hook != nil {
|
||||
log.Printf("%s got matched\n", id)
|
||||
if matchedHooks != nil {
|
||||
log.Printf("%s got matched (%d time(s))\n", id, len(matchedHooks))
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
|
@ -183,13 +183,26 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
hook.ParseJSONParameters(&headers, &query, &payload)
|
||||
|
||||
// handle hook
|
||||
for _, hook := range matchedHooks {
|
||||
hook.ParseJSONParameters(&headers, &query, &payload)
|
||||
if hook.TriggerRule == nil || hook.TriggerRule != nil && hook.TriggerRule.Evaluate(&headers, &query, &payload, &body) {
|
||||
log.Printf("%s hook triggered successfully\n", hook.ID)
|
||||
|
||||
go handleHook(hook, &headers, &query, &payload, &body)
|
||||
|
||||
// send the hook defined response message
|
||||
fmt.Fprintf(w, hook.ResponseMessage)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// if none of the hooks got triggered
|
||||
log.Printf("%s got matched (%d time(s)), but didn't get triggered because the trigger rules were not satisfied\n", matchedHooks[0].ID, len(matchedHooks))
|
||||
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
fmt.Fprintf(w, "Hook rules were not satisfied.")
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
fmt.Fprintf(w, "Hook not found.")
|
||||
|
@ -197,9 +210,6 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func handleHook(hook *hook.Hook, headers, query, payload *map[string]interface{}, body *[]byte) {
|
||||
if hook.TriggerRule == nil || hook.TriggerRule != nil && hook.TriggerRule.Evaluate(headers, query, payload, body) {
|
||||
log.Printf("%s hook triggered successfully\n", hook.ID)
|
||||
|
||||
cmd := exec.Command(hook.ExecuteCommand)
|
||||
cmd.Args = hook.ExtractCommandArguments(headers, query, payload)
|
||||
cmd.Dir = hook.CommandWorkingDirectory
|
||||
|
@ -213,10 +223,8 @@ func handleHook(hook *hook.Hook, headers, query, payload *map[string]interface{}
|
|||
if err != nil {
|
||||
log.Printf("stderr: %+v\n", err)
|
||||
}
|
||||
|
||||
log.Printf("finished handling %s\n", hook.ID)
|
||||
} else {
|
||||
log.Printf("%s hook did not get triggered\n", hook.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func reloadHooks() {
|
||||
|
|
|
@ -23,7 +23,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
version = "2.3.3"
|
||||
version = "2.3.4"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -133,10 +133,10 @@ func main() {
|
|||
func hookHandler(w http.ResponseWriter, r *http.Request) {
|
||||
id := mux.Vars(r)["id"]
|
||||
|
||||
hook := hooks.Match(id)
|
||||
matchedHooks := hooks.MatchAll(id)
|
||||
|
||||
if hook != nil {
|
||||
log.Printf("%s got matched\n", id)
|
||||
if matchedHooks != nil {
|
||||
log.Printf("%s got matched (%d time(s))\n", id, len(matchedHooks))
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
|
@ -172,13 +172,27 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
// handle hook
|
||||
for _, hook := range matchedHooks {
|
||||
hook.ParseJSONParameters(&headers, &query, &payload)
|
||||
|
||||
// handle hook
|
||||
if hook.TriggerRule == nil || hook.TriggerRule != nil && hook.TriggerRule.Evaluate(&headers, &query, &payload, &body) {
|
||||
log.Printf("%s hook triggered successfully\n", hook.ID)
|
||||
|
||||
go handleHook(hook, &headers, &query, &payload, &body)
|
||||
|
||||
// send the hook defined response message
|
||||
fmt.Fprintf(w, hook.ResponseMessage)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// if none of the hooks got triggered
|
||||
log.Printf("%s got matched (%d time(s)), but didn't get triggered because the trigger rules were not satisfied\n", matchedHooks[0].ID, len(matchedHooks))
|
||||
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
fmt.Fprintf(w, "Hook rules were not satisfied.")
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
fmt.Fprintf(w, "Hook not found.")
|
||||
|
@ -186,9 +200,6 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func handleHook(hook *hook.Hook, headers, query, payload *map[string]interface{}, body *[]byte) {
|
||||
if hook.TriggerRule == nil || hook.TriggerRule != nil && hook.TriggerRule.Evaluate(headers, query, payload, body) {
|
||||
log.Printf("%s hook triggered successfully\n", hook.ID)
|
||||
|
||||
cmd := exec.Command(hook.ExecuteCommand)
|
||||
cmd.Args = hook.ExtractCommandArguments(headers, query, payload)
|
||||
cmd.Dir = hook.CommandWorkingDirectory
|
||||
|
@ -202,10 +213,8 @@ func handleHook(hook *hook.Hook, headers, query, payload *map[string]interface{}
|
|||
if err != nil {
|
||||
log.Printf("stderr: %+v\n", err)
|
||||
}
|
||||
|
||||
log.Printf("finished handling %s\n", hook.ID)
|
||||
} else {
|
||||
log.Printf("%s hook did not get triggered\n", hook.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func reloadHooks() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue