From 075a31f8bf7fcdf6218283eb54fec92807e0bfd5 Mon Sep 17 00:00:00 2001 From: Yves Junqueira Date: Wed, 31 Jan 2018 15:44:26 -0800 Subject: [PATCH] Understand the field for Push events --- hookserve/hookserve.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/hookserve/hookserve.go b/hookserve/hookserve.go index 446121c..9ac409c 100644 --- a/hookserve/hookserve.go +++ b/hookserve/hookserve.go @@ -5,11 +5,12 @@ import ( "crypto/sha1" "encoding/hex" "errors" - "github.com/bmatsuo/go-jsontree" "io/ioutil" "net/http" "strconv" "strings" + + jsontree "github.com/bmatsuo/go-jsontree" ) var ErrInvalidEventFormat = errors.New("Unable to parse event string. Invalid Format.") @@ -20,6 +21,7 @@ type Event struct { Branch string // The branch the event took place on Commit string // The head commit hash attached to the event Type string // Can be either "pull_request" or "push" + Before string // For Pushes, contains the SHA of the recent commit on Branch before the push. Action string // For Pull Requests, contains "assigned", "unassigned", "labeled", "unlabeled", "opened", "closed", "reopened", or "synchronize". BaseOwner string // For Pull Requests, contains the base owner BaseRepo string // For Pull Requests, contains the base repo @@ -67,6 +69,9 @@ func NewEvent(e string) (*Event, error) { return nil, ErrInvalidEventFormat } } + if event.Type == "push" && len(parts) == 6 { + event.Before = parts[5][8:] + } return &event, nil } @@ -84,6 +89,9 @@ func (e *Event) String() (output string) { output += "brepo: " + e.BaseRepo + "\n" output += "bbranch:" + e.BaseBranch + "\n" } + if e.Type == "push" { + output += "before: " + e.Before + "\n" + } return } @@ -219,6 +227,12 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + event.Before, err = request.Get("before").String() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } else if eventType == "pull_request" { event.Action, err = request.Get("action").String() if err != nil {