Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.5k
Add WebhookTypes and EventForType methods#2865
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
575929f
Add WebhookTypes and EventForType methods
evankanderson 98b730e
Switch to a map of types and reflection for names, reduce future code…
evankanderson 2b545ee
Fix Unmarshal(nil) error and add tests for ParsePayload on a zero-val…
evankanderson 76a0c03
Merge remote-tracking branch 'upstream/master'
evankanderson 759f9be
Address gmlewis feedback
evankanderson 2d83b50
Merge #2868 and address gmlewis feedback
evankanderson ed58239
Use :=, since go figures out the type correctly
evankanderson 96eee83
Merge remote-tracking branch 'upstream/master'
evankanderson 848850a
Update github/messages.go
gmlewis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -22,6 +22,8 @@ import ( | ||
| "mime" | ||
| "net/http" | ||
| "net/url" | ||
| "reflect" | ||
| "sort" | ||
| "strings" | ||
| ) | ||
| @@ -43,74 +45,86 @@ const ( | ||
| var ( | ||
| // eventTypeMapping maps webhooks types to their corresponding go-github struct types. | ||
| eventTypeMapping = map[string]string{ | ||
| "branch_protection_rule": "BranchProtectionRuleEvent", | ||
| "check_run": "CheckRunEvent", | ||
| "check_suite": "CheckSuiteEvent", | ||
| "code_scanning_alert": "CodeScanningAlertEvent", | ||
| "commit_comment": "CommitCommentEvent", | ||
| "content_reference": "ContentReferenceEvent", | ||
| "create": "CreateEvent", | ||
| "delete": "DeleteEvent", | ||
| "deploy_key": "DeployKeyEvent", | ||
| "deployment": "DeploymentEvent", | ||
| "deployment_status": "DeploymentStatusEvent", | ||
| "deployment_protection_rule": "DeploymentProtectionRuleEvent", | ||
| "discussion": "DiscussionEvent", | ||
| "discussion_comment": "DiscussionCommentEvent", | ||
| "fork": "ForkEvent", | ||
| "github_app_authorization": "GitHubAppAuthorizationEvent", | ||
| "gollum": "GollumEvent", | ||
| "installation": "InstallationEvent", | ||
| "installation_repositories": "InstallationRepositoriesEvent", | ||
| "installation_target": "InstallationTargetEvent", | ||
| "issue_comment": "IssueCommentEvent", | ||
| "issues": "IssuesEvent", | ||
| "label": "LabelEvent", | ||
| "marketplace_purchase": "MarketplacePurchaseEvent", | ||
| "member": "MemberEvent", | ||
| "membership": "MembershipEvent", | ||
| "merge_group": "MergeGroupEvent", | ||
| "meta": "MetaEvent", | ||
| "milestone": "MilestoneEvent", | ||
| "organization": "OrganizationEvent", | ||
| "org_block": "OrgBlockEvent", | ||
| "package": "PackageEvent", | ||
| "page_build": "PageBuildEvent", | ||
| "personal_access_token_request": "PersonalAccessTokenRequestEvent", | ||
| "ping": "PingEvent", | ||
| "project": "ProjectEvent", | ||
| "project_card": "ProjectCardEvent", | ||
| "project_column": "ProjectColumnEvent", | ||
| "projects_v2": "ProjectV2Event", | ||
| "projects_v2_item": "ProjectV2ItemEvent", | ||
| "public": "PublicEvent", | ||
| "pull_request": "PullRequestEvent", | ||
| "pull_request_review": "PullRequestReviewEvent", | ||
| "pull_request_review_comment": "PullRequestReviewCommentEvent", | ||
| "pull_request_review_thread": "PullRequestReviewThreadEvent", | ||
| "pull_request_target": "PullRequestTargetEvent", | ||
| "push": "PushEvent", | ||
| "repository": "RepositoryEvent", | ||
| "repository_dispatch": "RepositoryDispatchEvent", | ||
| "repository_import": "RepositoryImportEvent", | ||
| "repository_vulnerability_alert": "RepositoryVulnerabilityAlertEvent", | ||
| "release": "ReleaseEvent", | ||
| "secret_scanning_alert": "SecretScanningAlertEvent", | ||
| "security_advisory": "SecurityAdvisoryEvent", | ||
| "security_and_analysis": "SecurityAndAnalysisEvent", | ||
| "star": "StarEvent", | ||
| "status": "StatusEvent", | ||
| "team": "TeamEvent", | ||
| "team_add": "TeamAddEvent", | ||
| "user": "UserEvent", | ||
| "watch": "WatchEvent", | ||
| "workflow_dispatch": "WorkflowDispatchEvent", | ||
| "workflow_job": "WorkflowJobEvent", | ||
| "workflow_run": "WorkflowRunEvent", | ||
| eventTypeMapping = map[string]interface{}{ | ||
| "branch_protection_rule": &BranchProtectionRuleEvent{}, | ||
gmlewis marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| "check_run": &CheckRunEvent{}, | ||
| "check_suite": &CheckSuiteEvent{}, | ||
| "code_scanning_alert": &CodeScanningAlertEvent{}, | ||
| "commit_comment": &CommitCommentEvent{}, | ||
| "content_reference": &ContentReferenceEvent{}, | ||
| "create": &CreateEvent{}, | ||
| "delete": &DeleteEvent{}, | ||
| "deploy_key": &DeployKeyEvent{}, | ||
| "deployment": &DeploymentEvent{}, | ||
| "deployment_status": &DeploymentStatusEvent{}, | ||
| "deployment_protection_rule": &DeploymentProtectionRuleEvent{}, | ||
| "discussion": &DiscussionEvent{}, | ||
| "discussion_comment": &DiscussionCommentEvent{}, | ||
| "fork": &ForkEvent{}, | ||
| "github_app_authorization": &GitHubAppAuthorizationEvent{}, | ||
| "gollum": &GollumEvent{}, | ||
| "installation": &InstallationEvent{}, | ||
| "installation_repositories": &InstallationRepositoriesEvent{}, | ||
| "installation_target": &InstallationTargetEvent{}, | ||
| "issue_comment": &IssueCommentEvent{}, | ||
| "issues": &IssuesEvent{}, | ||
| "label": &LabelEvent{}, | ||
| "marketplace_purchase": &MarketplacePurchaseEvent{}, | ||
| "member": &MemberEvent{}, | ||
| "membership": &MembershipEvent{}, | ||
| "merge_group": &MergeGroupEvent{}, | ||
| "meta": &MetaEvent{}, | ||
| "milestone": &MilestoneEvent{}, | ||
| "organization": &OrganizationEvent{}, | ||
| "org_block": &OrgBlockEvent{}, | ||
| "package": &PackageEvent{}, | ||
| "page_build": &PageBuildEvent{}, | ||
| "personal_access_token_request": &PersonalAccessTokenRequestEvent{}, | ||
| "ping": &PingEvent{}, | ||
| "project": &ProjectEvent{}, | ||
| "project_card": &ProjectCardEvent{}, | ||
| "project_column": &ProjectColumnEvent{}, | ||
| "projects_v2": &ProjectV2Event{}, | ||
| "projects_v2_item": &ProjectV2ItemEvent{}, | ||
| "public": &PublicEvent{}, | ||
| "pull_request": &PullRequestEvent{}, | ||
| "pull_request_review": &PullRequestReviewEvent{}, | ||
| "pull_request_review_comment": &PullRequestReviewCommentEvent{}, | ||
| "pull_request_review_thread": &PullRequestReviewThreadEvent{}, | ||
| "pull_request_target": &PullRequestTargetEvent{}, | ||
| "push": &PushEvent{}, | ||
| "repository": &RepositoryEvent{}, | ||
| "repository_dispatch": &RepositoryDispatchEvent{}, | ||
| "repository_import": &RepositoryImportEvent{}, | ||
| "repository_vulnerability_alert": &RepositoryVulnerabilityAlertEvent{}, | ||
| "release": &ReleaseEvent{}, | ||
| "secret_scanning_alert": &SecretScanningAlertEvent{}, | ||
| "security_advisory": &SecurityAdvisoryEvent{}, | ||
| "security_and_analysis": &SecurityAndAnalysisEvent{}, | ||
| "star": &StarEvent{}, | ||
| "status": &StatusEvent{}, | ||
| "team": &TeamEvent{}, | ||
| "team_add": &TeamAddEvent{}, | ||
| "user": &UserEvent{}, | ||
| "watch": &WatchEvent{}, | ||
| "workflow_dispatch": &WorkflowDispatchEvent{}, | ||
| "workflow_job": &WorkflowJobEvent{}, | ||
| "workflow_run": &WorkflowRunEvent{}, | ||
| } | ||
| // forward mapping of event types to the string names of the structs | ||
| messageToTypeName = make(map[string]string, len(eventTypeMapping)) | ||
| // Inverse map of the above | ||
| typeToMessageMapping = make(map[string]string, len(eventTypeMapping)) | ||
| ) | ||
| func init() { | ||
| for k, v := range eventTypeMapping { | ||
| typename := reflect.TypeOf(v).Elem().Name() | ||
| messageToTypeName[k] = typename | ||
| typeToMessageMapping[typename] = k | ||
| } | ||
| } | ||
| // genMAC generates the HMAC signature for a message provided the secret key | ||
| // and hashFunc. | ||
| func genMAC(message, key []byte, hashFunc func() hash.Hash) []byte { | ||
| @@ -299,7 +313,7 @@ func DeliveryID(r *http.Request) string { | ||
| // } | ||
| // } | ||
| func ParseWebHook(messageType string, payload []byte) (interface{}, error) { | ||
| eventType, ok := eventTypeMapping[messageType] | ||
| eventType, ok := messageToTypeName[messageType] | ||
| if !ok { | ||
| return nil, fmt.Errorf("unknown X-Github-Event in message: %v", messageType) | ||
| } | ||
| @@ -310,3 +324,28 @@ func ParseWebHook(messageType string, payload []byte) (interface{}, error) { | ||
| } | ||
| return event.ParsePayload() | ||
| } | ||
| // WebhookTypes returns a sorted list of all the known GitHub event type strings | ||
| // supported by go-github. | ||
| func MessageTypes() []string { | ||
| types := make([]string, 0, len(eventTypeMapping)) | ||
| for t := range eventTypeMapping { | ||
| types = append(types, t) | ||
| } | ||
| sort.Strings(types) | ||
| return types | ||
evankanderson marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| // EventForType returns an empty struct matching the specified GitHub event type. | ||
| // If messageType does not match any known event types, it returns nil. | ||
| func EventForType(messageType string) interface{} { | ||
| prototype := eventTypeMapping[messageType] | ||
| if prototype == nil { | ||
| return nil | ||
| } | ||
| // return a _copy_ of the pointed-to-object. Unfortunately, for this we | ||
| // need to use reflection. If we store the actual objects in the map, | ||
| // we still need to use reflection to convert from `any` to the actual | ||
| // type, so this was deemed the lesser of two evils. (#2865) | ||
| return reflect.New(reflect.TypeOf(prototype).Elem()).Interface() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.