-
Notifications
You must be signed in to change notification settings - Fork 2
/
lab2hub.py
executable file
·149 lines (104 loc) · 3.68 KB
/
lab2hub.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
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
#!/usr/bin/python
import requests
import json
import settings
from urlparse import urlparse, parse_qs
from git import Repo
def getLastPage(link_header):
links = link_header.split(',')
for link in links:
link = link.split(';')
if link[1].strip() == 'rel="last"':
# Trim it for garbage, and get the url params out.
link_data = parse_qs(link[0].strip(' <>').split('?')[1])
last_page = int(link_data['page'][0])
return last_page
return 0
github_urls = {
'repos': 'https://api.github.com/orgs/{}/repos', # GET
'create': 'https://api.github.com/orgs/{}/repos' # POST
}
gitlab_urls = {
'projects': settings.GITLAB_DOMAIN + '/api/v3/projects/all',
'repos': settings.GITLAB_DOMAIN + '/projects/{}/repository/tree'
}
# Get a list of all GitHub projects.
# This is so we don't create duplicates or overwrite anything.
print 'Getting projects from GitHub'
gh_projects = []
gh_payload = {'access_token': settings.GITHUB_TOKEN}
# First off, how many pages do we have?
# Get the first page.
head = requests.get(github_urls['repos'].format(settings.GITHUB_ORG), gh_payload)
# Look for the last page link in the headers
last_page = getLastPage(head.headers['link'])
if last_page != 0:
for page in range(1, last_page + 1):
new_payload = gh_payload
new_payload['page'] = page
r = requests.get(github_urls['repos'].format(settings.GITHUB_ORG), new_payload)
existing_projects = json.loads(r.text)
for existing_project in existing_projects:
# print existing_project['name']
gh_projects.append(existing_project['name'])
print 'Found ' + str(len(gh_projects)) + ' projects'
else:
print 'Didn\'t find any existing projects.'
# Get a list of all GitLab projects
print 'Getting GitLab projects'
gl_projects = []
gl_payload = {'private_token': settings.GITLAB_TOKEN}
# Get the first page.
head = requests.get(gitlab_urls['projects'], gl_payload)
# Look for the last page link in the headers
last_page = getLastPage(head.headers['link'])
if last_page != 0:
for page in range(1, last_page + 1):
new_payload = gl_payload
new_payload['page'] = page
r = requests.get(gitlab_urls['projects'], params=new_payload)
projects = json.loads(r.text)
for project in projects:
# print '{}/{}'.format(project['namespace']['name'], project['name'])
if project['name'] in gh_projects:
pass
# print 'Duplicate name - skipping'
# print ''
else:
gl_projects.append(project)
print 'Found ' + str(len(gl_projects)) + ' projects'
else:
print 'Didn\'t find any projects.'
# Create the projects
for project in gl_projects:
print '-- {}/{} --'.format(project['namespace']['name'], project['name'])
print 'Creating on GitHub'
repo_payload = {}
repo_payload['name'] = project['name']
repo_payload['private'] = True
gh_repo = False
r = requests.post(github_urls['repos'].format(settings.GITHUB_ORG), params=gh_payload, data=json.dumps(repo_payload))
if r.status_code != 201:
print '-> Failed: Error ' + str(r.status_code)
print ''
pass
else:
print 'Getting repo from GitLab'
gh_repo = json.loads(r.text)
repo_folder = settings.TEMP_FOLDER + project['name']
repo = Repo.init(repo_folder)
origin = repo.create_remote('origin',project['ssh_url_to_repo'])
origin.fetch()
try :
origin.pull(origin.refs[0].remote_head)
except AssertionError as error:
print '-> Failed: Remotes problem'
continue
print 'Pushing repo to GitHub'
gh_remote = repo.create_remote('gh',gh_repo['ssh_url'])
push_result = gh_remote.push('--all')
if len(push_result) is None:
print '-> Failed'
else:
print 'Done'
print ''