match all hooks with the same id

This commit is contained in:
Adnan Hajdarevic 2015-05-27 09:16:26 +02:00
parent 4350685330
commit f1ebc440a4
3 changed files with 82 additions and 48 deletions

View file

@ -260,6 +260,23 @@ func (h *Hooks) Match(id string) *Hook {
return nil 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 // Rules is a structure that contains one of the valid rule types
type Rules struct { type Rules struct {
And *AndRule `json:"and"` And *AndRule `json:"and"`

View file

@ -25,7 +25,7 @@ import (
) )
const ( const (
version = "2.3.3" version = "2.3.4"
) )
var ( var (
@ -144,10 +144,10 @@ func main() {
func hookHandler(w http.ResponseWriter, r *http.Request) { func hookHandler(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"] id := mux.Vars(r)["id"]
hook := hooks.Match(id) matchedHooks := hooks.MatchAll(id)
if hook != nil { if matchedHooks != nil {
log.Printf("%s got matched\n", id) log.Printf("%s got matched (%d time(s))\n", id, len(matchedHooks))
body, err := ioutil.ReadAll(r.Body) body, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
@ -183,13 +183,26 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
hook.ParseJSONParameters(&headers, &query, &payload)
// handle hook // 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) go handleHook(hook, &headers, &query, &payload, &body)
// send the hook defined response message // send the hook defined response message
fmt.Fprintf(w, hook.ResponseMessage) 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 { } else {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Hook not found.") 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) { 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 := exec.Command(hook.ExecuteCommand)
cmd.Args = hook.ExtractCommandArguments(headers, query, payload) cmd.Args = hook.ExtractCommandArguments(headers, query, payload)
cmd.Dir = hook.CommandWorkingDirectory cmd.Dir = hook.CommandWorkingDirectory
@ -213,10 +223,8 @@ func handleHook(hook *hook.Hook, headers, query, payload *map[string]interface{}
if err != nil { if err != nil {
log.Printf("stderr: %+v\n", err) log.Printf("stderr: %+v\n", err)
} }
log.Printf("finished handling %s\n", hook.ID) log.Printf("finished handling %s\n", hook.ID)
} else {
log.Printf("%s hook did not get triggered\n", hook.ID)
}
} }
func reloadHooks() { func reloadHooks() {

View file

@ -23,7 +23,7 @@ import (
) )
const ( const (
version = "2.3.3" version = "2.3.4"
) )
var ( var (
@ -133,10 +133,10 @@ func main() {
func hookHandler(w http.ResponseWriter, r *http.Request) { func hookHandler(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"] id := mux.Vars(r)["id"]
hook := hooks.Match(id) matchedHooks := hooks.MatchAll(id)
if hook != nil { if matchedHooks != nil {
log.Printf("%s got matched\n", id) log.Printf("%s got matched (%d time(s))\n", id, len(matchedHooks))
body, err := ioutil.ReadAll(r.Body) body, err := ioutil.ReadAll(r.Body)
if err != nil { 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) 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) go handleHook(hook, &headers, &query, &payload, &body)
// send the hook defined response message // send the hook defined response message
fmt.Fprintf(w, hook.ResponseMessage) 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 { } else {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Hook not found.") 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) { 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 := exec.Command(hook.ExecuteCommand)
cmd.Args = hook.ExtractCommandArguments(headers, query, payload) cmd.Args = hook.ExtractCommandArguments(headers, query, payload)
cmd.Dir = hook.CommandWorkingDirectory cmd.Dir = hook.CommandWorkingDirectory
@ -202,10 +213,8 @@ func handleHook(hook *hook.Hook, headers, query, payload *map[string]interface{}
if err != nil { if err != nil {
log.Printf("stderr: %+v\n", err) log.Printf("stderr: %+v\n", err)
} }
log.Printf("finished handling %s\n", hook.ID) log.Printf("finished handling %s\n", hook.ID)
} else {
log.Printf("%s hook did not get triggered\n", hook.ID)
}
} }
func reloadHooks() { func reloadHooks() {