-
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
Conversation
|
Not sure this works, but would it make sense? |
|
Actually it works @willnorris is that something that could be merged? |
github/messages.go
Outdated
| // // Process event ... | ||
| // } | ||
| // | ||
| func ParseWebHook(r *http.Request, secretKey []byte) (interface{}, error) { |
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 returns an interface{}. Can you please help me understand what one would do with the return value of type interface{} from ParseWebHook?
The example just says "// Process event ...", but it's unclear to me how to.
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.
If I look at the implementation of ParseWebHook, then look at what getWebhookEvent does, I'll see that it returns Event.Payload. Then if I refer to documentation for Event.Payload, it begins to make sense:
Payload returns the parsed event payload. For recognized event types, a value of the corresponding struct type will be returned.
The docs of ParseWebHook just say "returns an Event or a WebHookPayload" which isn't as clear IMO.
9528f84 to
01a4527
Compare
|
Updated, does it make more sense @shurcooL ? |
|
@gmlewis could you take a look and compare with our internal webhook processing code. I do like how this reuses the existing logic in |
|
@willnorris - It is similar to what we use internally, although the main difference is that we map the eventType to a specific handler, and then pass the |
github/messages.go
Outdated
| // getWebhookEvent parses the payload and request, and build the Event. | ||
| func getWebhookEvent(r *http.Request, payload []byte) (interface{}, error) { | ||
| eventType := r.Header.Get(eventTypeHeader) | ||
| if eventType == "push" { |
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.
Why does the push eventType need to be handled specially?
f5c3676 to
3cb4211
Compare
|
@gmlewis Updated |
| for _, test := range tests { | ||
| p, err := json.Marshal(test.payload) | ||
| if err != nil { | ||
| t.Fatalf("Marshal(%#v): %v", test.payload, err) |
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.
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.
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.
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.
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.
@apelisse, did you see this?
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.
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
|
A few minor nits - but I like how this turned out. |
github/messages.go
Outdated
| } | ||
|
|
||
| // getWebhookEvent parses the payload and request, and build the Event. | ||
| func getWebhookEvent(messageType string, payload []byte) (interface{}, error) { |
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.
Rename to parseWebhookEvent, that seems like a much more fitting and consistent prefix than "get".
Also, should it be "Webhook" or "WebHook"? I see both used in this PR. But the rest of the codebase is also inconsistent. The exported code seems to use "WebHook", so we should probably go with that (since it's harder to change compared to documentation).
|
Awaiting LGTM from @shurcooL |
|
Yes, please wait, I'm about to push a new version :-) |
3cb4211 to
653e0a0
Compare
|
@shurcooL updated A couple of points:
Hope you like it! |
|
Thanks, I'll be able to review this tonight.
I see |
|
OK Good catch, let me change that now so you can review it tonight. Thanks |
653e0a0 to
a577815
Compare
|
Updated |
dmitshur
left a comment
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.
There are many comments, test names, error messages that are out of date, please fix those. I've left comments inline.
Many of your sentences are missing periods. It's better to include them to be consistent with the rest of the code and the Go style.
Finally, I've made a suggestion to rename ParseWebHookType func.
I don't see any other issues. Thanks!
github/messages_test.go
Outdated
| if err != nil { | ||
| t.Fatalf("getWebHookEvent: %v", err) | ||
| } | ||
| if !reflect.DeepEqual(test.payload, got) { |
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.
The common style is to compare got with want, in that order. Not the other way around.
github $ cat *.go | grep "got, want" | wc -l
152
github $ cat *.go | grep "want, got" | wc -l
0
It might be more readable as:
if want := test.payload; !reflect.DeepEqual(got, want) {
github/messages_test.go
Outdated
| t.Fatalf("getWebHookEvent: %v", err) | ||
| } | ||
| if !reflect.DeepEqual(test.payload, got) { | ||
| t.Errorf("getWebHook(%#v, %#v) = %#v, want %#v", test.messageType, p, got, test.payload) |
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.
getWebHook is out of date, it's called ParseWebHook now.
github/messages_test.go
Outdated
| } | ||
| } | ||
|
|
||
| func TestGetWebHookEvent(t *testing.T) { |
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.
It should be TestParseWebHook now.
github/messages.go
Outdated
| } | ||
|
|
||
| // ParseWebHook parses the request into an interface to an activity | ||
| // event type (as returned by Event.Payload()) |
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 sentence is missing a period at the end, which is inconsistent.
The phrasing could be improved. There's no more request, its parameter is payload []byte. No need to mention interface, that's already in the return signature. What it returns is a struct type corresponding to the event type. Taking from Event.Payload docs, perhaps something like this:
// ParseWebHook parses the event payload. For recognized event types, a value of
// the corresponding struct type will be returned (as returned by Event.Payload()).
github/messages_test.go
Outdated
| } | ||
| got, err := ParseWebHook(test.messageType, p) | ||
| if err != nil { | ||
| t.Fatalf("getWebHookEvent: %v", err) |
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.
Same here. "ParseWebHook".
github/messages.go
Outdated
| } | ||
|
|
||
| // ParseWebHookType returns the type of payload | ||
| func ParseWebHookType(r *http.Request) string { |
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.
In this case, I wouldn't call this func parse.
Parse is a good name for something that can fail, and the signature usually has (Something, error) form.
Here, you're really just getting the type of payload from the request headers.
How about renaming it to just WebHookType(r *http.Request)? It feels like leaving out Get is better (see https://golang.org/doc/effective_go.html#Getters).
// WebHookType returns the event type of webhook request r.
func WebHookType(r *http.Request) string {
If not that, then call it GetWebHookType, but ParseWebHookType is definitely not the best name.
github/messages.go
Outdated
| 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 |
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.
Minor style nitpick. This sentence is missing a period at the end, which is inconsistent with the Go style and this codebase.
github/messages.go
Outdated
| ) | ||
|
|
||
| var ( | ||
| // eventTypeMapping is a conversion map between go-github types and webhooks types |
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.
Also missing a period.
The phrasing is odd. Isn't it a map from webhook types (e.g., "commit_comment") and go-github types (e.g., CommitCommentEvent)? The current phrasing makes it sound like the inverse of that.
github/messages.go
Outdated
| // ParseWebHook parses the request into an interface to an activity | ||
| // event type (as returned by Event.Payload()) | ||
| // | ||
| // secretKey is the GitHub Webhook secret message. |
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.
There's no secret key anymore.
| // } | ||
| // } | ||
| // | ||
| func ParseWebHook(messageType string, payload []byte) (interface{}, error) { |
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.
a577815 to
7c16ba8
Compare
|
Updated, I think I've addressed each individual comment. There are no sentence not ending with dots. Thanks again for your excellent feedback! |
dmitshur
left a comment
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.
Thanks again for your excellent feedback!
No problem!
Updated, I think I've addressed each individual comment. There are no sentence not ending with dots.
Thanks! But it looks like you missed exactly 2 comments, didn't you? Everything else looks great.
github/messages.go
Outdated
| ) | ||
|
|
||
| var ( | ||
| // eventTypeMapping is a conversion map between go-github types and webhooks types. |
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.
What about improving the phrasing to be more "left-to-right", e.g., it can say:
// eventTypeMapping maps webhooks types to their corresponding go-github struct types.
Or is that not a more accurate statement?
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.
OK Let's use that exact sentence, it's perfect (I think)
7c16ba8 to
3a3d51b
Compare
|
Updated :-) |
|
You've addressed all my feedback. Thanks for your patience working through these! I'll let @gmlewis or @willnorris take it from here. |
|
Integrated in 94a3cd9 |
Closes google#427. Change-Id: I67faac8df63e0d55fcce4ca5f9ab50e26c04d6b3

No description provided.