/
event.go
258 lines (239 loc) · 5.67 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Package event defines event types.
package event
import (
"encoding/json"
"fmt"
"time"
"dmitri.shuralyov.com/state"
"github.com/shurcooL/users"
)
// Event represents an event.
type Event struct {
Time time.Time
Actor users.User // UserSpec and Login fields populated.
// Container is the URL (without schema) of event target.
//
// For event types Issue, Change, IssueComment, ChangeComment, CommitComment,
// and Create with "package" type, it's the import path of the target package.
// E.g., "golang.org/x/image/font/sfnt" or "github.com/user/repo/sub/dir".
//
// For all other event types, it is the module path of the target repository.
// E.g., "golang.org/x/image" or "github.com/user/repo".
Container string
// Payload specifies the event type. It's one of:
// Issue, Change, IssueComment, ChangeComment, CommitComment,
// Push, Star, Create, Fork, Delete, Wiki.
Payload interface{}
}
// MarshalJSON implements the json.Marshaler interface.
func (e Event) MarshalJSON() ([]byte, error) {
v := struct {
Time time.Time
Actor users.User
Container string
Type string
Payload interface{}
}{
Time: e.Time,
Actor: e.Actor,
Container: e.Container,
Payload: e.Payload,
}
switch e.Payload.(type) {
case Issue:
v.Type = "Issue"
case Change:
v.Type = "Change"
case IssueComment:
v.Type = "IssueComment"
case ChangeComment:
v.Type = "ChangeComment"
case CommitComment:
v.Type = "CommitComment"
case Push:
v.Type = "Push"
case Star:
v.Type = "Star"
case Create:
v.Type = "Create"
case Fork:
v.Type = "Fork"
case Delete:
v.Type = "Delete"
case Wiki:
v.Type = "Wiki"
default:
return nil, fmt.Errorf("Event.MarshalJSON: invalid payload type %T; Event was %+v", e.Payload, e)
}
return json.Marshal(v)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (e *Event) UnmarshalJSON(b []byte) error {
// Ignore null, like in the main JSON package.
if string(b) == "null" {
return nil
}
var v struct {
Time time.Time
Actor users.User
Container string
Type string
Payload json.RawMessage
}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
*e = Event{
Time: v.Time,
Actor: v.Actor,
Container: v.Container,
}
switch v.Type {
case "Issue":
var p Issue
err := json.Unmarshal(v.Payload, &p)
if err != nil {
return err
}
e.Payload = p
case "Change":
var p Change
err := json.Unmarshal(v.Payload, &p)
if err != nil {
return err
}
e.Payload = p
case "IssueComment":
var p IssueComment
err := json.Unmarshal(v.Payload, &p)
if err != nil {
return err
}
e.Payload = p
case "ChangeComment":
var p ChangeComment
err := json.Unmarshal(v.Payload, &p)
if err != nil {
return err
}
e.Payload = p
case "CommitComment":
var p CommitComment
err := json.Unmarshal(v.Payload, &p)
if err != nil {
return err
}
e.Payload = p
case "Push":
var p Push
err := json.Unmarshal(v.Payload, &p)
if err != nil {
return err
}
e.Payload = p
case "Star":
var p Star
err := json.Unmarshal(v.Payload, &p)
if err != nil {
return err
}
e.Payload = p
case "Create":
var p Create
err := json.Unmarshal(v.Payload, &p)
if err != nil {
return err
}
e.Payload = p
case "Fork":
var p Fork
err := json.Unmarshal(v.Payload, &p)
if err != nil {
return err
}
e.Payload = p
case "Delete":
var p Delete
err := json.Unmarshal(v.Payload, &p)
if err != nil {
return err
}
e.Payload = p
case "Wiki":
var p Wiki
err := json.Unmarshal(v.Payload, &p)
if err != nil {
return err
}
e.Payload = p
default:
return fmt.Errorf("Event.UnmarshalJSON: invalid payload type %q", v.Type)
}
return nil
}
// Issue is an issue event.
type Issue struct {
Action string // "opened", "closed", "reopened".
IssueTitle string
IssueBody string // Only set when action is "opened".
IssueHTMLURL string
}
// Change is a change event.
type Change struct {
Action string // "opened", "closed", "merged", "reopened".
ChangeTitle string
ChangeBody string // Only set when action is "opened".
ChangeHTMLURL string
}
// IssueComment is an issue comment event.
type IssueComment struct {
IssueTitle string
IssueState state.Issue
CommentBody string
CommentHTMLURL string
}
// ChangeComment is a change comment event.
// A change comment is a review iff CommentReview is non-zero.
type ChangeComment struct {
ChangeTitle string
ChangeState state.Change
CommentBody string
CommentReview state.Review
CommentHTMLURL string
}
// CommitComment is a commit comment event.
type CommitComment struct {
Commit Commit
CommentBody string
}
// Push is a push event.
type Push struct {
Branch string // Name of branch pushed to. E.g., "master".
Head string // SHA of the most recent commit after the push.
Before string // SHA of the most recent commit before the push.
Commits []Commit // Ordered from earliest to most recent (head).
HeadHTMLURL string // Optional.
BeforeHTMLURL string // Optional.
}
// Star is a star event.
type Star struct{}
// Create is a create event.
type Create struct {
Type string // "repository", "package", "branch", "tag".
Name string // Only for "branch", "tag" types.
Description string // Only for "repository", "package" types. Optional.
}
// Fork is a fork event.
type Fork struct {
Container string // URL (without schema) of the created repository. E.g., "github.com/anotheruser/repo".
}
// Delete is a delete event.
type Delete struct {
Type string // "branch", "tag".
Name string
}
// Wiki is a wiki event. It happens when an actor updates a wiki.
type Wiki struct {
Pages []Page // Wiki pages that are affected.
}