-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration.py
executable file
·385 lines (308 loc) · 14.5 KB
/
migration.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
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
#!/usr/bin/env python3
import argparse
import tempfile
import argh
from github.GithubObject import NotSet
from attachments import Attachments
import utility
import buhtig
import arij
__author__ = 'lpellegr'
from github import Github
import os
import re
class Migration:
def __init__(self):
self.github = Github(github_authentication_token)
self.github_organization = self.github.get_organization(
github_organization_name)
self.repositories = \
self.load_data("mapping-repositories.txt", lambda data,
chunks: self._create_repository_entries(
data, chunks))
@staticmethod
def _create_repository_entries(data, chunks):
github_repo_name = chunks[0]
if len(chunks) > 1:
github_repo_name = chunks[1]
data.append(RepositoryMappingEntry(chunks[0], github_repo_name))
def get_repository(self, github_repo_name):
return self.github_organization.get_repo(github_repo_name)
def create_repositories(self):
[self.create_repository(r.github_repo_name) for r in self.repositories]
def create_repository(self, github_repo_name):
utility.execute(
lambda: self.github_organization.create_repo(
github_repo_name, has_wiki=False, has_issues=False,
has_downloads=True),
"Repository " + github_repo_name + " created",
"Cannot create repository '" + github_repo_name + "' since it already exists")
def delete_repositories(self):
[self.delete_repository(r.github_repo_name) for r in self.repositories]
def delete_repository(self, github_repo_name):
utility.execute(
lambda: self.get_repository(github_repo_name).delete(),
"Repository " + github_repo_name + " deleted",
"Cannot delete repository '" + github_repo_name + "' since it does not exist")
def edit_repositories(self, description=None, homepage=None, private=None,
has_issues=None, has_wiki=None, default_branch=None):
[self.edit_repository(r.github_repo_name, description, homepage,
private, has_issues, has_wiki, default_branch) for
r in self.repositories]
def edit_repository(self, github_repo_name, description=NotSet,
homepage=NotSet, private=NotSet, has_issues=NotSet,
has_wiki=NotSet, default_branch=NotSet):
repo = self.get_repository(github_repo_name)
description = Migration.transform_string(description)
homepage = Migration.transform_string(homepage)
private = Migration.transform_bool(private)
has_issues = Migration.transform_bool(has_issues)
has_wiki = Migration.transform_bool(has_wiki)
default_branch = Migration.transform_string(default_branch)
utility.execute(
lambda: repo.edit(github_repo_name, description, homepage, private,
has_issues, has_wiki, default_branch),
"Repository " + github_repo_name + " edited",
"Cannot edit repository '" + github_repo_name + "' since it does not exist")
def clone_repositories(self, working_dir=None):
if working_dir is None:
working_dir = tempfile.TemporaryDirectory(
suffix='github-migration').name
[self.clone_repository(r.ow2_repo_name, working_dir) for r in
self.repositories]
@staticmethod
def clone_repository(ow2_repo_name, working_dir):
repo_dir = working_dir + os.sep + ow2_repo_name
if not os.path.exists(repo_dir):
print("Cloning repository {} in {}".format(ow2_repo_name, repo_dir))
command = "git clone --mirror {} {}".format(
"git://gitorious.ow2.org/{}/{}.git".format(
ow2_organization_name,
ow2_repo_name),
repo_dir)
if utility.execute_command(command) is 0:
print("Repository '{}' has been cloned".format(ow2_repo_name))
else:
print("Error occurred while cloning '{}'".format(ow2_repo_name))
else:
if utility.execute_command(
"cd {}; git remote update".format(repo_dir)) is 0:
print("Repository '{}' has been updated".format(ow2_repo_name))
else:
print(
"Error occurred while updating '{}'".format(ow2_repo_name))
def gc_repositories(self, working_dir=None):
if working_dir is None:
raise ValueError("Undefined argument --working-dir")
[self.gc_repository(r.ow2_repo_name, working_dir) for r in
self.repositories]
@staticmethod
def gc_repository(ow2_repo_name, working_dir):
repo_dir = working_dir + os.sep + ow2_repo_name
command = "cd {} && git reflog expire --expire=now --all && git gc --aggressive --prune=now && git repack -a -d -l".format(
repo_dir)
print("Executing command '{}'".format(command))
if utility.execute_command(command) is 0:
print(
"Repository '{}' has been garbage collected".format(
ow2_repo_name))
else:
print("Error occurred while garbage collecting '{}'".format(
ow2_repo_name))
def prune_repositories(self, working_dir=None):
filters = self.load_data("mapping-filters.txt",
lambda data, chunks: data.append(
FilterMappingEntry(chunks[0], chunks[1])))
for f in filters:
self.prune_repository(f, working_dir)
@staticmethod
def prune_repository(entry, working_dir):
repo_dir = working_dir + os.sep + entry.ow2_repo_name
command = "java -jar {} --delete-files {} {}".format(bfg_jar_path,
entry.bfg_filter,
repo_dir)
print("Executing command '{}'".format(command))
if utility.execute_command(command) is 0:
command = \
'cd {} && git reflog expire --expire=now --all && git gc --prune=now --aggressive'.format(
repo_dir)
print("Executing command '{}'".format(command))
if utility.execute_command(command) is 0:
print(
"Repository '{}' has been pruned".format(
entry.ow2_repo_name))
else:
print(
"Error occurred while pruning '{}'".format(entry.ow2_repo_name))
def import_repositories(self, working_dir=None):
if working_dir is None:
raise ValueError("Undefined argument --working-dir")
# Sort repositories by Github name, in reverse order. This way
# they will be imported so that at the end they appear
# lexicographically sorted on Github
sorted_repositories = sorted(self.repositories,
key=lambda r: r.github_repo_name,
reverse=True)
[self.import_repository(r, working_dir) for r in sorted_repositories]
@staticmethod
def import_repository(entry, working_dir):
repo_dir = working_dir + os.sep + entry.ow2_repo_name
github_url = "[email protected]:{}/{}.git".format(github_organization_name,
entry.github_repo_name)
if utility.execute_command(
"cd {} && git push --mirror {}".format(repo_dir,
github_url)) is 0:
print(
"Repository '{}' has been imported".format(entry.ow2_repo_name))
else:
print("Error occurred while importing '{}' to Github".format(
entry.ow2_repo_name))
@staticmethod
def load_data(file, appender):
data = []
with open(file, "r") as f:
lines = f.read().splitlines()
for line in lines:
if not line.startswith("#") and len(line) > 0:
chunks = re.split('\s+', line)
appender(data, chunks)
return data
def import_attachments(self, jira_endpoint,
github_attachments_repository_name,
working_dir=None):
attachments = Attachments(jira_endpoint,
github_organization_name,
github_attachments_repository_name,
working_dir=working_dir)
for entry in self._load_issues_mapping():
self.import_attachments_for_project(attachments,
entry.jira_project_key)
attachments.push_on_github(github_authentication_token)
@staticmethod
def import_attachments_for_project(attachments, jira_project_key):
attachments.fetch_from_jira(jira_project_key)
def import_issues(self, jira_endpoint, github_attachments_repository_name,
default_assignee=None):
mapping_usernames = self._load_usernames_mapping()
for entry in self._load_issues_mapping():
print(
"Importing issues from JIRA project {} to Github repository named '{}'".format(
entry.jira_project_key, entry.github_project_name))
self.import_issues_for_project(jira_endpoint,
entry.jira_project_key,
entry.github_project_name,
github_attachments_repository_name,
mapping_usernames,
default_assignee)
def _load_issues_mapping(self):
return self.load_data("mapping-issues.txt",
lambda data, chunks: data.append(
IssueMappingEntry(chunks[0], chunks[1])))
def _load_usernames_mapping(self):
result = {}
self.load_data("mapping-usernames.txt",
lambda data, chunks:
result.update({chunks[0]: chunks[1]}))
return result
def import_issues_for_project(self, jira_endpoint, jira_project_key,
github_project_name,
github_attachments_repository_name=None,
mapping_usernames=None,
default_assignee=None):
jira_project = arij.JiraProject(jira_endpoint, jira_project_key)
github_comet = buhtig.GithubComet(jira_project, self.github,
github_organization_name,
github_project_name,
github_authentication_token)
# delete default labels created by Github
github_comet.delete_labels()
# create custom default labels
github_comet.create_labels({
# labels related to issue priority
'priority:blocker': 'ff6666',
'priority:critical': 'ff8080',
'priority:major': 'ff9999',
'priority:minor': 'ffb2b2',
'priority:trivial': 'ffcccc',
# labels related to issue resolution
'resolution:cannot-reproduce': 'bfe5bf',
'resolution:duplicate': 'bfe5bf',
'resolution:fixed': 'bfe5bf',
'resolution:incomplete': 'bfe5bf',
'resolution:invalid': 'bfe5bf',
'resolution:wont-fix': 'bfe5bf',
# labels related to issue type
'type:bug': 'c7def8',
'type:improvement': 'c7def8',
'type:new-feature': 'c7def8',
'type:story': 'c7def8',
'type:story-item': 'c7def8',
'type:task': 'c7def8',
'type:task-related-bug': 'c7def8'
})
github_comet.create_milestones()
github_comet.import_issues(github_attachments_repository_name,
mapping_usernames, default_assignee)
@staticmethod
def transform_bool(v):
if v is None:
return NotSet
else:
return Migration.str2bool(v)
@staticmethod
def transform_string(v):
if v is None:
return NotSet
else:
return v
@staticmethod
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
class FilterMappingEntry:
def __init__(self, ow2_repo_name, bfg_filter):
self.ow2_repo_name = ow2_repo_name
self.bfg_filter = bfg_filter
def __str__(self):
return "{} {}".format(self.ow2_repo_name, self.bfg_filter)
class IssueMappingEntry:
def __init__(self, jira_project_key, github_project_name):
self.jira_project_key = jira_project_key
self.github_project_name = github_project_name
class RepositoryMappingEntry:
def __init__(self, ow2_repo_name, github_repo_name):
self.ow2_repo_name = ow2_repo_name
self.github_repo_name = github_repo_name
def __str__(self, *args, **kwargs):
return "OW2={} \t\t\t\t Github={}".format(self.ow2_repo_name,
self.github_repo_name)
def main():
github_subcommands = [
migration.create_repositories,
migration.delete_repositories,
migration.edit_repositories,
migration.import_repositories,
migration.import_attachments,
migration.import_issues,
migration.import_issues_for_project
]
ow2_subcommands = [
migration.clone_repositories,
migration.gc_repositories,
migration.prune_repositories
]
parser = argparse.ArgumentParser()
argh.add_commands(parser, github_subcommands, namespace='github')
argh.add_commands(parser, ow2_subcommands, namespace='ow2')
argh.dispatch(parser)
if __name__ == "__main__":
from requests.packages import urllib3
urllib3.disable_warnings()
try:
bfg_jar_path = utility.get_env_var("BFG_JAR_PATH")
github_authentication_token = utility.get_env_var("GITHUB_TOKEN")
github_organization_name = utility.get_env_var("GITHUB_ORGANIZATION")
ow2_organization_name = utility.get_env_var("OW2_ORGANIZATION")
migration = Migration()
main()
except utility.UndefinedEnvironmentVariable as e:
utility.error("Undefined environment variable: {}".format(e.var_name))