From 8ad36255de207c2aaeb4faf859d5f32c6efac67e Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sat, 6 Sep 2014 10:23:36 -0700 Subject: [PATCH] get githook hook payload from either form or json --- plugin/remote/github/github.go | 10 ++++------ plugin/remote/github/helper.go | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/plugin/remote/github/github.go b/plugin/remote/github/github.go index 32f2a2a749..d82374297a 100644 --- a/plugin/remote/github/github.go +++ b/plugin/remote/github/github.go @@ -212,11 +212,9 @@ func (r *GitHub) ParseHook(req *http.Request) (*model.Hook, error) { return r.ParsePullRequestHook(req) } - // get the payload of the message - var payload = req.FormValue("payload") - // parse the github Hook payload - var data, err = github.ParseHook([]byte(payload)) + var payload = GetPayload(req) + var data, err = github.ParseHook(payload) if err != nil { return nil, nil } @@ -259,11 +257,11 @@ func (r *GitHub) ParseHook(req *http.Request) (*model.Hook, error) { // ParsePullRequestHook parses the pull request hook from the Request body // and returns the required data in a standard format. func (r *GitHub) ParsePullRequestHook(req *http.Request) (*model.Hook, error) { - var payload = req.FormValue("payload") // parse the payload to retrieve the pull-request // hook meta-data. - var data, err = github.ParsePullRequestHook([]byte(payload)) + var payload = GetPayload(req) + var data, err = github.ParsePullRequestHook(payload) if err != nil { return nil, err } diff --git a/plugin/remote/github/helper.go b/plugin/remote/github/helper.go index 6cd21d8175..6a605776ad 100644 --- a/plugin/remote/github/helper.go +++ b/plugin/remote/github/helper.go @@ -3,6 +3,8 @@ package github import ( "encoding/base32" "fmt" + "io/ioutil" + "net/http" "net/url" "code.google.com/p/goauth2/oauth" @@ -236,3 +238,15 @@ func GetFile(client *github.Client, owner, name, path, ref string) ([]byte, erro func GetRandom() string { return base32.StdEncoding.EncodeToString(securecookie.GenerateRandomKey(32)) } + +// GetPayload is a helper function that will parse the JSON payload. It will +// first check for a `payload` parameter in a POST, but can fallback to a +// raw JSON body as well. +func GetPayload(req *http.Request) []byte { + var payload = req.FormValue("payload") + if len(payload) == 0 { + raw, _ := ioutil.ReadAll(req.Body) + return raw + } + return []byte(payload) +}