-
Notifications
You must be signed in to change notification settings - Fork 11
/
create-github-repo.py
83 lines (57 loc) · 2.02 KB
/
create-github-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
import sys
import requests
import json
DST_ORG = "citrix-openstack-build"
GURL = 'https://api.github.com'
GETREPO = GURL + '/repos/%s/%s'
FORKREPO = GURL + '/repos/%s/%s/forks'
class Repo(object):
def __init__(self, provider, org, name):
self.org = org
self.name = name
self.provider = provider
def __str__(self):
return "Repository %s %s %s" % (self.provider, self.org, self.name)
def create_repo_from_line(repoline):
varname, org, repo, provider = repoline.strip().split(' ')
assert repo.endswith('.git')
return Repo(provider, org, repo[:-len('.git')])
def status_is_ok(status):
return status >= 200 and status < 300
def fork_repo(username, password, repo):
params = dict(auth=(username, password))
r = requests.post(FORKREPO % (repo.org, repo.name), **params)
assert status_is_ok(r.status_code)
def repo_missing(username, password, repo):
params = dict(auth=(username, password))
r = requests.get(GETREPO % (repo.org, repo.name), **params)
if r.status_code == 404:
return True
assert status_is_ok(r.status_code)
def deal_with_repos(username, password, repos):
print "Checking that all repos exist"
missing = []
total = 0
for repo_record in repos:
total += 1
if repo_missing(username, password, repo_record):
missing.append(repo_record)
if len(missing):
print "Some repos are missing:"
for repo in missing:
print " ", repo
return 1
print "OK"
for src_repo in repos:
dst_repo = Repo('github', DST_ORG, src_repo.name)
if repo_missing(username, password, dst_repo):
print dst_repo, "does not exist, forking", src_repo
fork_repo(username, password, src_repo)
print "OK"
return 0
def main():
username, password = sys.argv[1:]
repo_records = [create_repo_from_line(line) for line in sys.stdin.readlines()]
sys.exit(deal_with_repos(username, password, repo_records))
if __name__ == "__main__":
main()