mirror of
https://github.com/adnanh/webhook.git
synced 2025-05-23 13:52:29 +00:00
separated github from post handler
This commit is contained in:
parent
478d189de8
commit
a5d79fddd9
2 changed files with 78 additions and 29 deletions
|
@ -14,6 +14,7 @@ type Hook struct {
|
||||||
Command string `json:"command"`
|
Command string `json:"command"`
|
||||||
Cwd string `json:"cwd"`
|
Cwd string `json:"cwd"`
|
||||||
Secret string `json:"secret"`
|
Secret string `json:"secret"`
|
||||||
|
Args []string `json:"args"`
|
||||||
Rule rules.Rule `json:"trigger-rule"`
|
Rule rules.Rule `json:"trigger-rule"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,6 +50,14 @@ func (h *Hook) UnmarshalJSON(j []byte) error {
|
||||||
h.Secret = v.(string)
|
h.Secret = v.(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v, ok := m["args"]; ok {
|
||||||
|
h.Args = make([]string, 0)
|
||||||
|
|
||||||
|
for i := range v.([]interface{}) {
|
||||||
|
h.Args = append(h.Args, v.([]interface{})[i].(string))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if v, ok := m["trigger-rule"]; ok {
|
if v, ok := m["trigger-rule"]; ok {
|
||||||
rule := v.(map[string]interface{})
|
rule := v.(map[string]interface{})
|
||||||
|
|
||||||
|
@ -150,5 +159,9 @@ func (h *Hooks) SetDefaults() {
|
||||||
if h.list[i].Cwd == "" {
|
if h.list[i].Cwd == "" {
|
||||||
h.list[i].Cwd = "."
|
h.list[i].Cwd = "."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if h.list[i].Args == nil {
|
||||||
|
h.list[i].Args = make([]string, 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
86
webhook.go
86
webhook.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -70,27 +71,7 @@ func rootHandler() string {
|
||||||
return fmt.Sprintf("webhook %s running for %s serving %d hook(s)\n", version, time.Since(appStart).String(), webhooks.Count())
|
return fmt.Sprintf("webhook %s running for %s serving %d hook(s)\n", version, time.Since(appStart).String(), webhooks.Count())
|
||||||
}
|
}
|
||||||
|
|
||||||
func hookHandler(req *http.Request, params martini.Params) string {
|
func githubHandler(id string, body []byte, signature string, params interface{}) {
|
||||||
defer req.Body.Close()
|
|
||||||
body, err := ioutil.ReadAll(req.Body)
|
|
||||||
if err != nil {
|
|
||||||
l4g.Warn("Error occurred while trying to read the request body: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
payloadJSON := make(map[string]interface{})
|
|
||||||
|
|
||||||
if req.Header.Get("Content-Type") == "application/json" {
|
|
||||||
decoder := json.NewDecoder(strings.NewReader(string(body)))
|
|
||||||
decoder.UseNumber()
|
|
||||||
|
|
||||||
err := decoder.Decode(&payloadJSON)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
l4g.Warn("Error occurred while trying to parse the payload as JSON: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
go func(id string, body []byte, signature string, params interface{}) {
|
|
||||||
if hook := webhooks.Match(id, params); hook != nil {
|
if hook := webhooks.Match(id, params); hook != nil {
|
||||||
if hook.Secret != "" {
|
if hook.Secret != "" {
|
||||||
if signature == "" {
|
if signature == "" {
|
||||||
|
@ -108,11 +89,66 @@ func hookHandler(req *http.Request, params martini.Params) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command(hook.Command, "", "", hook.Cwd)
|
cmd := exec.Command(hook.Command)
|
||||||
out, _ := cmd.Output()
|
cmd.Dir = hook.Cwd
|
||||||
l4g.Info("Hook %s triggered successfully! Command output:\n%s", hook.ID, out)
|
out, err := cmd.Output()
|
||||||
|
l4g.Info("Hook %s triggered successfully! Command output:\n%s\nError: %+v", hook.ID, out, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultPostHookHandler(id string, formValues url.Values) {
|
||||||
|
if hook := webhooks.Match(id, make(map[string]interface{})); hook != nil {
|
||||||
|
args := make([]string, 0)
|
||||||
|
args = append(args, hook.Command)
|
||||||
|
for i := range hook.Args {
|
||||||
|
if arg := formValues[hook.Args[i]]; len(arg) > 0 {
|
||||||
|
args = append(args, arg[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(hook.Command)
|
||||||
|
cmd.Args = args
|
||||||
|
cmd.Dir = hook.Cwd
|
||||||
|
|
||||||
|
out, err := cmd.Output()
|
||||||
|
|
||||||
|
l4g.Info("Hook %s triggered successfully! Command output:\n%s\nError: %+v", hook.ID, out, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func hookHandler(req *http.Request, params martini.Params) string {
|
||||||
|
if req.Header.Get("Content-Type") == "application/json" {
|
||||||
|
defer req.Body.Close()
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(req.Body)
|
||||||
|
if err != nil {
|
||||||
|
l4g.Warn("Error occurred while trying to read the request body: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
payloadJSON := make(map[string]interface{})
|
||||||
|
|
||||||
|
decoder := json.NewDecoder(strings.NewReader(string(body)))
|
||||||
|
decoder.UseNumber()
|
||||||
|
|
||||||
|
err = decoder.Decode(&payloadJSON)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
l4g.Warn("Error occurred while trying to parse the payload as JSON: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
githubSignature := ""
|
||||||
|
|
||||||
|
if len(req.Header.Get("X-Hub-Signature")) > 5 {
|
||||||
|
githubSignature = req.Header.Get("X-Hub-Signature")[5:]
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(req.Header.Get("User-Agent"), "Github") {
|
||||||
|
go githubHandler(params["id"], body, githubSignature, payloadJSON)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
req.ParseForm()
|
||||||
|
go defaultPostHookHandler(params["id"], req.Form)
|
||||||
}
|
}
|
||||||
}(params["id"], body, req.Header.Get("X-Hub-Signature")[5:], payloadJSON)
|
|
||||||
|
|
||||||
return "Got it, thanks. :-)"
|
return "Got it, thanks. :-)"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue