Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions github/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"hash"
Expand All @@ -30,6 +31,35 @@ const (
sha512Prefix = "sha512"
// signatureHeader is the GitHub header key used to pass the HMAC hexdigest.
signatureHeader = "X-Hub-Signature"
// eventTypeHeader is the Github header key used to pass the event type.
eventTypeHeader = "X-Github-Event"
)

var (
// eventTypeMapping maps webhooks types to their corresponding go-github struct types.
eventTypeMapping = map[string]string{
"commit_comment": "CommitCommentEvent",
"create": "CreateEvent",
"delete": "DeleteEvent",
"deployment": "DeploymentEvent",
"deployment_status": "DeploymentStatusEvent",
"fork": "ForkEvent",
"gollum": "GollumEvent",
"issue_comment": "IssueCommentEvent",
"issues": "IssuesEvent",
"member": "MemberEvent",
"membership": "MembershipEvent",
"page_build": "PageBuildEvent",
"public": "PublicEvent",
"pull_request_review_comment": "PullRequestReviewCommentEvent",
"pull_request": "PullRequestEvent",
"push": "PushEvent",
"repository": "RepositoryEvent",
"release": "ReleaseEvent",
"status": "StatusEvent",
"team_add": "TeamAddEvent",
"watch": "WatchEvent",
}
)

// genMAC generates the HMAC signature for a message provided the secret key
Expand Down Expand Up @@ -117,3 +147,42 @@ func validateSignature(signature string, payload, secretKey []byte) error {
}
return nil
}

// WebHookType returns the event type of webhook request r.
func WebHookType(r *http.Request) string {
return r.Header.Get(eventTypeHeader)
}

// ParseWebHook parses the event payload. For recognized event types, a
// value of the corresponding struct type will be returned (as returned
// by Event.Payload()). An error will be returned for unrecognized event
// types.
//
// Example usage:
//
// func (s *GitHubEventMonitor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// payload, err := github.ValidatePayload(r, s.webhookSecretKey)
// if err != nil { ... }
// event, err := github.ParseWebHook(github.WebHookType(r), payload)
// if err != nil { ... }
// switch event := event.(type) {
// case CommitCommentEvent:
// processCommitCommentEvent(event)
// case CreateEvent:
// processCreateEvent(event)
// ...
// }
// }
//
func ParseWebHook(messageType string, payload []byte) (interface{}, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This signature looks as nice as I expect it'll ever look, no further suggestions from me.

eventType, ok := eventTypeMapping[messageType]
if !ok {
return nil, fmt.Errorf("unknown X-Github-Event in message: %v", messageType)
}

event := Event{
Type: &eventType,
RawPayload: (*json.RawMessage)(&payload),
}
return event.Payload(), nil
}
109 changes: 109 additions & 0 deletions github/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package github

import (
"bytes"
"encoding/json"
"net/http"
"reflect"
"testing"
)

Expand Down Expand Up @@ -79,3 +81,110 @@ func TestValidatePayload(t *testing.T) {
}
}
}

func TestParseWebHook(t *testing.T) {
tests := []struct {
payload interface{}
messageType string
}{
{
payload: &CommitCommentEvent{},
messageType: "commit_comment",
},
{
payload: &CreateEvent{},
messageType: "create",
},
{
payload: &DeleteEvent{},
messageType: "delete",
},
{
payload: &DeploymentEvent{},
messageType: "deployment",
},

{
payload: &DeploymentStatusEvent{},
messageType: "deployment_status",
},
{
payload: &ForkEvent{},
messageType: "fork",
},
{
payload: &GollumEvent{},
messageType: "gollum",
},
{
payload: &IssueCommentEvent{},
messageType: "issue_comment",
},
{
payload: &IssuesEvent{},
messageType: "issues",
},
{
payload: &MemberEvent{},
messageType: "member",
},
{
payload: &MembershipEvent{},
messageType: "membership",
},
{
payload: &PageBuildEvent{},
messageType: "page_build",
},
{
payload: &PublicEvent{},
messageType: "public",
},
{
payload: &PullRequestEvent{},
messageType: "pull_request",
},
{
payload: &PullRequestReviewCommentEvent{},
messageType: "pull_request_review_comment",
},
{
payload: &PushEvent{},
messageType: "push",
},
{
payload: &ReleaseEvent{},
messageType: "release",
},
{
payload: &RepositoryEvent{},
messageType: "repository",
},
{
payload: &StatusEvent{},
messageType: "status",
},
{
payload: &TeamAddEvent{},
messageType: "team_add",
},
{
payload: &WatchEvent{},
messageType: "watch",
},
}

for _, test := range tests {
p, err := json.Marshal(test.payload)
if err != nil {
t.Fatalf("Marshal(%#v): %v", test.payload, err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all of these test outputs, you might prefer "%+v" over "%#v" since we are dealing with pointers to structs... but your call... see which you output you think is clearer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you see this comment @apelisse?

It's not mandatory to change this, I just want to find out if you saw it and decided not to change it, or didn't see the comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apelisse, did you see this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I did, I answered (but never published my comment). I like the %#v more because it prints the type, and we care about the type a lot here

}
got, err := ParseWebHook(test.messageType, p)
if err != nil {
t.Fatalf("ParseWebHook: %v", err)
}
if want := test.payload; !reflect.DeepEqual(got, want) {
t.Errorf("ParseWebHook(%#v, %#v) = %#v, want %#v", test.messageType, p, got, want)
}
}
}