forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.py
More file actions
620 lines (451 loc) · 20.5 KB
/
users.py
File metadata and controls
620 lines (451 loc) · 20.5 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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# -*- coding: utf-8 -*-
"""This module contains everything relating to Users."""
from __future__ import unicode_literals
from json import dumps
from github3.auths import Authorization
from uritemplate import URITemplate
from . import models
from .decorators import requires_auth
from .events import Event
class Key(models.GitHubCore):
"""The :class:`Key <Key>` object.
Please see GitHub's `Key Documentation`_ for more information.
.. _Key Documentation:
http://developer.github.com/v3/users/keys/
"""
def _update_attributes(self, key, session=None):
self._api = self._get_attribute(key, 'url')
#: The text of the actual key
self.key = self._get_attribute(key, 'key')
#: The unique id of the key at GitHub
self.id = self._get_attribute(key, 'id')
#: The title the user gave to the key
self.title = self._get_attribute(key, 'title')
def _repr(self):
return '<User Key [{0}]>'.format(self.title)
def __str__(self):
return self.key
@requires_auth
def delete(self):
"""Delete this key."""
return self._boolean(self._delete(self._api), 204, 404)
@requires_auth
def update(self, title, key):
"""Update this key.
.. warning::
As of 20 June 2014, the API considers keys to be immutable.
This will soon begin to return MethodNotAllowed errors.
:param str title: (required), title of the key
:param str key: (required), text of the key file
:returns: bool
"""
json = None
if title and key:
data = {'title': title, 'key': key}
json = self._json(self._patch(self._api, data=dumps(data)), 200)
if json:
self._update_attributes(json)
return True
return False
class Plan(models.GitHubCore):
"""The :class:`Plan <Plan>` object.
Please see GitHub's `Authenticated User`_ documentation for more details.
.. _Authenticated User:
http://developer.github.com/v3/users/#get-the-authenticated-user
"""
def _update_attributes(self, plan):
#: Number of collaborators
self.collaborators = self._get_attribute(plan, 'collaborators')
#: Name of the plan
self.name = self._get_attribute(plan, 'name')
#: Number of private repos
self.private_repos = self._get_attribute(plan, 'private_repos')
#: Space allowed
self.space = self._get_attribute(plan, 'space')
def _repr(self):
return '<Plan [{0}]>'.format(self.name) # (No coverage)
def __str__(self):
return self.name
def is_free(self):
"""Check if this is a free plan.
:returns: bool
"""
return self.name == 'free' # (No coverage)
class Email(models.GitHubCore):
"""The :class:`Email` object.
Please see GitHub's `Emails documentation`_ for more information.
.. _Emails documentation:
https://developer.github.com/v3/users/emails/
"""
def _update_attributes(self, email):
#: Email address
self.email = self._get_attribute(email, 'email')
#: Whether the address has been verified
self.verified = self._get_attribute(email, 'verified')
#: Whether the address is the primary address
self.primary = self._get_attribute(email, 'primary')
def _repr(self):
return '<Email [{0}]>'.format(self.email)
def __str__(self):
return self.email
class _User(models.GitHubCore):
"""The :class:`User <User>` object.
This handles and structures information in the `User section`_.
Two user instances can be checked like so::
u1 == u2
u1 != u2
And is equivalent to::
u1.id == u2.id
u1.id != u2.id
.. _User section:
http://developer.github.com/v3/users/
"""
class_name = '_User'
def _update_attributes(self, user):
#: URL of the avatar at gravatar
self.avatar_url = user['avatar_url']
#: Events URL Template. Expands with ``privacy``
self.events_urlt = URITemplate(user['events_url'])
#: Followers URL (not a template)
self.followers_url = user['followers_url']
#: Following URL Template. Expands with ``other_user``
self.following_urlt = URITemplate(user['following_url'])
#: Gists URL Template. Expands with ``gist_id``
self.gists_urlt = URITemplate(user['gists_url'])
#: ID of the user's image on Gravatar
self.gravatar_id = user['gravatar_id']
# e.g. https://github.com/self._login
#: URL of the user/org's profile
self.html_url = user['html_url']
#: Unique ID of the account
self.id = user['id']
#: User name of the user
self.login = user['login']
#: Organizations URL (not a template)
self.organizations_url = user['organizations_url']
#: Received Events URL (not a template)
self.received_events_url = user['received_events_url']
#: Repostories URL (not a template)
self.repos_url = user['repos_url']
self.site_admin = user.get('site_admin')
#: Starred URL Template. Expands with ``owner`` and ``repo``
self.starred_urlt = URITemplate(user['starred_url'])
#: Subscriptions URL (not a template)
self.subscriptions_url = user['subscriptions_url']
self.type = user['type']
self.url = self._api = user['url']
self._uniq = self.id
def __str__(self):
return self.login
def _repr(self):
return '<{s.class_name} [{s.login}:{s.name}]>'.format(s=self)
def is_assignee_on(self, username, repository):
"""Check if this user can be assigned to issues on username/repository.
:param str username: owner's username of the repository
:param str repository: name of the repository
:returns: True if the use can be assigned, False otherwise
:rtype: :class:`bool`
"""
url = self._build_url('repos', username, repository, 'assignees',
self.login)
return self._boolean(self._get(url), 204, 404)
def is_following(self, username):
"""Check if this user is following ``username``.
:param str username: (required)
:returns: bool
"""
url = self.following_urlt.expand(other_user=username)
return self._boolean(self._get(url), 204, 404)
def events(self, public=False, number=-1, etag=None):
r"""Iterate over events performed by this user.
:param bool public: (optional), only list public events for the
authenticated user
:param int number: (optional), number of events to return. Default: -1
returns all available events.
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`Event <github3.events.Event>`\ s
"""
path = ['events']
if public:
path.append('public')
url = self._build_url(*path, base_url=self._api)
return self._iter(int(number), url, Event, etag=etag)
def followers(self, number=-1, etag=None):
r"""Iterate over the followers of this user.
:param int number: (optional), number of followers to return. Default:
-1 returns all available
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`User <User>`\ s
"""
url = self._build_url('followers', base_url=self._api)
return self._iter(int(number), url, ShortUser, etag=etag)
def following(self, number=-1, etag=None):
r"""Iterate over the users being followed by this user.
:param int number: (optional), number of users to return. Default: -1
returns all available users
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`User <User>`\ s
"""
url = self._build_url('following', base_url=self._api)
return self._iter(int(number), url, ShortUser, etag=etag)
def keys(self, number=-1, etag=None):
r"""Iterate over the public keys of this user.
.. versionadded:: 0.5
:param int number: (optional), number of keys to return. Default: -1
returns all available keys
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`Key <Key>`\ s
"""
url = self._build_url('keys', base_url=self._api)
return self._iter(int(number), url, Key, etag=etag)
@requires_auth
def organization_events(self, org, number=-1, etag=None):
r"""Iterate over events from the user's organization dashboard.
.. note:: You must be authenticated to view this.
:param str org: (required), name of the organization
:param int number: (optional), number of events to return. Default: -1
returns all available events
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`Event <github3.events.Event>`\ s
"""
url = ''
if org:
url = self._build_url('events', 'orgs', org, base_url=self._api)
return self._iter(int(number), url, Event, etag=etag)
def received_events(self, public=False, number=-1, etag=None):
r"""Iterate over events that the user has received.
If the user is the authenticated user, you will see private and public
events, otherwise you will only see public events.
:param bool public: (optional), determines if the authenticated user
sees both private and public or just public
:param int number: (optional), number of events to return. Default: -1
returns all events available
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`Event <github3.events.Event>`\ s
"""
path = ['received_events']
if public:
path.append('public')
url = self._build_url(*path, base_url=self._api)
return self._iter(int(number), url, Event, etag=etag)
def organizations(self, number=-1, etag=None):
r"""Iterate over organizations the user is member of.
:param int number: (optional), number of organizations to return.
Default: -1 returns all available organization
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of
:class:`ShortOrganization <github3.orgs.ShortOrganization>`\ s
"""
# Import here, because a toplevel import causes an import loop
from .orgs import ShortOrganization
url = self._build_url('orgs', base_url=self._api)
return self._iter(int(number), url, ShortOrganization, etag=etag)
def starred_repositories(self, sort=None, direction=None, number=-1,
etag=None):
"""Iterate over repositories starred by this user.
.. versionchanged:: 0.5
Added sort and direction parameters (optional) as per the change in
GitHub's API.
:param int number: (optional), number of starred repos to return.
Default: -1, returns all available repos
:param str sort: (optional), either 'created' (when the star was
created) or 'updated' (when the repository was last pushed to)
:param str direction: (optional), either 'asc' or 'desc'. Default:
'desc'
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`~github3.repos.repo.StarredRepository`
"""
from .repos import Repository, StarredRepository
params = {'sort': sort, 'direction': direction}
self._remove_none(params)
url = self.starred_urlt.expand(owner=None, repo=None)
return self._iter(int(number), url, StarredRepository, params, etag,
headers=Repository.STAR_HEADERS)
def subscriptions(self, number=-1, etag=None):
"""Iterate over repositories subscribed to by this user.
:param int number: (optional), number of subscriptions to return.
Default: -1, returns all available
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`Repository <github3.repos.Repository>`
"""
from .repos import ShortRepository
url = self._build_url('subscriptions', base_url=self._api)
return self._iter(int(number), url, ShortRepository, etag=etag)
@requires_auth
def rename(self, login):
"""Rename the user.
.. note::
This is only available for administrators of a GitHub Enterprise
instance.
:param str login: (required), new name of the user
:returns: bool
"""
url = self._build_url('admin', 'users', self.login)
payload = {'login': login}
resp = self._boolean(self._patch(url, data=payload), 202, 403)
return resp
@requires_auth
def impersonate(self, scopes=None):
"""Obtain an impersonation token for the user.
The retrieved token will allow impersonation of the user.
This is only available for admins of a GitHub Enterprise instance.
:param list scopes: (optional), areas you want this token to apply to,
i.e., 'gist', 'user'
:returns: :class:`Authorization <Authorization>`
"""
url = self._build_url('admin', 'users', self.login, 'authorizations')
data = {}
if scopes:
data['scopes'] = scopes
json = self._json(self._post(url, data=data), 201)
return self._instance_or_null(Authorization, json)
@requires_auth
def revoke_impersonation(self):
"""Revoke all impersonation tokens for the current user.
This is only available for admins of a GitHub Enterprise instance.
:returns: bool -- True if successful, False otherwise
"""
url = self._build_url('admin', 'users', self.login, 'authorizations')
return self._boolean(self._delete(url), 204, 403)
@requires_auth
def promote(self):
"""Promote a user to site administrator.
This is only available for admins of a GitHub Enterprise instance.
:returns: bool -- True if successful, False otherwise
"""
url = self._build_url('site_admin', base_url=self._api)
return self._boolean(self._put(url), 204, 403)
@requires_auth
def demote(self):
"""Demote a site administrator to simple user.
You can demote any user account except your own.
This is only available for admins of a GitHub Enterprise instance.
:returns: bool -- True if successful, False otherwise
"""
url = self._build_url('site_admin', base_url=self._api)
return self._boolean(self._delete(url), 204, 403)
@requires_auth
def suspend(self):
"""Suspend the user.
This is only available for admins of a GitHub Enterprise instance.
This API is disabled if you use LDAP, check the GitHub API dos for more
information.
:returns: bool -- True if successful, False otherwise
"""
url = self._build_url('suspended', base_url=self._api)
return self._boolean(self._put(url), 204, 403)
@requires_auth
def unsuspend(self):
"""Unsuspend the user.
This is only available for admins of a GitHub Enterprise instance.
This API is disabled if you use LDAP, check the GitHub API dos for more
information.
:returns: bool -- True if successful, False otherwise
"""
url = self._build_url('suspended', base_url=self._api)
return self._boolean(self._delete(url), 204, 403)
@requires_auth
def delete(self):
"""Delete the user.
Per GitHub API documentation, it is often preferable to suspend the
user.
.. note::
This is only available for admins of a GitHub Enterprise instance.
:returns: bool -- True if successful, False otherwise
"""
url = self._build_url('admin', 'users', self.login)
return self._boolean(self._delete(url), 204, 403)
class ShortUser(_User):
"""Object for the shortened representation of a User.
GitHub's API returns different amounts of information about users based
upon how that information is retrieved. Often times, when iterating over
several users, GitHub will return less information. To provide a clear
distinction between the types of users, github3.py uses different classes
with different sets of attributes.
.. versionadded:: 1.0.0
"""
class_name = 'ShortUser'
class User(_User):
"""Object for the full representation of a User.
GitHub's API returns different amounts of information about users based
upon how that information is retrieved. This object exists to represent
the full amount of information returned for a specific user. For example,
you would receive this class when calling
:meth:`~github3.github.GitHub.user`. To provide a clear distinction
between the types of users, github3.py uses different classes with
different sets of attributes.
This object no longer contains information about the currently
authenticated user (e.g., :meth:`~github3.github.GitHub.me`).
.. versionchanged:: 1.0.0
"""
class_name = 'User'
def _update_attributes(self, user):
super(User, self)._update_attributes(user)
#: Markdown formatted biography
self.bio = user['bio']
#: URL of the blog
self.blog = user['blog']
#: Name of the company
self.company = user['company']
#: datetime object representing the date the account was created
self.created_at = self._strptime(user['created_at'])
#: E-mail address of the user/org
self.email = user['email']
# The number of people following this user
#: Number of followers
self.followers_count = user['followers']
# The number of people this user follows
#: Number of people the user is following
self.following_count = user['following']
#: True -- for hire, False -- not for hire
self.hireable = user['hireable']
#: Location of the user/org
self.location = user['location']
# e.g. first_name last_name
#: Real name of the user/org
self.name = user['name']
# The number of public_gists
#: Number of public gists
self.public_gists_count = user['public_gists']
# The number of public_repos
#: Number of public repos owned by the user/org
self.public_repos_count = user['public_repos']
self.updated_at = self._strptime(user['updated_at'])
class AuthenticatedUser(User):
"""Object to represent the currently authenticated user.
This is returned by :meth:`~github3.github.GitHub.me`. It contains the
extra informtation that is not returned for other users such as the
currently authenticated user's plan and private email information.
.. versionadded:: 1.0.0
.. versionchanged:: 1.0.0
The ``total_private_gists`` attribute is no longer returned by
GitHub's API and so is removed.
"""
class_name = 'AuthenticatedUser'
def _update_attributes(self, user):
super(AuthenticatedUser, self)._update_attributes(user)
#: How much disk consumed by the user
self.disk_usage = user['disk_usage']
#: Number of private repos owned by this user
self.owned_private_repos = user['owned_private_repos']
#: Total number of private repos
self.total_private_repos = user['total_private_repos']
#: Which plan this user is on
self.plan = Plan(user['plan'], self)
#: Number of repo contributions. Only appears in ``repo.contributors``
contributions = user.get('contributions')
# The refresh method uses __init__ to replace the attributes on the
# instance with what it receives from the /users/:username endpoint.
# What that means is that contributions is no longer returned and as
# such is changed because it doesn't exist. This guards against that.
if contributions is not None:
self.contributions = contributions