Skip to content

Commit

Permalink
Use json decoder instead of json.Unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafe Colton committed Oct 30, 2014
1 parent 5d9e11c commit 6d386c3
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions server/webhook/github.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package webhook

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

Expand Down Expand Up @@ -35,18 +37,21 @@ func Github(w http.ResponseWriter, req *http.Request) (int, string) {
event := req.Header.Get("X-Github-Event")
if !githubSupportedEvents[event] {
logger.Errorf("Github event type %s is not supported.", event)
return 400, "400 bad request"
return http.StatusBadRequest, fmt.Sprintf("%d bad request", http.StatusBadRequest)
}
body, err := ioutil.ReadAll(req.Body)
defer req.Body.Close()

if err != nil {
logger.Error(err)
return 400, "400 bad request"
return http.StatusBadRequest, fmt.Sprintf("%d bad request", http.StatusBadRequest)
}

decoder := json.NewDecoder(bytes.NewReader(body))
var payload = &githubPushPayload{}
if err = json.Unmarshal([]byte(body), payload); err != nil {
if err := decoder.Decode(payload); err != nil {
logger.Error(err)
return 400, "400 bad request"
return http.StatusBadRequest, fmt.Sprintf("%d bad request", http.StatusBadRequest)
}

spec := &job.Spec{
Expand Down

0 comments on commit 6d386c3

Please sign in to comment.