forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorgs.py
More file actions
466 lines (382 loc) · 16.1 KB
/
orgs.py
File metadata and controls
466 lines (382 loc) · 16.1 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
"""
github3.orgs
============
This module contains all of the classes related to organizations.
"""
from json import dumps
from github3.events import Event
from github3.models import BaseAccount, GitHubCore
from github3.repos import Repository
from github3.users import User
from github3.decorators import requires_auth
class Team(GitHubCore):
"""The :class:`Team <Team>` object.
Two team instances can be checked like so::
t1 == t2
t1 != t2
And is equivalent to::
t1.id == t2.id
t1.id != t2.id
See also: http://developer.github.com/v3/orgs/teams/
"""
def __init__(self, team, session=None):
super(Team, self).__init__(team, session)
self._api = team.get('url', '')
#: This team's name.
self.name = team.get('name')
#: Unique ID of the team.
self.id = team.get('id')
#: Permission leve of the group
self.permission = team.get('permission')
#: Number of members in this team.
self.members_count = team.get('members_count')
#: Number of repos owned by this team.
self.repos_count = team.get('repos_count')
def __repr__(self):
return '<Team [{0}]>'.format(self.name)
def __eq__(self, other):
return self.id == other.id
def __ne__(self, other):
return self.id != other.id
def _update_(self, team):
self.__init__(team, self._session)
@requires_auth
def add_member(self, login):
"""Add ``login`` to this team.
:returns: bool
"""
url = self._build_url('members', login, base_url=self._api)
return self._boolean(self._put(url), 204, 404)
@requires_auth
def add_repo(self, repo):
"""Add ``repo`` to this team.
:param str repo: (required), form: 'user/repo'
:returns: bool
"""
url = self._build_url('repos', repo, base_url=self._api)
return self._boolean(self._put(url), 204, 404)
@requires_auth
def delete(self):
"""Delete this team.
:returns: bool
"""
return self._boolean(self._delete(self._api), 204, 404)
@requires_auth
def edit(self, name, permission=''):
"""Edit this team.
:param str name: (required)
:param str permission: (optional), ('pull', 'push', 'admin')
:returns: bool
"""
if name:
data = {'name': name, 'permission': permission}
json = self._json(self._patch(self._api, data=dumps(data)), 200)
if json:
self._update_(json)
return True
return False
def has_repo(self, repo):
"""Checks if this team has access to ``repo``
:param str repo: (required), form: 'user/repo'
:returns: bool
"""
url = self._build_url('repos', repo, base_url=self._api)
return self._boolean(self._get(url), 204, 404)
def is_member(self, login):
"""Check if ``login`` is a member of this team.
:param str login: (required), login name of the user
:returns: bool
"""
url = self._build_url('members', login, base_url=self._api)
return self._boolean(self._get(url), 204, 404)
def iter_members(self, number=-1, etag=None):
"""Iterate over the members of this team.
:param int number: (optional), number of users to iterate over.
Default: -1 iterates over all values
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`User <github3.users.User>`\ s
"""
url = self._build_url('members', base_url=self._api)
return self._iter(int(number), url, User, etag=etag)
def iter_repos(self, number=-1, etag=None):
"""Iterate over the repositories this team has access to.
:param int number: (optional), number of repos to iterate over.
Default: -1 iterates over all values
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`Repository <github3.repos.Repository>`
objects
"""
url = self._build_url('repos', base_url=self._api)
return self._iter(int(number), url, Repository, etag=etag)
@requires_auth
def remove_member(self, login):
"""Remove ``login`` from this team.
:param str login: (required), login of the member to remove
:returns: bool
"""
url = self._build_url('members', login, base_url=self._api)
return self._boolean(self._delete(url), 204, 404)
@requires_auth
def remove_repo(self, repo):
"""Remove ``repo`` from this team.
:param str repo: (required), form: 'user/repo'
:returns: bool
"""
url = self._build_url('repos', repo, base_url=self._api)
return self._boolean(self._delete(url), 204, 404)
class Organization(BaseAccount):
"""The :class:`Organization <Organization>` object.
Two organization instances can be checked like so::
o1 == o2
o1 != o2
And is equivalent to::
o1.id == o2.id
o1.id != o2.id
See also: http://developer.github.com/v3/orgs/
"""
def __init__(self, org, session=None):
super(Organization, self).__init__(org, session)
if not self.type:
self.type = 'Organization'
#: Number of private repositories.
self.private_repos = org.get('private_repos', 0)
@requires_auth
def add_member(self, login, team):
"""Add ``login`` to ``team`` and thereby to this organization.
Any user that is to be added to an organization, must be added
to a team as per the GitHub api.
.. note::
This method is of complexity O(n). This iterates over all teams in
your organization and only adds the user when the team name
matches the team parameter above. If you want constant time, you
should retrieve the team and call ``add_member`` on that team
directly.
:param str login: (required), login name of the user to be added
:param str team: (required), team name
:returns: bool
"""
for t in self.iter_teams():
if team == t.name:
return t.add_member(login)
return False
@requires_auth
def add_repo(self, repo, team):
"""Add ``repo`` to ``team``.
.. note::
This method is of complexity O(n). This iterates over all teams in
your organization and only adds the repo when the team name
matches the team parameter above. If you want constant time, you
should retrieve the team and call ``add_repo`` on that team
directly.
:param str repo: (required), form: 'user/repo'
:param str team: (required), team name
"""
for t in self.iter_teams():
if team == t.name:
return t.add_repo(repo)
return False
@requires_auth
def create_repo(self,
name,
description='',
homepage='',
private=False,
has_issues=True,
has_wiki=True,
has_downloads=True,
team_id=0,
auto_init=False,
gitignore_template=''):
"""Create a repository for this organization if the authenticated user
is a member.
:param str name: (required), name of the repository
:param str description: (optional)
:param str homepage: (optional)
:param bool private: (optional), If ``True``, create a private
repository. API default: ``False``
:param bool has_issues: (optional), If ``True``, enable issues for
this repository. API default: ``True``
:param bool has_wiki: (optional), If ``True``, enable the wiki for
this repository. API default: ``True``
:param bool has_downloads: (optional), If ``True``, enable downloads
for this repository. API default: ``True``
:param int team_id: (optional), id of the team that will be granted
access to this repository
:param bool auto_init: (optional), auto initialize the repository.
:param str gitignore_template: (optional), name of the template; this
is ignored if auto_int = False.
:returns: :class:`Repository <github3.repos.Repository>`
.. warning: ``name`` should be no longer than 100 characters
"""
url = self._build_url('repos', base_url=self._api)
data = {'name': name, 'description': description,
'homepage': homepage, 'private': private,
'has_issues': has_issues, 'has_wiki': has_wiki,
'has_downloads': has_downloads, 'auto_init': auto_init,
'gitignore_template': gitignore_template}
if team_id > 0:
data.update({'team_id': team_id})
json = self._json(self._post(url, data), 201)
return Repository(json, self) if json else None
@requires_auth
def conceal_member(self, login):
"""Conceal ``login``'s membership in this organization.
:returns: bool
"""
url = self._build_url('public_members', login, base_url=self._api)
return self._boolean(self._delete(url), 204, 404)
@requires_auth
def create_team(self, name, repo_names=[], permissions=''):
"""Assuming the authenticated user owns this organization,
create and return a new team.
:param str name: (required), name to be given to the team
:param list repo_names: (optional) repositories, e.g.
['github/dotfiles']
:param str permissions: (optional), options:
- ``pull`` -- (default) members can not push or administer
repositories accessible by this team
- ``push`` -- members can push and pull but not administer
repositories accessible by this team
- ``admin`` -- members can push, pull and administer
repositories accessible by this team
:returns: :class:`Team <Team>`
"""
data = {'name': name, 'repo_names': repo_names,
'permissions': permissions}
url = self._build_url('teams', base_url=self._api)
json = self._json(self._post(url, data), 201)
return Team(json, self._session) if json else None
@requires_auth
def edit(self,
billing_email=None,
company=None,
email=None,
location=None,
name=None):
"""Edit this organization.
:param str billing_email: (optional) Billing email address (private)
:param str company: (optional)
:param str email: (optional) Public email address
:param str location: (optional)
:param str name: (optional)
:returns: bool
"""
json = None
data = {'billing_email': billing_email, 'company': company,
'email': email, 'location': location, 'name': name}
self._remove_none(data)
if data:
json = self._json(self._patch(self._api, data=dumps(data)), 200)
if json:
self._update_(json)
return True
return False
def is_member(self, login):
"""Check if the user with login ``login`` is a member.
:returns: bool
"""
url = self._build_url('members', login, base_url=self._api)
return self._boolean(self._get(url), 204, 404)
def is_public_member(self, login):
"""Check if the user with login ``login`` is a public member.
:returns: bool
"""
url = self._build_url('public_members', login, base_url=self._api)
return self._boolean(self._get(url), 204, 404)
def iter_events(self, number=-1, etag=None):
"""Iterate over events for this org.
:param int number: (optional), number of events to return. Default: -1
iterates over 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
"""
url = self._build_url('events', base_url=self._api)
return self._iter(int(number), url, Event, etag=etag)
def iter_members(self, number=-1, etag=None):
"""Iterate over members of this organization.
:param int number: (optional), number of members to return. Default:
-1 will return all available.
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`User <github3.users.User>`\ s
"""
url = self._build_url('members', base_url=self._api)
return self._iter(int(number), url, User, etag=etag)
def iter_public_members(self, number=-1, etag=None):
"""Iterate over public members of this organization.
:param int number: (optional), number of members to return. Default:
-1 will return all available.
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`User <github3.users.User>`\ s
"""
url = self._build_url('public_members', base_url=self._api)
return self._iter(int(number), url, User, etag=etag)
def iter_repos(self, type='', number=-1, etag=None):
"""Iterate over repos for this organization.
:param str type: (optional), accepted values:
('all', 'public', 'member', 'private', 'forks', 'sources'), API
default: 'all'
:param int number: (optional), number of members to return. Default:
-1 will return all available.
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`Repository <github3.repos.Repository>`
"""
url = self._build_url('repos', base_url=self._api)
params = {}
if type in ('all', 'public', 'member', 'private', 'forks', 'sources'):
params['type'] = type
return self._iter(int(number), url, Repository, params, etag)
@requires_auth
def iter_teams(self, number=-1, etag=None):
"""Iterate over teams that are part of this organization.
:param int number: (optional), number of teams to return. Default: -1
returns all available teams.
:param str etag: (optional), ETag from a previous request to the same
endpoint
:returns: generator of :class:`Team <Team>`\ s
"""
url = self._build_url('teams', base_url=self._api)
return self._iter(int(number), url, Team, etag=etag)
@requires_auth
def publicize_member(self, login):
"""Make ``login``'s membership in this organization public.
:returns: bool
"""
url = self._build_url('public_members', login, base_url=self._api)
return self._boolean(self._put(url), 204, 404)
@requires_auth
def remove_member(self, login):
"""Remove the user with login ``login`` from this
organization.
:returns: bool
"""
url = self._build_url('members', login, base_url=self._api)
return self._boolean(self._delete(url), 204, 404)
@requires_auth
def remove_repo(self, repo, team):
"""Remove ``repo`` from ``team``.
:param str repo: (required), form: 'user/repo'
:param str team: (required)
:returns: bool
"""
for t in self.iter_teams():
if team == t.name:
return t.remove_repo(repo)
return False
@requires_auth
def team(self, team_id):
"""Returns Team object with information about team specified by
``team_id``.
:param int team_id: (required), unique id for the team
:returns: :class:`Team <Team>`
"""
json = None
if int(team_id) > 0:
url = self._build_url('teams', str(team_id))
json = self._json(self._get(url), 200)
return Team(json, self._session) if json else None