-
Notifications
You must be signed in to change notification settings - Fork 2
/
mk_repo.py
executable file
·87 lines (62 loc) · 2.29 KB
/
mk_repo.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
#!/usr/bin/env python3
import sys
from unittest.mock import MagicMock
import boatswain_env as benv
from github import Github
from add_collaborator import do_add_collaborator
CMD_NAME = 'add_collaborators'
DESC = 'Add multiple users as collaborator to GitHub repo'
def mk_repo_deco(parser):
parser.add_argument('org',
type=str,
help='organization name',
metavar='<org>',
)
parser.add_argument('prefix',
type=str,
help='prefix prepended to each repo',
metavar='<prefix>',
)
parser.add_argument('repo',
type=str,
help='repo name, appended to prefix',
metavar='<repo>',
)
def fmt_hyphen(prefix, name):
if prefix[-1] == '-':
return '{}{}'.format(prefix, name)
else:
return '{}-{}'.format(prefix, name)
def do_mk_repo(org, repo_name, opt):
opt.info('Creating private repo {} under organization {}'
.format(repo_name, org))
if opt.noop:
opt.log('--noop option specified; not creating repo')
mock_repo = MagicMock()
mock_repo.full_name, mock_repo.name = repo_name, repo_name
return mock_repo
repo = org.create_repo(repo_name, private=True)
# funky output so easily greppable
opt.log('@CREATED_REPO: {} {}'.format(repo_name, repo.git_url))
return repo
def mk_repo(opt):
g = Github(opt.githubToken())
org = g.get_organization(opt.org)
repo_name = fmt_hyphen(opt.prefix, opt.repo)
if not opt.promptYes(('Are you sure you would like to create a private '
'repo under organization {} named {}?')
.format(opt.org, repo_name),
True):
opt.warn('Aborting')
return
opt.info('Proceeding to create private repo {}/{}'
.format(opt.org, repo_name))
do_mk_repo(org, repo_name, opt)
def main(args=None, config_path=None):
if args is None:
args = sys.argv[1:]
opt = benv.ParseOption(args, section=CMD_NAME, config_path=config_path,
desc=DESC, parse_deco=mk_repo_deco, req_github=True)
mk_repo(opt)
if __name__ == '__main__':
main()