本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.6KB

  1. // Copyright 2018 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "encoding/json"
  8. "time"
  9. )
  10. // Event represents a GitHub event.
  11. type Event struct {
  12. Type *string `json:"type,omitempty"`
  13. Public *bool `json:"public,omitempty"`
  14. RawPayload *json.RawMessage `json:"payload,omitempty"`
  15. Repo *Repository `json:"repo,omitempty"`
  16. Actor *User `json:"actor,omitempty"`
  17. Org *Organization `json:"org,omitempty"`
  18. CreatedAt *time.Time `json:"created_at,omitempty"`
  19. ID *string `json:"id,omitempty"`
  20. }
  21. func (e Event) String() string {
  22. return Stringify(e)
  23. }
  24. // ParsePayload parses the event payload. For recognized event types,
  25. // a value of the corresponding struct type will be returned.
  26. func (e *Event) ParsePayload() (payload interface{}, err error) {
  27. switch *e.Type {
  28. case "CheckRunEvent":
  29. payload = &CheckRunEvent{}
  30. case "CheckSuiteEvent":
  31. payload = &CheckSuiteEvent{}
  32. case "CommitCommentEvent":
  33. payload = &CommitCommentEvent{}
  34. case "CreateEvent":
  35. payload = &CreateEvent{}
  36. case "DeleteEvent":
  37. payload = &DeleteEvent{}
  38. case "DeploymentEvent":
  39. payload = &DeploymentEvent{}
  40. case "DeploymentStatusEvent":
  41. payload = &DeploymentStatusEvent{}
  42. case "ForkEvent":
  43. payload = &ForkEvent{}
  44. case "GitHubAppAuthorizationEvent":
  45. payload = &GitHubAppAuthorizationEvent{}
  46. case "GollumEvent":
  47. payload = &GollumEvent{}
  48. case "InstallationEvent":
  49. payload = &InstallationEvent{}
  50. case "InstallationRepositoriesEvent":
  51. payload = &InstallationRepositoriesEvent{}
  52. case "IssueCommentEvent":
  53. payload = &IssueCommentEvent{}
  54. case "IssuesEvent":
  55. payload = &IssuesEvent{}
  56. case "LabelEvent":
  57. payload = &LabelEvent{}
  58. case "MarketplacePurchaseEvent":
  59. payload = &MarketplacePurchaseEvent{}
  60. case "MemberEvent":
  61. payload = &MemberEvent{}
  62. case "MembershipEvent":
  63. payload = &MembershipEvent{}
  64. case "MilestoneEvent":
  65. payload = &MilestoneEvent{}
  66. case "OrganizationEvent":
  67. payload = &OrganizationEvent{}
  68. case "OrgBlockEvent":
  69. payload = &OrgBlockEvent{}
  70. case "PageBuildEvent":
  71. payload = &PageBuildEvent{}
  72. case "PingEvent":
  73. payload = &PingEvent{}
  74. case "ProjectEvent":
  75. payload = &ProjectEvent{}
  76. case "ProjectCardEvent":
  77. payload = &ProjectCardEvent{}
  78. case "ProjectColumnEvent":
  79. payload = &ProjectColumnEvent{}
  80. case "PublicEvent":
  81. payload = &PublicEvent{}
  82. case "PullRequestEvent":
  83. payload = &PullRequestEvent{}
  84. case "PullRequestReviewEvent":
  85. payload = &PullRequestReviewEvent{}
  86. case "PullRequestReviewCommentEvent":
  87. payload = &PullRequestReviewCommentEvent{}
  88. case "PushEvent":
  89. payload = &PushEvent{}
  90. case "ReleaseEvent":
  91. payload = &ReleaseEvent{}
  92. case "RepositoryEvent":
  93. payload = &RepositoryEvent{}
  94. case "RepositoryVulnerabilityAlertEvent":
  95. payload = &RepositoryVulnerabilityAlertEvent{}
  96. case "StatusEvent":
  97. payload = &StatusEvent{}
  98. case "TeamEvent":
  99. payload = &TeamEvent{}
  100. case "TeamAddEvent":
  101. payload = &TeamAddEvent{}
  102. case "WatchEvent":
  103. payload = &WatchEvent{}
  104. }
  105. err = json.Unmarshal(*e.RawPayload, &payload)
  106. return payload, err
  107. }
  108. // Payload returns the parsed event payload. For recognized event types,
  109. // a value of the corresponding struct type will be returned.
  110. //
  111. // Deprecated: Use ParsePayload instead, which returns an error
  112. // rather than panics if JSON unmarshaling raw payload fails.
  113. func (e *Event) Payload() (payload interface{}) {
  114. var err error
  115. payload, err = e.ParsePayload()
  116. if err != nil {
  117. panic(err)
  118. }
  119. return payload
  120. }
上海开阖软件有限公司 沪ICP备12045867号-1