forked from haysclark/bitbucket_issue_migration
/
migrate.py
348 lines (275 loc) · 10.1 KB
/
migrate.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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# This file is part of the bitbucket issue migration script.
#
# The script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the bitbucket issue migration script.
# If not, see <http://www.gnu.org/licenses/>.
import argparse
import urllib2
import getpass
import logging
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import base64
from github import Github
from github import GithubException
logging.basicConfig(level = logging.ERROR)
try:
import json
except ImportError:
import simplejson as json
def output(string):
sys.stdout.write(string)
sys.stdout.flush()
def read_arguments():
parser = argparse.ArgumentParser(
description=(
"A tool to migrate issues from Bitbucket to GitHub.\n"
"note: the Bitbucket repository and issue tracker have to be"
"public"
)
)
parser.add_argument(
"bitbucket_username",
help="Your Bitbucket username"
)
parser.add_argument(
"bitbucket_password",
help="Your Bitbucket password"
)
parser.add_argument(
"bitbucket_repo",
help="Bitbucket repository to pull data from."
)
parser.add_argument(
"github_username",
help="Your GitHub username"
)
parser.add_argument(
"github_repo",
help="GitHub to add issues to. Format: <username>/<repo name>"
)
parser.add_argument(
"-n", "--dry-run",
action="store_true", dest="dry_run", default=False,
help="Perform a dry run and print eveything."
)
parser.add_argument(
"-f", "--start", type=int, dest="start", default=0,
help="Bitbucket issue id from which to start import"
)
return parser.parse_args()
# Formatters
def format_user(author_info):
if not author_info:
return "Anonymous"
if author_info['first_name'] and author_info['last_name']:
return " ".join([author_info['first_name'], author_info['last_name']])
if 'username' in author_info:
return '[{0}](http://bitbucket.org/{0})'.format(
author_info['username']
)
def format_name(issue):
if 'reported_by' in issue:
return format_user(issue['reported_by'])
else:
return "Anonymous"
def format_body(options, issue):
content = clean_body(issue.get('content'))
return u"""{}
{}
- Bitbucket: https://bitbucket.org/{}/{}/issue/{}
- Originally reported by: {}
- Originally created at: {}
""".format(
content,
'-' * 40,
options.bitbucket_username, options.bitbucket_repo, issue['local_id'],
format_name(issue),
issue['created_on']
)
def format_comment(comment):
return u"""{}
{}
Original comment by: {}
""".format(
comment['body'],
'-' * 40,
comment['user'].encode('utf-8')
)
def clean_body(body):
lines = []
in_block = False
for line in unicode(body).splitlines():
if line.startswith("{{{") or line.startswith("}}}"):
if "{{{" in line:
before, part, after = line.partition("{{{")
lines.append(' ' + after)
in_block = True
if "}}}" in line:
before, part, after = line.partition("}}}")
lines.append(' ' + before)
in_block = False
else:
if in_block:
lines.append(" " + line)
else:
lines.append(line.replace("{{{", "`").replace("}}}", "`"))
return "\n".join(lines)
# Bitbucket fetch
def get_issues(bb_url, start_id):
'''
Fetch the issues from Bitbucket
'''
issues = []
while True:
url = "{}/?start={}".format(
bb_url,
start_id
)
try:
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (options.bitbucket_username, options.bitbucket_password)).replace('\n','')
request.add_header("Authorization", "Basic %s" % base64string)
response = urllib2.urlopen(request)
except urllib2.HTTPError as ex:
ex.message = (
'Problem trying to connect to bitbucket ({url}): {ex} '
'Hint: the bitbucket repository name is case-sensitive.'
.format(url=url, ex=ex)
)
raise
else:
result = json.loads(response.read())
if not result['issues']:
# Check to see if there is issues to process if not break out.
break
issues += result['issues']
start_id += len(result['issues'])
return issues
def get_comments(bb_url, issue):
'''
Fetch the comments for a Bitbucket issue
'''
url = "{}/{}/comments/".format(
bb_url,
issue['local_id']
)
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (options.bitbucket_username, options.bitbucket_password)).replace('\n','')
request.add_header("Authorization", "Basic %s" % base64string)
result = json.loads(urllib2.urlopen(request).read())
ordered = sorted(result, key=lambda comment: comment["utc_created_on"])
comments = []
for comment in ordered:
body = comment['content'] or ''
# Status comments (assigned, version, etc. changes) have in bitbucket
# no body
if body:
comments.append({
'user': format_user(comment['author_info']),
'created_at': comment['utc_created_on'],
'body': body.encode('utf-8'),
'number': comment['comment_id']
})
return comments
def github_label(name, color = "FFFFFF"):
""" Returns the Github label with the given name, creating it if necessary. """
try:
return label_cache[name]
except KeyError:
try:
return label_cache.setdefault(name, github_repo.get_label(name))
except GithubException:
return label_cache.setdefault(name, github_repo.create_label(name, color))
def add_comments_to_issue(github_issue, bitbucket_comments):
""" Migrates all comments from a Bitbucket issue to its Github copy. """
# Retrieve existing Github comments, to figure out which Google Code comments are new
existing_comments = [comment.body for comment in github_issue.get_comments()]
if len(bitbucket_comments) > 0:
output(", adding comments")
for i, comment in enumerate(bitbucket_comments):
body = u'_From {user} on {created_at}_\n\n{body}'.format(**comment)
if body in existing_comments:
logging.info('Skipping comment %d: already present', i + 1)
else:
logging.info('Adding comment %d', i + 1)
if not options.dry_run:
github_issue.create_comment(body.encode('utf-8'))
output('.')
output('\n')
# GitHub push
def push_issue(gh_username, gh_repository, issue, body):
""" Migrates the given Bitbucket issue to Github. """
body = issue['content'].replace('%', '%')
output('Adding issue [%d]: %s' % (issue.get('local_id'), issue.get('title').encode('utf-8')))
github_labels = []
# Set the status and labels
if issue.get('status') == 'resolved':
pass
# Everything else is done with labels in github
else:
github_labels = [github_label(issue['status'])]
github_issue = None
if not options.dry_run:
github_issue = github_repo.create_issue(issue['title'], body = body.encode('utf-8'), labels = github_labels)
# Set the status and labels
if issue.get('status') == 'resolved':
github_issue.edit(state = 'closed')
# Milestones
return github_issue
if __name__ == "__main__":
options = read_arguments()
bb_url = "https://bitbucket.org/api/1.0/repositories/{}/{}/issues".format(
options.bitbucket_username,
options.bitbucket_repo
)
# Cache Github tags, to avoid unnecessary API requests
label_cache = {}
google_project_name = options.github_repo
# fetch issues from Bitbucket
issues = get_issues(bb_url, options.start)
while True:
github_password = getpass.getpass("Github password: ")
try:
Github(options.github_username, github_password).get_user().login
break
except Exception:
output("Bad credentials, try again.\n")
github = Github(options.github_username, github_password)
github_user = github.get_user()
# If the project name is specified as owner/project, assume that it's owned by either
# a different user than the one we have credentials for, or an organization.
if "/" in google_project_name:
gh_username, gh_repository = google_project_name.split('/')
try:
github_owner = github.get_user(gh_username)
except GithubException:
try:
github_owner = github.get_organization(gh_username)
except GithubException:
github_owner = github_user
else:
github_owner = github_user
github_repo = github_owner.get_repo(gh_repository)
# Sort issues, to sync issue numbers on freshly created GitHub projects.
# Note: not memory efficient, could use too much memory on large projects.
for issue in sorted(issues, key=lambda issue: issue['local_id']):
body = format_body(options, issue).encode('utf-8')
github_issue = push_issue(gh_username, gh_repository, issue, body)
if github_issue:
comments = get_comments(bb_url, issue)
add_comments_to_issue(github_issue, comments)
output("Created {} issues\n".format(len(issues)))