get githook hook payload from either form or json

This commit is contained in:
Brad Rydzewski 2014-09-06 10:23:36 -07:00
parent 5f3b882c0c
commit 8ad36255de
2 changed files with 18 additions and 6 deletions

View file

@ -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
}

View file

@ -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)
}