diff --git a/plugin/remote/github/github.go b/plugin/remote/github/github.go index 32f2a2a74..d82374297 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 6cd21d817..6a605776a 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) +}