diff --git a/gateways/community/github/start.go b/gateways/community/github/start.go index 53f97cfbba..3d6b035ace 100644 --- a/gateways/community/github/start.go +++ b/gateways/community/github/start.go @@ -18,6 +18,7 @@ package github import ( "context" + "encoding/json" "fmt" "net/http" "net/url" @@ -38,9 +39,8 @@ const ( ) const ( - githubSignatureHeader = "x-hub-signature" - githubEventHeader = "x-github-event" - githubDeliveryHeader = "x-github-delivery" + githubEventHeader = "X-GitHub-Event" + githubDeliveryHeader = "X-GitHub-Delivery" ) var ( @@ -176,6 +176,25 @@ func (ese *GithubEventSourceExecutor) StartEventSource(eventSource *gateways.Eve }, helper, eventStream) } +func parseValidateRequest(r *http.Request, secret []byte) ([]byte, error) { + body, err := gh.ValidatePayload(r, secret) + if err != nil { + return nil, err + } + + payload := make(map[string]interface{}) + if err := json.Unmarshal(body, &payload); err != nil { + return nil, err + } + for _, h := range []string{ + githubEventHeader, + githubDeliveryHeader, + } { + payload[h] = r.Header.Get(h) + } + return json.Marshal(payload) +} + // routeActiveHandler handles new route func RouteActiveHandler(writer http.ResponseWriter, request *http.Request, rc *gwcommon.RouteConfig) { var response string @@ -197,7 +216,7 @@ func RouteActiveHandler(writer http.ResponseWriter, request *http.Request, rc *g if s, ok := hook.Config["secret"]; ok { secret = s.(string) } - body, err := gh.ValidatePayload(request, []byte(secret)) + body, err := parseValidateRequest(request, []byte(secret)) if err != nil { logger.Error().Err(err).Msg("request is not valid event notification") common.SendErrorResponse(writer, fmt.Sprintf("invalid event notification")) diff --git a/gateways/community/github/start_test.go b/gateways/community/github/start_test.go index 628de12ca2..04751467c4 100644 --- a/gateways/community/github/start_test.go +++ b/gateways/community/github/start_test.go @@ -18,6 +18,7 @@ package github import ( "bytes" + "encoding/json" "io/ioutil" "net/http" "testing" @@ -103,3 +104,28 @@ func TestRouteActiveHandler(t *testing.T) { }) }) } + +func TestAddEventTypeBody(t *testing.T) { + convey.Convey("Given a request", t, func() { + var ( + buf = bytes.NewBuffer([]byte(`{ "hello": "world" }`)) + eventType = "PushEvent" + deliveryID = "131C7C9B-A571-4F60-9ACA-EA3ADA19FABE" + ) + request, err := http.NewRequest("POST", "http://example.com", buf) + convey.So(err, convey.ShouldBeNil) + request.Header.Set("X-GitHub-Event", eventType) + request.Header.Set("X-GitHub-Delivery", deliveryID) + request.Header.Set("Content-Type", "application/json") + + convey.Convey("Delivery headers should be written to message", func() { + body, err := parseValidateRequest(request, []byte{}) + convey.So(err, convey.ShouldBeNil) + payload := make(map[string]interface{}) + json.Unmarshal(body, &payload) + convey.So(err, convey.ShouldBeNil) + convey.So(payload["X-GitHub-Event"], convey.ShouldEqual, eventType) + convey.So(payload["X-GitHub-Delivery"], convey.ShouldEqual, deliveryID) + }) + }) +}