Skip to content

Conversation

@apelisse
Copy link
Contributor

@apelisse apelisse commented Sep 7, 2016

No description provided.

@apelisse
Copy link
Contributor Author

apelisse commented Sep 7, 2016

Not sure this works, but would it make sense?

@apelisse
Copy link
Contributor Author

apelisse commented Sep 9, 2016

Actually it works

@willnorris is that something that could be merged?

// // Process event ...
// }
//
func ParseWebHook(r *http.Request, secretKey []byte) (interface{}, error) {
Copy link
Member

@dmitshur dmitshur Sep 9, 2016

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.

Copy link
Member

@dmitshur dmitshur Sep 9, 2016

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.

@apelisse
Copy link
Contributor Author

apelisse commented Sep 9, 2016

Updated, does it make more sense @shurcooL ?

@willnorris
Copy link
Collaborator

@gmlewis could you take a look and compare with our internal webhook processing code. I do like how this reuses the existing logic in Event.Payload.

@gmlewis gmlewis assigned gmlewis and unassigned gmlewis Sep 12, 2016
@gmlewis
Copy link
Collaborator

gmlewis commented Sep 12, 2016

@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 payload []byte to the handler, which can then choose to use Event.Payload or not.

// 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" {
Copy link
Collaborator

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?

@apelisse apelisse force-pushed the add-parse-event branch 2 times, most recently from f5c3676 to 3cb4211 Compare September 13, 2016 23:59
@apelisse
Copy link
Contributor Author

@gmlewis Updated

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

@gmlewis
Copy link
Collaborator

gmlewis commented Sep 14, 2016

A few minor nits - but I like how this turned out.
LGTM after the minor tweaks.

@gmlewis gmlewis added the lgtm label Sep 14, 2016
}

// getWebhookEvent parses the payload and request, and build the Event.
func getWebhookEvent(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.

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).

@gmlewis gmlewis removed the lgtm label Sep 14, 2016
@gmlewis
Copy link
Collaborator

gmlewis commented Sep 14, 2016

Awaiting LGTM from @shurcooL

@apelisse
Copy link
Contributor Author

Yes, please wait, I'm about to push a new version :-)

@apelisse
Copy link
Contributor Author

apelisse commented Sep 14, 2016

@shurcooL updated

A couple of points:

  • Not sure where you saw WebHook being used rather than Webhook. The existing documentation reads Webhook
  • I added a ParseWebhookType function. I don't want ParseWebhook() to depend on the request

Hope you like it!

@dmitshur
Copy link
Member

Thanks, I'll be able to review this tonight.

Not sure where you saw WebHook being used rather than Webhook. The existing documentation reads Webhook

I see WebHook used in all the exported types.

See https://godoc.org/github.com/google/go-github/github.

image

@apelisse
Copy link
Contributor Author

OK Good catch, let me change that now so you can review it tonight.

Thanks

@apelisse
Copy link
Contributor Author

Updated

Copy link
Member

@dmitshur dmitshur left a 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!

if err != nil {
t.Fatalf("getWebHookEvent: %v", err)
}
if !reflect.DeepEqual(test.payload, got) {
Copy link
Member

@dmitshur dmitshur Sep 15, 2016

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) {

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)
Copy link
Member

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.

}
}

func TestGetWebHookEvent(t *testing.T) {
Copy link
Member

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.

}

// ParseWebHook parses the request into an interface to an activity
// event type (as returned by Event.Payload())
Copy link
Member

@dmitshur dmitshur Sep 15, 2016

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()).

}
got, err := ParseWebHook(test.messageType, p)
if err != nil {
t.Fatalf("getWebHookEvent: %v", err)
Copy link
Member

@dmitshur dmitshur Sep 15, 2016

Choose a reason for hiding this comment

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

Same here. "ParseWebHook".

}

// ParseWebHookType returns the type of payload
func ParseWebHookType(r *http.Request) string {
Copy link
Member

@dmitshur dmitshur Sep 15, 2016

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.

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
Copy link
Member

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.

)

var (
// eventTypeMapping is a conversion map between go-github types and webhooks types
Copy link
Member

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.

// ParseWebHook parses the request into an interface to an activity
// event type (as returned by Event.Payload())
//
// secretKey is the GitHub Webhook secret message.
Copy link
Member

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) {
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.

@apelisse
Copy link
Contributor Author

Updated, I think I've addressed each individual comment. There are no sentence not ending with dots.

Thanks again for your excellent feedback!

Copy link
Member

@dmitshur dmitshur left a 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.

)

var (
// eventTypeMapping is a conversion map between go-github types and webhooks types.
Copy link
Member

@dmitshur dmitshur Sep 16, 2016

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?

Copy link
Contributor Author

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)

@apelisse
Copy link
Contributor Author

Updated :-)

@dmitshur
Copy link
Member

dmitshur commented Sep 17, 2016

You've addressed all my feedback. Thanks for your patience working through these!

I'll let @gmlewis or @willnorris take it from here.

@gmlewis
Copy link
Collaborator

gmlewis commented Sep 20, 2016

Very nice! Thank you, @apelisse and @shurcooL !
LGTM. Merging.

@gmlewis gmlewis closed this in 94a3cd9 Sep 20, 2016
@gmlewis
Copy link
Collaborator

gmlewis commented Sep 20, 2016

Integrated in 94a3cd9

bubg-dev pushed a commit to bubg-dev/go-github that referenced this pull request Jun 16, 2017
Closes google#427.

Change-Id: I67faac8df63e0d55fcce4ca5f9ab50e26c04d6b3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants