Skip to content

Commit f9794a8

Browse files
gmlewisMatt Gaunt
authored and
Matt Gaunt
committed
Move event-related code to event.go (google#1057)
1 parent 35781f7 commit f9794a8

File tree

2 files changed

+124
-115
lines changed

2 files changed

+124
-115
lines changed

github/activity_events.go

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -7,124 +7,9 @@ package github
77

88
import (
99
"context"
10-
"encoding/json"
1110
"fmt"
12-
"time"
1311
)
1412

15-
// Event represents a GitHub event.
16-
type Event struct {
17-
Type *string `json:"type,omitempty"`
18-
Public *bool `json:"public,omitempty"`
19-
RawPayload *json.RawMessage `json:"payload,omitempty"`
20-
Repo *Repository `json:"repo,omitempty"`
21-
Actor *User `json:"actor,omitempty"`
22-
Org *Organization `json:"org,omitempty"`
23-
CreatedAt *time.Time `json:"created_at,omitempty"`
24-
ID *string `json:"id,omitempty"`
25-
}
26-
27-
func (e Event) String() string {
28-
return Stringify(e)
29-
}
30-
31-
// ParsePayload parses the event payload. For recognized event types,
32-
// a value of the corresponding struct type will be returned.
33-
func (e *Event) ParsePayload() (payload interface{}, err error) {
34-
switch *e.Type {
35-
case "CheckRunEvent":
36-
payload = &CheckRunEvent{}
37-
case "CheckSuiteEvent":
38-
payload = &CheckSuiteEvent{}
39-
case "CommitCommentEvent":
40-
payload = &CommitCommentEvent{}
41-
case "CreateEvent":
42-
payload = &CreateEvent{}
43-
case "DeleteEvent":
44-
payload = &DeleteEvent{}
45-
case "DeploymentEvent":
46-
payload = &DeploymentEvent{}
47-
case "DeploymentStatusEvent":
48-
payload = &DeploymentStatusEvent{}
49-
case "ForkEvent":
50-
payload = &ForkEvent{}
51-
case "GollumEvent":
52-
payload = &GollumEvent{}
53-
case "InstallationEvent":
54-
payload = &InstallationEvent{}
55-
case "InstallationRepositoriesEvent":
56-
payload = &InstallationRepositoriesEvent{}
57-
case "IssueCommentEvent":
58-
payload = &IssueCommentEvent{}
59-
case "IssuesEvent":
60-
payload = &IssuesEvent{}
61-
case "LabelEvent":
62-
payload = &LabelEvent{}
63-
case "MarketplacePurchaseEvent":
64-
payload = &MarketplacePurchaseEvent{}
65-
case "MemberEvent":
66-
payload = &MemberEvent{}
67-
case "MembershipEvent":
68-
payload = &MembershipEvent{}
69-
case "MilestoneEvent":
70-
payload = &MilestoneEvent{}
71-
case "OrganizationEvent":
72-
payload = &OrganizationEvent{}
73-
case "OrgBlockEvent":
74-
payload = &OrgBlockEvent{}
75-
case "PageBuildEvent":
76-
payload = &PageBuildEvent{}
77-
case "PingEvent":
78-
payload = &PingEvent{}
79-
case "ProjectEvent":
80-
payload = &ProjectEvent{}
81-
case "ProjectCardEvent":
82-
payload = &ProjectCardEvent{}
83-
case "ProjectColumnEvent":
84-
payload = &ProjectColumnEvent{}
85-
case "PublicEvent":
86-
payload = &PublicEvent{}
87-
case "PullRequestEvent":
88-
payload = &PullRequestEvent{}
89-
case "PullRequestReviewEvent":
90-
payload = &PullRequestReviewEvent{}
91-
case "PullRequestReviewCommentEvent":
92-
payload = &PullRequestReviewCommentEvent{}
93-
case "PushEvent":
94-
payload = &PushEvent{}
95-
case "ReleaseEvent":
96-
payload = &ReleaseEvent{}
97-
case "RepositoryEvent":
98-
payload = &RepositoryEvent{}
99-
case "RepositoryVulnerabilityAlertEvent":
100-
payload = &RepositoryVulnerabilityAlertEvent{}
101-
case "StatusEvent":
102-
payload = &StatusEvent{}
103-
case "TeamEvent":
104-
payload = &TeamEvent{}
105-
case "TeamAddEvent":
106-
payload = &TeamAddEvent{}
107-
case "WatchEvent":
108-
payload = &WatchEvent{}
109-
}
110-
err = json.Unmarshal(*e.RawPayload, &payload)
111-
return payload, err
112-
}
113-
114-
// Payload returns the parsed event payload. For recognized event types,
115-
// a value of the corresponding struct type will be returned.
116-
//
117-
// Deprecated: Use ParsePayload instead, which returns an error
118-
// rather than panics if JSON unmarshaling raw payload fails.
119-
func (e *Event) Payload() (payload interface{}) {
120-
var err error
121-
payload, err = e.ParsePayload()
122-
if err != nil {
123-
panic(err)
124-
}
125-
return payload
126-
}
127-
12813
// ListEvents drinks from the firehose of all public events across GitHub.
12914
//
13015
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-public-events

github/event.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
6+
package github
7+
8+
import (
9+
"encoding/json"
10+
"time"
11+
)
12+
13+
// Event represents a GitHub event.
14+
type Event struct {
15+
Type *string `json:"type,omitempty"`
16+
Public *bool `json:"public,omitempty"`
17+
RawPayload *json.RawMessage `json:"payload,omitempty"`
18+
Repo *Repository `json:"repo,omitempty"`
19+
Actor *User `json:"actor,omitempty"`
20+
Org *Organization `json:"org,omitempty"`
21+
CreatedAt *time.Time `json:"created_at,omitempty"`
22+
ID *string `json:"id,omitempty"`
23+
}
24+
25+
func (e Event) String() string {
26+
return Stringify(e)
27+
}
28+
29+
// ParsePayload parses the event payload. For recognized event types,
30+
// a value of the corresponding struct type will be returned.
31+
func (e *Event) ParsePayload() (payload interface{}, err error) {
32+
switch *e.Type {
33+
case "CheckRunEvent":
34+
payload = &CheckRunEvent{}
35+
case "CheckSuiteEvent":
36+
payload = &CheckSuiteEvent{}
37+
case "CommitCommentEvent":
38+
payload = &CommitCommentEvent{}
39+
case "CreateEvent":
40+
payload = &CreateEvent{}
41+
case "DeleteEvent":
42+
payload = &DeleteEvent{}
43+
case "DeploymentEvent":
44+
payload = &DeploymentEvent{}
45+
case "DeploymentStatusEvent":
46+
payload = &DeploymentStatusEvent{}
47+
case "ForkEvent":
48+
payload = &ForkEvent{}
49+
case "GollumEvent":
50+
payload = &GollumEvent{}
51+
case "InstallationEvent":
52+
payload = &InstallationEvent{}
53+
case "InstallationRepositoriesEvent":
54+
payload = &InstallationRepositoriesEvent{}
55+
case "IssueCommentEvent":
56+
payload = &IssueCommentEvent{}
57+
case "IssuesEvent":
58+
payload = &IssuesEvent{}
59+
case "LabelEvent":
60+
payload = &LabelEvent{}
61+
case "MarketplacePurchaseEvent":
62+
payload = &MarketplacePurchaseEvent{}
63+
case "MemberEvent":
64+
payload = &MemberEvent{}
65+
case "MembershipEvent":
66+
payload = &MembershipEvent{}
67+
case "MilestoneEvent":
68+
payload = &MilestoneEvent{}
69+
case "OrganizationEvent":
70+
payload = &OrganizationEvent{}
71+
case "OrgBlockEvent":
72+
payload = &OrgBlockEvent{}
73+
case "PageBuildEvent":
74+
payload = &PageBuildEvent{}
75+
case "PingEvent":
76+
payload = &PingEvent{}
77+
case "ProjectEvent":
78+
payload = &ProjectEvent{}
79+
case "ProjectCardEvent":
80+
payload = &ProjectCardEvent{}
81+
case "ProjectColumnEvent":
82+
payload = &ProjectColumnEvent{}
83+
case "PublicEvent":
84+
payload = &PublicEvent{}
85+
case "PullRequestEvent":
86+
payload = &PullRequestEvent{}
87+
case "PullRequestReviewEvent":
88+
payload = &PullRequestReviewEvent{}
89+
case "PullRequestReviewCommentEvent":
90+
payload = &PullRequestReviewCommentEvent{}
91+
case "PushEvent":
92+
payload = &PushEvent{}
93+
case "ReleaseEvent":
94+
payload = &ReleaseEvent{}
95+
case "RepositoryEvent":
96+
payload = &RepositoryEvent{}
97+
case "RepositoryVulnerabilityAlertEvent":
98+
payload = &RepositoryVulnerabilityAlertEvent{}
99+
case "StatusEvent":
100+
payload = &StatusEvent{}
101+
case "TeamEvent":
102+
payload = &TeamEvent{}
103+
case "TeamAddEvent":
104+
payload = &TeamAddEvent{}
105+
case "WatchEvent":
106+
payload = &WatchEvent{}
107+
}
108+
err = json.Unmarshal(*e.RawPayload, &payload)
109+
return payload, err
110+
}
111+
112+
// Payload returns the parsed event payload. For recognized event types,
113+
// a value of the corresponding struct type will be returned.
114+
//
115+
// Deprecated: Use ParsePayload instead, which returns an error
116+
// rather than panics if JSON unmarshaling raw payload fails.
117+
func (e *Event) Payload() (payload interface{}) {
118+
var err error
119+
payload, err = e.ParsePayload()
120+
if err != nil {
121+
panic(err)
122+
}
123+
return payload
124+
}

0 commit comments

Comments
 (0)