-
Notifications
You must be signed in to change notification settings - Fork 2
/
redmine_issues_to_github.py
executable file
·446 lines (383 loc) · 16.3 KB
/
redmine_issues_to_github.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
# +--------------------------------------------------+
# | Copyright (c) AD ticket GmbH |
# | All rights reserved. |
# +--------------------------------------------------+
# | AD ticket GmbH |
# | Kaiserstraße 69 |
# | D-60329 Frankfurt am Main |
# | |
# | phone: +49 (0)69 407 662 0 |
# | fax: +49 (0)69 407 662 50 |
# | mail: [email protected] |
# | web: www.ADticket.de |
# +--------------------------------------------------+
# | This library 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. |
# | |
# | In addition you are required to retain all |
# | author attributions provided in this software |
# | and attribute all modifications made by you |
# | clearly and in an appropriate way. |
# | |
# | This software 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 this software. |
# | If not, see <http://www.gnu.org/licenses/>. |
# +--------------------------------------------------+
'''Reads issues from a redmine API XML and adds
them to a github repository.
@see http://www.redmine.org/projects/redmine/wiki/Rest_api
@see http://develop.github.com/
@author Markus Tacker <[email protected]>'''
import io
import sys
from xml.dom.minidom import parse, parseString
from minidomutil import domGetText
import urllib.request
import urllib.parse
import json
from base64 import encodebytes as base64
class RedmineIssue(object):
'Represents a redmine issue'
def __repr__(self):
return "redmine issue #%s: %s" % (self.id, self.subject)
class GithubIssue(object):
'Represents a github issue'
def __init__(self):
self.state = 'open'
self.labels = []
self.milestone = None
self.assignee = None
def __repr__(self):
return "github issue #%s: %s" % (self.id, self.title)
def close(self):
self.state = 'closed'
class RedmineIssueXML(object):
'Represents the list of redmine redmineIssues in XML format'
def __init__(self):
pass
def readXML(self, xmlfile, closedStati=None, userMap=None):
'''Read the issues from the xml file
closedStati: a list of redmine status ids whose tickets are closed
userMap: a map of redmine user ids to github usernames
'''
self.dom = parse(xmlfile)
self.redmineIssues = []
self.githubIssues = []
self.closedStati = closedStati
self.userMap = userMap
for issue in self.dom.getElementsByTagName('issue'):
self.addIssue(issue)
print("Processing %d redmine issues." % len(self.redmineIssues))
def addIssue(self, issueNode):
'Creates an issue from the given node'
issue = RedmineIssue()
for key in ['project', 'tracker', 'status', 'priority', 'author', 'assigned_to', 'category', 'fixed_version', 'parent']:
nodes = issueNode.getElementsByTagName(key)
v = None
if len(nodes):
el = nodes[0]
v = {'id': int(el.getAttribute('id')), 'name': el.getAttribute('name')}
setattr(issue, key, v)
for key in ['id', 'subject', 'description', 'start_date', 'due_date', 'done_ratio', 'estimated_hours', 'created_on', 'updated_on']:
setattr(issue, key, domGetText(issueNode.getElementsByTagName(key)[0]))
self.redmineIssues.append(issue)
self.githubIssues.append(self.toGithubIssue(issue))
def toGithubIssue(self, redmineIssue):
'Convert a redmine issue to a github issue'
issue = GithubIssue()
if redmineIssue.status['id'] in self.closedStati:
issue.close()
issue.id = redmineIssue.id
issue.title = redmineIssue.subject
issue.body = redmineIssue.description
issue.body += "\n"
for (key, label) in [('author', 'Reporter'), ('assigned_to', 'Assigned to')]:
v = getattr(redmineIssue, key)
if v:
issue.body += "__%s:__ %s\n" % (label, v['name'])
for (key, label) in [('start_date', 'Begin'), ('due_date', 'End'), ('done_ratio', 'Completed')]:
v = getattr(redmineIssue, key)
if v:
issue.body += "__%s:__ %s\n" % (label, v)
assigned_to = getattr(redmineIssue, 'assigned_to')
if assigned_to:
assignedId = int(assigned_to['id'])
if assignedId in self.userMap:
issue.assignee = self.userMap[assignedId]
issue.labels.append(redmineIssue.tracker['name'])
issue.labels.append(redmineIssue.priority['name'])
if redmineIssue.category:
issue.labels.append(redmineIssue.category['name'])
if redmineIssue.fixed_version:
issue.milestone = redmineIssue.fixed_version['name']
return issue
def publishIssues(self, user, repo, authUser, authPassword):
'''Publish the issues to github
In order to keep the redmine ticket ids we create dummy tickets
and close the immediately.
user: the github username for the repository
repo: the github repository
authUser: your github username
authPassword: your github password
'''
self.baseurl = "https://api.github.com/repos/%s/%s/" % (user, repo)
self.authData = base64(bytes(('%s:%s' % (authUser, authPassword)), 'utf-8')).decode().replace('\n', '')
# Load milestone data
# Milestones are created on demand
self.milestones = {}
milestonesUrl = self.baseurl + "milestones"
for state in ['open', 'closed']:
req = urllib.request.Request(milestonesUrl + '?state=' + state)
req.add_header('Authorization', 'Basic %s' % self.authData)
res = urllib.request.urlopen(req)
for milestonedata in json.loads(res.read().decode('utf-8')):
self.milestones[milestonedata['title']] = milestonedata
# Create label data
# Labes are created upfront
labels = []
for ilabels in map(lambda p: p.labels, self.githubIssues):
for label in ilabels:
if not label in labels:
labels.append(label)
for label in labels:
self.createLabel(label)
self.createLabel("Dummy-Ticket")
currentIssue = 1
for issue in sorted(self.githubIssues, key=lambda p: int(p.id)):
# Too keep the old redmine id's we have to create dummy
# tickets
while currentIssue < int(issue.id):
if self.getIssue(currentIssue) == None:
self.createIssue("Dummy ticket %d" % currentIssue, labels=["Dummy-Ticket"])
self.closeIssue(currentIssue)
currentIssue += 1
# Check if milestone exists
milestone = None
if issue.milestone:
if not issue.milestone in self.milestones:
self.createMilestone(issue.milestone)
milestone = self.milestones[issue.milestone]['number']
# Check if issue exists
githubissuedata = self.getIssue(issue.id)
if githubissuedata:
print("Issue %d already exists." % int(issue.id))
if githubissuedata['body'] != issue.body:
self.createComment(issue.id, issue.title + "\n\n" + issue.body)
if githubissuedata['title'] != issue.title:
self.editIssue(issue.id, issue.title, body=issue.body, milestone=milestone, labels=issue.labels, assignee=issue.assignee)
else:
self.createIssue(issue.title, body=issue.body, milestone=milestone, labels=issue.labels, assignee=issue.assignee)
# Close tickets
if issue.state == 'closed':
self.closeIssue(issue.id)
def getIssue(self, id):
'Return an issue from github'
issueUrl = self.baseurl + "issues/" + str(id)
req = urllib.request.Request(issueUrl)
req.add_header('Authorization', 'Basic %s' % self.authData)
try:
res = urllib.request.urlopen(req)
return json.loads(res.read().decode('utf-8'))
except urllib.error.HTTPError as e:
if e.code == 404:
return None
else:
raise e
def createIssue(self, title, body=None, assignee=None, milestone=None, labels=None):
'Create an issue on github'
url = self.baseurl + "issues"
issuedata = {
'title': title,
}
if body:
issuedata['body'] = body
if assignee:
issuedata['assignee'] = assignee
if milestone:
issuedata['milestone'] = milestone
if labels:
issuedata['labels'] = labels
data = json.dumps(issuedata)
clen = len(data)
data = data.encode('utf-8')
req = urllib.request.Request(url)
req.add_header('Authorization', 'Basic %s' % self.authData)
req.add_header('Content-Type', 'application/json')
req.add_header('Content-Length', str(clen))
try:
res = urllib.request.urlopen(req, data)
print("Created ticket %s" % title)
return json.loads(res.read().decode('utf-8'))
except urllib.error.HTTPError as e:
print(e.msg)
print(e.fp.read().decode('utf-8'))
raise e
def editIssue(self, id, title, body=None, assignee=None, milestone=None, labels=None):
'Edit an existing issue on github'
url = self.baseurl + "issues"
issuedata = {
'title': title,
}
if body:
issuedata['body'] = body
if assignee:
issuedata['assignee'] = assignee
if milestone:
issuedata['milestone'] = milestone
if labels:
issuedata['labels'] = labels
data = json.dumps(issuedata)
clen = len(data)
data = data.encode('utf-8')
req = urllib.request.Request(url)
req.method = "PATCH"
req.add_header('Authorization', 'Basic %s' % self.authData)
req.add_header('Content-Type', 'application/json')
req.add_header('Content-Length', str(clen))
try:
res = urllib.request.urlopen(req, data)
print("Edited ticket %s" % title)
return json.loads(res.read().decode('utf-8'))
except urllib.error.HTTPError as e:
print(e.msg)
print(e.fp.read().decode('utf-8'))
raise e
'''
'''
def createComment(self, id, body):
'Add a comment to an issue'
url = self.baseurl + "issues/" + id + "/comments"
commentdata = {
'body': body,
}
data = json.dumps(commentdata)
clen = len(data)
data = data.encode('utf-8')
req = urllib.request.Request(url)
req.add_header('Authorization', 'Basic %s' % self.authData)
req.add_header('Content-Type', 'application/json')
req.add_header('Content-Length', str(clen))
try:
res = urllib.request.urlopen(req, data)
print("Added comment to ticket %s" % id)
return json.loads(res.read().decode('utf-8'))
except urllib.error.HTTPError as e:
print(e.msg)
print(e.fp.read().decode('utf-8'))
raise e
def closeIssue(self, id):
'Close an issue'
url = self.baseurl + "issues/" + str(id)
issuedata = {
'state': 'closed',
}
data = json.dumps(issuedata)
clen = len(data)
data = data.encode('utf-8')
req = MyRequest(url)
req.method = 'PATCH'
req.add_header('Authorization', 'Basic %s' % self.authData)
req.add_header('Content-Type', 'application/json')
req.add_header('Content-Length', str(clen))
try:
res = urllib.request.urlopen(req, data)
print("Closed issue %d" % int(id))
return json.loads(res.read().decode('utf-8'))
except urllib.error.HTTPError as e:
print(e.msg)
print(e.fp.read().decode('utf-8'))
raise e
def getLabel(self, label):
'Fetch a label'
issueUrl = self.baseurl + "labels/" + label
req = urllib.request.Request(issueUrl)
req.add_header('Authorization', 'Basic %s' % self.authData)
try:
res = urllib.request.urlopen(req)
return json.loads(res.read().decode('utf-8'))
except urllib.error.HTTPError as e:
if e.code == 404:
return None
else:
raise e
def createLabel(self, label):
'Create a label'
if self.getLabel(label) is not None:
return
url = self.baseurl + "labels"
labeldata = {
'name': label,
}
data = json.dumps(labeldata)
clen = len(data)
data = data.encode('utf-8')
req = urllib.request.Request(url)
req.add_header('Authorization', 'Basic %s' % self.authData)
req.add_header('Content-Type', 'application/json')
req.add_header('Content-Length', str(clen))
try:
res = urllib.request.urlopen(req, data)
print("Created label %s" % label)
return json.loads(res.read().decode('utf-8'))
except urllib.error.HTTPError as e:
print(e.msg)
print(e.fp.read().decode('utf-8'))
raise e
def createMilestone(self, title):
'Create a milestone'
data = json.dumps({'title':title})
clen = len(data)
data = data.encode('utf-8')
url = self.baseurl + "milestones"
req = urllib.request.Request(url)
req.add_header('Authorization', 'Basic %s' % self.authData)
req.add_header('Content-Type', 'application/json')
req.add_header('Content-Length', str(clen))
try:
res = urllib.request.urlopen(req, data)
print("Created milestone %s" % title)
milestone = json.loads(res.read().decode('utf-8'))
self.milestones[title] = milestone
except urllib.error.HTTPError as e:
print(e.msg)
print(e.fp.read().decode('utf-8'))
raise e
class MyRequest(urllib.request.Request):
'This class adds custom HTTP method support'
def get_method(self):
if self.method is not None:
return self.method
else:
if self.data is not None:
return "POST"
else:
return "GET"
if __name__ == '__main__':
issuesfile = 'MaintenanceServiceIssues.xml'
user = 'cyberhiker'
repo = 'RedSpartanMaintenance'
authUser = 'cyberhiker'
authPassword = 'k7%9F!8i'
rix = RedmineIssueXML()
rix.readXML(
issuesfile,
closedStati = [3,5,6], # Redmine ID of closed ticket states
userMap = { # Map redmine user id to github usernames
1: 'cyberhiker',
}
)
rix.publishIssues(user, repo, authUser, authPassword)