-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Parse Webhook payload into actual event #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,9 @@ package github | |
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "net/http" | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
|
|
@@ -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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @apelisse, did you see this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
| 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) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.