Skip to content

Commit

Permalink
github-gateway: Add delivery headers (#236)
Browse files Browse the repository at this point in the history
This closes #235
  • Loading branch information
discordianfish authored and VaibhavPage committed Mar 18, 2019
1 parent af1f855 commit d8785c0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
27 changes: 23 additions & 4 deletions gateways/community/github/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package github

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
Expand All @@ -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 (
Expand Down Expand Up @@ -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
Expand All @@ -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"))
Expand Down
26 changes: 26 additions & 0 deletions gateways/community/github/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package github

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"testing"
Expand Down Expand Up @@ -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)
})
})
}

0 comments on commit d8785c0

Please sign in to comment.