forked from ask/python-github2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_manage_collaborators
More file actions
81 lines (65 loc) · 3.22 KB
/
github_manage_collaborators
File metadata and controls
81 lines (65 loc) · 3.22 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
#!/usr/bin/env python
# encoding: utf-8
"""github_manage_collaborators - add/remove collaborators to all github
projects of an account.
Typically used with company accounts where all coders should have
R/W access to all private repositories of the company account.
"""
# Created by Maximillian Dornseif on 2009-12-31 for HUDORA.
# Copyright (c) 2009 HUDORA. All rights reserved.
# BSD licensed
from optparse import OptionParser
import github2.client
def parse_commandline():
"""Parse the comandline and return parsed options."""
parser = OptionParser()
parser.description = __doc__
parser.set_usage('usage: %prog [options] (list|add|remove) [collaborator].'
'Try %prog --help for details.')
parser.add_option('-d', '--debug', action='store_true',
help='Enables debugging mode')
parser.add_option('-l', '--login',
help='Username to login with')
parser.add_option('-a', '--account',
help='User owning the repositories to be changed ' \
'[default: same as --login]')
parser.add_option('-t', '--apitoken',
help='API Token - can be found on the lower right of ' \
'https://github.com/account')
options, args = parser.parse_args()
if len(args) not in [1, 2]:
parser.error('wrong number of arguments')
if (len(args) == 1 and args[0] in ['add', 'remove']):
parser.error('%r needs a collaborator name as second parameter\n' % args[0])
elif (len(args) == 1 and args[0] != 'list'):
parser.error('unknown command %r. Try "list", "add" or "remove"\n' % args[0])
if (len(args) == 2 and args[0] not in ['add', 'remove']):
parser.error('unknown command %r. Try "list", "add" or "remove"\n' % args[0])
if not options.login:
parser.error('you must provide --login information\n')
return options, args
def main(options, args):
"""This implements the actual program functionality"""
if not options.account:
options.account = options.login
github = github2.client.Github(username=options.login,
api_token=options.apitoken,
debug=options.debug)
if len(args) == 1:
for repos in github.repos.list(options.account):
fullreposname = github.project_for_user_repo(options.account, repos.name)
print "%s: %s" % (repos.name, ' '.join(github.repos.list_collaborators(fullreposname)))
elif len(args) == 2:
command, collaborator = args
for repos in github.repos.list(options.account):
fullreposname = github.project_for_user_repo(options.account, repos.name)
if collaborator in github.repos.list_collaborators(fullreposname):
if command == 'remove':
github.repos.remove_collaborator(repos.name, collaborator)
print "removed %r from %r" % (collaborator, repos.name)
else:
if command == 'add':
github.repos.add_collaborator(repos.name, collaborator)
print "added %r to %r" % (collaborator, repos.name)
if __name__ == '__main__':
main(*parse_commandline())