From 0906ae1217434e256ee09a42f2ba72c1f339e4df Mon Sep 17 00:00:00 2001 From: Nicolas Carlier Date: Thu, 25 Sep 2014 12:19:51 +0000 Subject: [PATCH] feat: Move JSON decoder inside each hook. --- src/hook/bitbucket_hook.go | 22 ++++++++++++++++++++++ src/hook/docker_hook.go | 14 ++++++++++++++ src/hook/github_hook.go | 14 ++++++++++++++ src/hook/hook_factory.go | 2 ++ src/notification/http_notifier.go | 4 ++-- src/webhookd.go | 4 +--- 6 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/hook/bitbucket_hook.go b/src/hook/bitbucket_hook.go index d3c8341..014d20b 100644 --- a/src/hook/bitbucket_hook.go +++ b/src/hook/bitbucket_hook.go @@ -1,7 +1,9 @@ package hook import ( + "encoding/json" "fmt" + "net/http" ) type BitbucketRecord struct { @@ -18,3 +20,23 @@ func (r BitbucketRecord) GetURL() string { func (r BitbucketRecord) GetName() string { return r.Repository.Slug } + +func (r BitbucketRecord) Decode(req *http.Request) error { + if err := req.ParseForm(); err != nil { + return err + } + + if payload, ok := req.PostForm["payload"]; ok { + err := json.Unmarshal([]byte(payload[0]), &r) + if err != nil { + return err + } + } else { + decoder := json.NewDecoder(req.Body) + err := decoder.Decode(&r) + if err != nil { + return err + } + } + return nil +} diff --git a/src/hook/docker_hook.go b/src/hook/docker_hook.go index e0627a3..40e554d 100644 --- a/src/hook/docker_hook.go +++ b/src/hook/docker_hook.go @@ -1,5 +1,10 @@ package hook +import ( + "encoding/json" + "net/http" +) + type DockerRecord struct { Repository struct { Name string `json:"repo_name"` @@ -14,3 +19,12 @@ func (r DockerRecord) GetURL() string { func (r DockerRecord) GetName() string { return r.Repository.Name } + +func (r DockerRecord) Decode(req *http.Request) error { + decoder := json.NewDecoder(req.Body) + err := decoder.Decode(&r) + if err != nil { + return err + } + return nil +} diff --git a/src/hook/github_hook.go b/src/hook/github_hook.go index 27d5943..ae93b18 100644 --- a/src/hook/github_hook.go +++ b/src/hook/github_hook.go @@ -1,5 +1,10 @@ package hook +import ( + "encoding/json" + "net/http" +) + type GithubRecord struct { Repository struct { Name string `json:"name"` @@ -14,3 +19,12 @@ func (r GithubRecord) GetURL() string { func (r GithubRecord) GetName() string { return r.Repository.Name } + +func (r GithubRecord) Decode(req *http.Request) error { + decoder := json.NewDecoder(req.Body) + err := decoder.Decode(&r) + if err != nil { + return err + } + return nil +} diff --git a/src/hook/hook_factory.go b/src/hook/hook_factory.go index ee1266c..c6c7286 100644 --- a/src/hook/hook_factory.go +++ b/src/hook/hook_factory.go @@ -2,11 +2,13 @@ package hook import ( "errors" + "net/http" ) type Record interface { GetURL() string GetName() string + Decode(r *http.Request) error } func RecordFactory(hookname string) (Record, error) { diff --git a/src/notification/http_notifier.go b/src/notification/http_notifier.go index eb645dc..5f0eaf3 100644 --- a/src/notification/http_notifier.go +++ b/src/notification/http_notifier.go @@ -36,7 +36,7 @@ func NewHttpNotifier() *HttpNotifier { } func (n HttpNotifier) Notify(subject string, text string, attachfile string) { - log.Println("HTTP notification: ", subject) + log.Println("Sending notification '" + subject + "' to " + n.URL + " ...") data := make(url.Values) data.Set("from", n.From) data.Set("to", n.To) @@ -88,7 +88,7 @@ func (n HttpNotifier) Notify(subject string, text string, attachfile string) { log.Println("bad status: %s", res.Status) return } - log.Println("HTTP notification done with attachment: ", attachfile) + log.Println("HTTP notification sended with attachment: ", attachfile) } else { resp, err := http.PostForm(n.URL, data) if err != nil { diff --git a/src/webhookd.go b/src/webhookd.go index 87741b3..a43331f 100644 --- a/src/webhookd.go +++ b/src/webhookd.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "flag" "fmt" "github.com/gorilla/mux" @@ -105,8 +104,7 @@ func Handler(w http.ResponseWriter, r *http.Request) { log.Println("Using hook: ", context.Hook) - decoder := json.NewDecoder(r.Body) - err = decoder.Decode(&record) + err = record.Decode(r) if err != nil { log.Println(err.Error()) http.Error(w, err.Error(), http.StatusBadRequest)