-
Notifications
You must be signed in to change notification settings - Fork 407
/
Copy pathevents.py
586 lines (392 loc) · 15.7 KB
/
events.py
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
"""This module contains the classes related to Events."""
import copy
from . import models
class EventUser(models.GitHubCore):
"""The class that represents the user information returned in Events.
.. note::
Refreshing this object will return a :class:`~github3.users.User`.
.. attribute:: avatar_url
The URL of the avatar image this user chose.
.. attribute:: display_login
The login that is displayed as part of the event.
.. attribute:: gravatar_id
The unique ID for the user's gravatar, if they're using gravatar to
host their avatar.
.. attribute:: id
The user's unique ID in GitHub.
.. attribute:: login
The user's login (or handle) on GitHub.
"""
def _update_attributes(self, user):
self.avatar_url = user["avatar_url"]
self.display_login = user.get("display_login")
self.gravatar_id = user["id"]
self.id = user["id"]
self.login = user["login"]
self._api = self.url = user["url"]
def to_user(self, conditional: bool = False) -> models.GitHubCore:
"""Retrieve a full User object for this EventUser.
:returns:
The full information about this user.
:rtype:
:class:`~github3.users.User`
"""
from . import users
url = self._build_url("users", self.login)
json = self._json(self._get(url), 200)
return self._instance_or_null(users.User, json)
refresh = to_user
class EventOrganization(models.GitHubCore):
"""Representation of the organization information returned in Events.
.. note::
Refreshing this object will return a
:class:`~github3.orgs.Organization`.
This object has the following attributes:
.. attribute:: avatar_url
The URL to this organization's avatar.
.. attribute:: gravatar_id
The unique identifier for this organization on Gravatar, if its
avatar is hosted there.
.. attribute:: id
This organization's unique identifier on GitHub.
.. attribute:: login
The unique login for this organization.
"""
def _update_attributes(self, org):
self.avatar_url = org["avatar_url"]
self.gravatar_id = org["id"]
self.id = org["id"]
self.login = org["login"]
self._api = self.url = org["url"]
def to_org(self, conditional: bool = False) -> models.GitHubCore:
"""Retrieve a full Organization object for this EventOrganization.
:returns:
The full information about this organization.
:rtype:
:class:`~github3.orgs.Organization`
"""
from . import orgs
url = self._build_url("orgs", self.login)
json = self._json(self._get(url), 200)
return self._instance_or_null(orgs.Organization, json)
refresh = to_org
class EventPullRequest(models.GitHubCore):
"""Representation of a Pull Request returned in Events.
.. note::
Refreshing this object returns a :class:`~github3.pulls.PullRequest`.
This object has the following attributes:
.. attribute:: id
The unique id of this pull request across all of GitHub.
.. attribute:: number
The number of this pull request on its repository.
.. attribute:: state
The state of this pull request during this event.
.. attribute:: title
The title of this pull request during this event.
.. attribute:: locked
A boolean attribute describing if this pull request was locked.
"""
def _update_attributes(self, pull):
self.id = pull["id"]
self.number = pull["number"]
self.state = pull["state"]
self.title = pull["title"]
self.locked = pull["locked"]
self._api = self.url = pull["url"]
def to_pull(self, conditional: bool = False) -> models.GitHubCore:
"""Retrieve a full PullRequest object for this EventPullRequest.
:returns:
The full information about this pull request.
:rtype:
:class:`~github3.pulls.PullRequest`
"""
from . import pulls
json = self._json(self._get(self.url), 200)
return self._instance_or_null(pulls.PullRequest, json)
refresh = to_pull
class EventReviewComment(models.GitHubCore):
"""Representation of review comments in events.
.. note::
Refreshing this object will return a new
:class`~github3.pulls.ReviewComment`
This object has the following attributes:
.. attribute:: id
The unique id of this comment across all of GitHub.
.. attribute:: author_association
The association the author has with this project.
.. attribute:: body
The markdown body of this review comment.
.. attribute:: commit_id
The identifier of the commit that this comment was left on.
.. attribute:: created_at
A :class:`~datetime.datetime` object representing the date and time
this comment was created.
.. attribute:: diff_hunk
The section (or hunk) of the diff this comment was left on.
.. attribute:: html_url
The URL to view this comment in a browser.
.. attribute:: links
A dictionary of links to various items about this comment.
.. attribute:: original_commit_id
The identifier of original commit this comment was left on.
.. attribute:: original_position
The original position within the diff this comment was left.
.. attribute:: path
The path to the file this comment was left on.
.. attribute:: position
The current position within the diff this comment is placed.
.. attribute:: pull_request_url
The URL to retrieve the pull request informtation from the API.
.. attribute:: updated_at
A :class:`~datetime.datetime` object representing the date and time
this comment was updated.
.. attribute:: user
A :class:`~github3.users.ShortUser` representing the user who authored
this comment.
"""
def _update_attributes(self, comment):
from . import users
self._api = comment["url"]
self.id = comment["id"]
self.author_association = comment["author_association"]
self.body = comment["body"]
self.commit_id = comment["commit_id"]
self.created_at = self._strptime(comment["created_at"])
self.diff_hunk = comment["diff_hunk"]
self.html_url = comment["html_url"]
self.links = comment["_links"]
self.original_commit_id = comment["original_commit_id"]
self.original_position = comment["original_position"]
self.path = comment["path"]
self.position = comment["position"]
self.pull_request_url = comment["pull_request_url"]
self.updated_at = self._strptime(comment["updated_at"])
self.user = users.ShortUser(comment["user"], self)
def to_review_comment(
self, conditional: bool = False
) -> models.GitHubCore:
"""Retrieve a full ReviewComment object for this EventReviewComment.
:returns:
The full information about this review comment
:rtype:
:class:`~github3.pulls.ReviewComment`
"""
from . import pulls
comment = self._json(self._get(self._api), 200)
return pulls.ReviewComment(comment, self.session)
refresh = to_review_comment
class EventIssue(models.GitHubCore):
"""The class that represents the issue information returned in Events."""
def _update_attributes(self, issue):
self.id = issue["id"]
self.number = issue["number"]
self.state = issue["state"]
self.title = issue["title"]
self.locked = issue["locked"]
self._api = self.url = issue["url"]
def to_issue(self, conditional: bool = False) -> models.GitHubCore:
"""Retrieve a full Issue object for this EventIssue."""
from . import issues
json = self._json(self._get(self.url), 200)
return self._instance_or_null(issues.Issue, json)
refresh = to_issue
class EventIssueComment(models.GitHubCore):
"""Representation of a comment left on an issue.
See also: http://developer.github.com/v3/issues/comments/
This object has the following attributes:
.. attribute:: author_association
The association of the author (:attr:`user`) with the repository
this issue belongs to.
.. attribute:: body
The markdown formatted original text written by the author.
.. attribute:: created_at
A :class:`~datetime.datetime` object representing the date and time
when this comment was created.
.. attribute:: html_url
The URL to view this comment in a browser.
.. attribute:: id
The unique identifier for this comment.
.. attribute:: issue_url
The URL of the parent issue in the API.
.. attribute:: updated_at
A :class:`~datetime.datetime` object representing the date and time
when this comment was most recently updated.
.. attribute:: user
A :class:`~github3.users.ShortUser` representing the author of this
comment.
"""
def _update_attributes(self, comment):
from . import users
self._api = comment["url"]
self.author_association = comment["author_association"]
self.body = comment["body"]
self.created_at = self._strptime(comment["created_at"])
self.html_url = comment["html_url"]
self.id = comment["id"]
self.issue_url = comment["issue_url"]
self.updated_at = self._strptime(comment["updated_at"])
self.user = users.ShortUser(comment["user"], self)
def to_issue_comment(
self, conditional: bool = False
) -> models.GitHubCore:
"""Retrieve the full IssueComment object for this comment.
:returns:
All the information about an IssueComment.
:rtype:
:class:`~github3.issues.comment.IssueComment`
"""
from .issues import comment
json = self._json(self._get(self.url), 200)
return self._instance_or_null(comment.IssueComment, json)
refresh = to_issue_comment
class Event(models.GitHubCore):
"""Represents an event as returned by the API.
It structures and handles the data returned by via the
`Events <https://developer.github.com/v3/activity/events>`_ section
of the GitHub API.
Two events can be compared like so::
e1 == e2
e1 != e2
And that is equivalent to::
e1.id == e2.id
e1.id != e2.id
.. attribute:: actor
A :class:`~github3.events.EventUser` that represents the user whose
action generated this event, or `None` if the user no longer exists.
.. attribute:: created_at
A :class:`~datetime.datetime` representing when this event was created.
.. attribute:: id
The unique identifier for this event.
.. attribute:: org
If present, a :class:`~github3.events.EventOrganization` representing
the organization on which this event occurred.
.. attribute:: type
The type of event this is.
.. seealso::
`Event Types Documentation`_
GitHub's documentation of different event types
.. attribute:: payload
The payload of the event which has all of the details relevant to this
event.
.. attribute:: repo
The string representation of the repository this event pertains to.
.. versionchanged:: 1.0.0
This restores the behaviour of the API. To get a tuple,
representation, use ``self.repo.split('/', 1)``
.. attribute:: public
A boolean representing whether the event is publicly viewable or not.
.. _Event Types Documentation:
https://developer.github.com/v3/activity/events/types/
"""
def _update_attributes(self, event):
# If we don't copy this, then we end up altering _json_data which we do
# not want to do:
event = copy.deepcopy(event)
self.actor = event["actor"]
if self.actor:
self.actor = EventUser(self.actor, self)
self.created_at = self._strptime(event["created_at"])
self.id = event["id"]
self.org = event.get("org")
if self.org:
self.org = EventOrganization(event["org"], self)
self.type = event["type"]
handler = _payload_handlers.get(self.type, identity)
self.payload = handler(event["payload"], self)
self.repo = event["repo"]
self.public = event["public"]
def _repr(self):
return f"<Event [{self.type[:-5]}]>"
@staticmethod
def list_types():
"""List available payload types."""
return sorted(_payload_handlers.keys())
def _commitcomment(payload, session):
from .repos.comment import ShortComment
if payload.get("comment"):
payload["comment"] = ShortComment(payload["comment"], session)
return payload
def _follow(payload, session):
if payload.get("target"):
payload["target"] = EventUser(payload["target"], session)
return payload
def _forkev(payload, session):
from .repos import ShortRepository
if payload.get("forkee"):
payload["forkee"] = ShortRepository(payload["forkee"], session)
return payload
def _gist(payload, session):
from .gists import Gist
if payload.get("gist"):
payload["gist"] = Gist(payload["gist"], session)
return payload
def _issuecomm(payload, session):
if payload.get("issue"):
payload["issue"] = EventIssue(payload["issue"], session)
if payload.get("comment"):
payload["comment"] = EventIssueComment(payload["comment"], session)
return payload
def _issueevent(payload, session):
if payload.get("issue"):
payload["issue"] = EventIssue(payload["issue"], session)
return payload
def _member(payload, session):
if payload.get("member"):
payload["member"] = EventUser(payload["member"], session)
return payload
def _pullreqev(payload, session):
if payload.get("pull_request"):
payload["pull_request"] = EventPullRequest(
payload["pull_request"], session
)
return payload
def _pullreqcomm(payload, session):
# Transform the Pull Request attribute
pull = payload.get("pull_request")
if pull:
payload["pull_request"] = EventPullRequest(pull, session)
# Transform the Comment attribute
comment = payload.get("comment")
if comment:
payload["comment"] = EventReviewComment(comment, session)
return payload
def _release(payload, session):
from .repos.release import Release
release = payload.get("release")
if release:
payload["release"] = Release(release, session)
return payload
def _team(payload, session):
from .orgs import ShortTeam
from .repos import ShortRepository
if payload.get("team"):
payload["team"] = ShortTeam(payload["team"], session)
if payload.get("repo"):
payload["repo"] = ShortRepository(payload["repo"], session)
if payload.get("sender"):
payload["sender"] = EventUser(payload["sender"], session)
return payload
def identity(x, session):
"""Return the value."""
return x
_payload_handlers = {
"CommitCommentEvent": _commitcomment,
"CreateEvent": identity,
"DeleteEvent": identity,
"FollowEvent": _follow,
"ForkEvent": _forkev,
"ForkApplyEvent": identity,
"GistEvent": _gist,
"GollumEvent": identity,
"IssueCommentEvent": _issuecomm,
"IssuesEvent": _issueevent,
"MemberEvent": _member,
"PublicEvent": identity,
"PullRequestEvent": _pullreqev,
"PullRequestReviewCommentEvent": _pullreqcomm,
"PushEvent": identity,
"ReleaseEvent": _release,
"StatusEvent": identity,
"TeamAddEvent": _team,
"WatchEvent": identity,
}