forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.py
More file actions
270 lines (201 loc) · 7.8 KB
/
events.py
File metadata and controls
270 lines (201 loc) · 7.8 KB
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
# -*- coding: utf-8 -*-
"""
github3.events
==============
This module contains the class(es) related to Events
"""
from __future__ import unicode_literals
import copy
from .models import GitHubCore
class EventUser(GitHubCore):
"""The class that represents the user information returned in Events."""
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):
"""Retrieve a full User object for this EventUser."""
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(GitHubCore):
"""The class that represents the org information returned in Events."""
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):
"""Retrieve a full Organization object for this EventOrganization."""
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(GitHubCore):
"""The class that represents the pr information returned in Events."""
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):
"""Retrieve a full PullRequest object for this EventPullRequest."""
from . import pulls
json = self._json(self._get(self.url), 200)
return self._instance_or_null(pulls.PullRequest, json)
refresh = to_pull
class EventIssue(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):
"""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 Event(GitHubCore):
"""The :class:`Event <Event>` object. 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
"""
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)
#: :class:`User <github3.users.User>` object representing the actor.
self.actor = self._class_attribute(event, 'actor', EventUser)
#: datetime object representing when the event was created.
self.created_at = self._strptime_attribute(event, 'created_at')
#: Unique id of the event
self.id = self._get_attribute(event, 'id')
#: List all possible types of Events
self.org = self._class_attribute(event, 'org', EventOrganization)
#: Event type https://developer.github.com/v3/activity/events/types/
self.type = self._get_attribute(event, 'type')
handler = _payload_handlers.get(self.type, identity)
#: Dictionary with the payload. Payload structure is defined by type_.
# _type: http://developer.github.com/v3/events/types
self.payload = self._class_attribute(event, 'payload', handler, self)
#: Return ``tuple(owner, repository_name)``
self.repo = self._get_attribute(event, 'repo')
if self.repo:
self.repo = tuple(self.repo['name'].split('/'))
#: Indicates whether the Event is public or not.
self.public = self._get_attribute(event, 'public')
def _repr(self):
return '<Event [{0}]>'.format(self.type[:-5])
@staticmethod
def list_types():
"""List available payload types."""
return sorted(_payload_handlers.keys())
def _commitcomment(payload, session):
from .repos.comment import RepoComment
if payload.get('comment'):
payload['comment'] = RepoComment(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):
from .issues.comment import IssueComment
if payload.get('issue'):
payload['issue'] = EventIssue(payload['issue'], session)
if payload.get('comment'):
payload['comment'] = IssueComment(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):
from .pulls import ReviewComment
# 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'] = ReviewComment(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 Team
from .repos import ShortRepository
if payload.get('team'):
payload['team'] = Team(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 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,
}