Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Accept suggestions from gmlewis with thanks :)
  • Loading branch information
mumoshu committed Jul 9, 2021
commit 1381c0f3a94a92fb26d5cc36e0c5b818245a2739
108 changes: 8 additions & 100 deletions github/repos_hooks_deliveries.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,106 +103,14 @@ func (s *RepositoriesService) GetHookDelivery(ctx context.Context, owner, repo s
return h, resp, nil
}

// ParseRequestPayload parses the request payload. For recognized event types,
// a value of the corresponding struct type will be returned.
func (d *HookDelivery) ParseRequestPayload() (interface{}, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a utility function to ease parsing event payloads, and it is derived from the equivalent function for Events API

// ParsePayload parses the event payload. For recognized event types,
// a value of the corresponding struct type will be returned.
func (e *Event) ParsePayload() (payload interface{}, err error) {

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@mumoshu mumoshu Jul 6, 2021

Choose a reason for hiding this comment

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

@gmlewis Hey! Interesting idea. How would you like to do that? That snippet in event.go switch on Type whose the value can be e.g. CheckRunEvent. On the other hand, this one switches on event field where the value is e.g. check_run. They're very similar but different enough to worth a dedicated function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also note that this is almost fully covered by TestHookDelivery_ParsePayload, if that helps.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What I'm thinking here is to make a helper function that both ParsePayload and ParseRequestPayload both call.

The helper method could take an anonymous function to perform the required mapping... one of them would use the eventTypeMethod map found in messages.go.

Let me know if you don't think this is possible and I'll download your PR and see if I can get it to work.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Here's my proposed replacement for this method:

// ParseRequestPayload parses the request payload. For recognized event types,
// a value of the corresponding struct type will be returned.
func (d *HookDelivery) ParseRequestPayload() (interface{}, error) {
	eType, ok := eventTypeMapping[*d.Event]
	if !ok {
		return nil, fmt.Errorf("unsupported event type %q", *d.Event)
	}

	e := &Event{Type: &eType, RawPayload: d.Request.RawPayload}
	return e.ParsePayload()
}

var payload interface{}
switch *d.Event {
case "check_run":
payload = &CheckRunEvent{}
case "check_suite":
payload = &CheckSuiteEvent{}
case "commit_comment":
payload = &CommitCommentEvent{}
case "content_reference":
payload = &ContentReferenceEvent{}
case "create":
payload = &CreateEvent{}
case "delete":
payload = &DeleteEvent{}
case "deploy_ket":
payload = &DeployKeyEvent{}
case "deployment":
payload = &DeploymentEvent{}
case "deployment_status":
payload = &DeploymentStatusEvent{}
case "fork":
payload = &ForkEvent{}
case "github_app_authorization":
payload = &GitHubAppAuthorizationEvent{}
case "gollum":
payload = &GollumEvent{}
case "installation":
payload = &InstallationEvent{}
case "installation_repositories":
payload = &InstallationRepositoriesEvent{}
case "issue_comment":
payload = &IssueCommentEvent{}
case "issues":
payload = &IssuesEvent{}
case "label":
payload = &LabelEvent{}
case "marketplace_purchase":
payload = &MarketplacePurchaseEvent{}
case "member_event":
payload = &MemberEvent{}
case "membership_event":
payload = &MembershipEvent{}
case "meta":
payload = &MetaEvent{}
case "milestone":
payload = &MilestoneEvent{}
case "organization":
payload = &OrganizationEvent{}
case "org_block":
payload = &OrgBlockEvent{}
case "package":
payload = &PackageEvent{}
case "page_build":
payload = &PageBuildEvent{}
case "ping":
payload = &PingEvent{}
case "project":
payload = &ProjectEvent{}
case "project_card":
payload = &ProjectCardEvent{}
case "project_column":
payload = &ProjectColumnEvent{}
case "public":
payload = &PublicEvent{}
case "pull_request":
payload = &PullRequestEvent{}
case "pull_request_review":
payload = &PullRequestReviewEvent{}
case "pull_request_review_comment":
payload = &PullRequestReviewCommentEvent{}
case "pull_request_target":
payload = &PullRequestTargetEvent{}
case "push":
payload = &PushEvent{}
case "release":
payload = &ReleaseEvent{}
case "repository":
payload = &RepositoryEvent{}
case "repository_dispatch":
payload = &RepositoryDispatchEvent{}
case "repository_vulnerability_alert":
payload = &RepositoryVulnerabilityAlertEvent{}
case "star":
payload = &StarEvent{}
case "status":
payload = &StatusEvent{}
case "team":
payload = &TeamEvent{}
case "team_add":
payload = &TeamAddEvent{}
case "user":
payload = &UserEvent{}
case "watch":
payload = &WatchEvent{}
case "workflow_dispatch":
payload = &WorkflowDispatchEvent{}
case "workflow_run":
payload = &WorkflowRunEvent{}
eType, ok := eventTypeMapping[*d.Event]
if !ok {
return nil, fmt.Errorf("unsupported event type %q", *d.Event)
}
err := json.Unmarshal(*d.Request.RawPayload, &payload)
return payload, err

e := &Event{Type: &eType, RawPayload: d.Request.RawPayload}
return e.ParsePayload()
}
8 changes: 4 additions & 4 deletions github/repos_hooks_deliveries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ var hookDeliveryPayloadTypeToStruct = map[string]interface{}{
"content_reference": &ContentReferenceEvent{},
"create": &CreateEvent{},
"delete": &DeleteEvent{},
"deploy_ket": &DeployKeyEvent{},
"deploy_key": &DeployKeyEvent{},
"deployment": &DeploymentEvent{},
"deployment_status": &DeploymentStatusEvent{},
"fork": &ForkEvent{},
Expand All @@ -126,8 +126,8 @@ var hookDeliveryPayloadTypeToStruct = map[string]interface{}{
"issues": &IssuesEvent{},
"label": &LabelEvent{},
"marketplace_purchase": &MarketplacePurchaseEvent{},
"member_event": &MemberEvent{},
"membership_event": &MembershipEvent{},
"member": &MemberEvent{},
"membership": &MembershipEvent{},
"meta": &MetaEvent{},
"milestone": &MilestoneEvent{},
"organization": &OrganizationEvent{},
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestHookDelivery_ParsePayload_invalidEvent(t *testing.T) {
}

_, err := d.ParseRequestPayload()
if err == nil || err.Error() != "unexpected end of JSON input" {
if err == nil || err.Error() != `unsupported event type "some_invalid_event"` {
t.Errorf("unexpected error: %v", err)
}
}
Expand Down