39
|
1 #!/usr/bin/env python
|
|
2
|
|
3 """
|
|
4 Import a Bugzilla items into a Trac database.
|
|
5
|
|
6 Requires: Trac 0.9b1 from http://trac.edgewall.com/
|
|
7 Python 2.3 from http://www.python.org/
|
|
8 MySQL >= 3.23 from http://www.mysql.org/
|
|
9
|
|
10 Thanks: Mark Rowe <mrowe@bluewire.net.nz>
|
|
11 for original TracDatabase class
|
|
12
|
|
13 Copyright 2004, Dmitry Yusupov <dmitry_yus@yahoo.com>
|
|
14
|
|
15 Many enhancements, Bill Soudan <bill@soudan.net>
|
|
16 Other enhancements, Florent Guillaume <fg@nuxeo.com>
|
|
17 Reworked, Jeroen Ruigrok van der Werven <asmodai@tendra.org>
|
|
18
|
|
19 $Id$
|
|
20 """
|
|
21
|
|
22 import re
|
|
23
|
|
24 ###
|
|
25 ### Conversion Settings -- edit these before running if desired
|
|
26 ###
|
|
27
|
|
28 # Bugzilla version. You can find this in Bugzilla's globals.pl file.
|
|
29 #
|
|
30 # Currently, the following bugzilla versions are known to work:
|
|
31 # 2.11 (2110), 2.16.5 (2165), 2.18.3 (2183), 2.19.1 (2191)
|
|
32 #
|
|
33 # If you run this script on a version not listed here and it is successful,
|
|
34 # please report it to the Trac mailing list and drop a note to
|
|
35 # asmodai@tendra.org so we can update the list.
|
|
36 BZ_VERSION = 2180
|
|
37
|
|
38 # MySQL connection parameters for the Bugzilla database. These can also
|
|
39 # be specified on the command line.
|
|
40 BZ_DB = ""
|
|
41 BZ_HOST = ""
|
|
42 BZ_USER = ""
|
|
43 BZ_PASSWORD = ""
|
|
44
|
|
45 # Path to the Trac environment.
|
|
46 TRAC_ENV = "/usr/local/trac"
|
|
47
|
|
48 # If true, all existing Trac tickets and attachments will be removed
|
|
49 # prior to import.
|
|
50 TRAC_CLEAN = True
|
|
51
|
|
52 # Enclose imported ticket description and comments in a {{{ }}}
|
|
53 # preformat block? This formats the text in a fixed-point font.
|
|
54 PREFORMAT_COMMENTS = False
|
|
55
|
|
56 # Replace bug numbers in comments with #xyz
|
|
57 REPLACE_BUG_NO = False
|
|
58
|
|
59 # Severities
|
|
60 SEVERITIES = [
|
|
61 ("blocker", "1"),
|
|
62 ("critical", "2"),
|
|
63 ("major", "3"),
|
|
64 ("normal", "4"),
|
|
65 ("minor", "5"),
|
|
66 ("trivial", "6")
|
|
67 ]
|
|
68
|
|
69 # Priorities
|
|
70 # If using the default Bugzilla priorities of P1 - P5, do not change anything
|
|
71 # here.
|
|
72 # If you have other priorities defined please change the P1 - P5 mapping to
|
|
73 # the order you want. You can also collapse multiple priorities on bugzilla's
|
|
74 # side into the same priority on Trac's side, simply adjust PRIORITIES_MAP.
|
|
75 PRIORITIES = [
|
|
76 ("highest", "1"),
|
|
77 ("high", "2"),
|
|
78 ("normal", "3"),
|
|
79 ("low", "4"),
|
|
80 ("lowest", "5")
|
|
81 ]
|
|
82
|
|
83 # Bugzilla: Trac
|
|
84 # NOTE: Use lowercase.
|
|
85 PRIORITIES_MAP = {
|
|
86 "p1": "highest",
|
|
87 "p2": "high",
|
|
88 "p3": "normal",
|
|
89 "p4": "low",
|
|
90 "p5": "lowest"
|
|
91 }
|
|
92
|
|
93 # By default, all bugs are imported from Bugzilla. If you add a list
|
|
94 # of products here, only bugs from those products will be imported.
|
|
95 PRODUCTS = []
|
|
96 # These Bugzilla products will be ignored during import.
|
|
97 IGNORE_PRODUCTS = []
|
|
98
|
|
99 # These milestones are ignored
|
|
100 IGNORE_MILESTONES = ["---"]
|
|
101
|
|
102 # These logins are converted to these user ids
|
|
103 LOGIN_MAP = {
|
|
104 #'some.user@example.com': 'someuser',
|
|
105 }
|
|
106
|
|
107 # These emails are removed from CC list
|
|
108 IGNORE_CC = [
|
|
109 #'loser@example.com',
|
|
110 ]
|
|
111
|
|
112 # The 'component' field in Trac can come either from the Product or
|
|
113 # or from the Component field of Bugzilla. COMPONENTS_FROM_PRODUCTS
|
|
114 # switches the behavior.
|
|
115 # If COMPONENTS_FROM_PRODUCTS is True:
|
|
116 # - Bugzilla Product -> Trac Component
|
|
117 # - Bugzilla Component -> Trac Keyword
|
|
118 # IF COMPONENTS_FROM_PRODUCTS is False:
|
|
119 # - Bugzilla Product -> Trac Keyword
|
|
120 # - Bugzilla Component -> Trac Component
|
|
121 COMPONENTS_FROM_PRODUCTS = False
|
|
122
|
|
123 # If COMPONENTS_FROM_PRODUCTS is True, the default owner for each
|
|
124 # Trac component is inferred from a default Bugzilla component.
|
|
125 DEFAULT_COMPONENTS = ["default", "misc", "main"]
|
|
126
|
|
127 # This mapping can assign keywords in the ticket entry to represent
|
|
128 # products or components (depending on COMPONENTS_FROM_PRODUCTS).
|
|
129 # The keyword will be ignored if empty.
|
|
130 KEYWORDS_MAPPING = {
|
|
131 #'Bugzilla_product_or_component': 'Keyword',
|
|
132 "default": "",
|
|
133 "misc": "",
|
|
134 }
|
|
135
|
|
136 # If this is True, products or components are all set as keywords
|
|
137 # even if not mentionned in KEYWORDS_MAPPING.
|
|
138 MAP_ALL_KEYWORDS = True
|
|
139
|
|
140
|
|
141 # Bug comments that should not be imported. Each entry in list should
|
|
142 # be a regular expression.
|
|
143 IGNORE_COMMENTS = [
|
|
144 "^Created an attachment \(id="
|
|
145 ]
|
|
146
|
|
147 ###########################################################################
|
|
148 ### You probably don't need to change any configuration past this line. ###
|
|
149 ###########################################################################
|
|
150
|
|
151 # Bugzilla status to Trac status translation map.
|
|
152 #
|
|
153 # NOTE: bug activity is translated as well, which may cause bug
|
|
154 # activity to be deleted (e.g. resolved -> closed in Bugzilla
|
|
155 # would translate into closed -> closed in Trac, so we just ignore the
|
|
156 # change).
|
|
157 #
|
|
158 # There is some special magic for open in the code: if there is no
|
|
159 # Bugzilla owner, open is mapped to 'new' instead.
|
|
160 STATUS_TRANSLATE = {
|
|
161 "unconfirmed": "new",
|
|
162 "open": "assigned",
|
|
163 "resolved": "closed",
|
|
164 "verified": "closed",
|
|
165 "released": "closed"
|
|
166 }
|
|
167
|
|
168 # Translate Bugzilla statuses into Trac keywords. This provides a way
|
|
169 # to retain the Bugzilla statuses in Trac. e.g. when a bug is marked
|
|
170 # 'verified' in Bugzilla it will be assigned a VERIFIED keyword.
|
|
171 STATUS_KEYWORDS = {
|
|
172 "verified": "VERIFIED",
|
|
173 "released": "RELEASED"
|
|
174 }
|
|
175
|
|
176 # Some fields in Bugzilla do not have equivalents in Trac. Changes in
|
|
177 # fields listed here will not be imported into the ticket change history,
|
|
178 # otherwise you'd see changes for fields that don't exist in Trac.
|
|
179 IGNORED_ACTIVITY_FIELDS = ["everconfirmed"]
|
|
180
|
|
181 # Regular expression and its replacement
|
|
182 BUG_NO_RE = re.compile(r"\b(bug #?)([0-9])")
|
|
183 BUG_NO_REPL = r"#\2"
|
|
184
|
|
185 ###
|
|
186 ### Script begins here
|
|
187 ###
|
|
188
|
|
189 import os
|
|
190 import sys
|
|
191 import string
|
|
192 import StringIO
|
|
193
|
|
194 import MySQLdb
|
|
195 import MySQLdb.cursors
|
|
196 try:
|
|
197 from trac.env import Environment
|
|
198 except:
|
|
199 from trac.Environment import Environment
|
|
200 from trac.attachment import Attachment
|
|
201
|
|
202 if not hasattr(sys, 'setdefaultencoding'):
|
|
203 reload(sys)
|
|
204
|
|
205 sys.setdefaultencoding('latin1')
|
|
206
|
|
207 # simulated Attachment class for trac.add
|
|
208 #class Attachment:
|
|
209 # def __init__(self, name, data):
|
|
210 # self.filename = name
|
|
211 # self.file = StringIO.StringIO(data.tostring())
|
|
212
|
|
213 # simple field translation mapping. if string not in
|
|
214 # mapping, just return string, otherwise return value
|
|
215 class FieldTranslator(dict):
|
|
216 def __getitem__(self, item):
|
|
217 if not dict.has_key(self, item):
|
|
218 return item
|
|
219
|
|
220 return dict.__getitem__(self, item)
|
|
221
|
|
222 statusXlator = FieldTranslator(STATUS_TRANSLATE)
|
|
223
|
|
224 class TracDatabase(object):
|
|
225 def __init__(self, path):
|
|
226 self.env = Environment(path)
|
|
227 self._db = self.env.get_db_cnx()
|
|
228 self._db.autocommit = False
|
|
229 self.loginNameCache = {}
|
|
230 self.fieldNameCache = {}
|
|
231
|
|
232 def db(self):
|
|
233 return self._db
|
|
234
|
|
235 def hasTickets(self):
|
|
236 c = self.db().cursor()
|
|
237 c.execute("SELECT count(*) FROM Ticket")
|
|
238 return int(c.fetchall()[0][0]) > 0
|
|
239
|
|
240 def assertNoTickets(self):
|
|
241 if self.hasTickets():
|
|
242 raise Exception("Will not modify database with existing tickets!")
|
|
243
|
|
244 def setSeverityList(self, s):
|
|
245 """Remove all severities, set them to `s`"""
|
|
246 self.assertNoTickets()
|
|
247
|
|
248 c = self.db().cursor()
|
|
249 c.execute("DELETE FROM enum WHERE type='severity'")
|
|
250 for value, i in s:
|
|
251 print " inserting severity '%s' - '%s'" % (value, i)
|
|
252 c.execute("""INSERT INTO enum (type, name, value)
|
|
253 VALUES (%s, %s, %s)""",
|
|
254 ("severity", value.encode('utf-8'), i))
|
|
255 self.db().commit()
|
|
256
|
|
257 def setPriorityList(self, s):
|
|
258 """Remove all priorities, set them to `s`"""
|
|
259 self.assertNoTickets()
|
|
260
|
|
261 c = self.db().cursor()
|
|
262 c.execute("DELETE FROM enum WHERE type='priority'")
|
|
263 for value, i in s:
|
|
264 print " inserting priority '%s' - '%s'" % (value, i)
|
|
265 c.execute("""INSERT INTO enum (type, name, value)
|
|
266 VALUES (%s, %s, %s)""",
|
|
267 ("priority", value.encode('utf-8'), i))
|
|
268 self.db().commit()
|
|
269
|
|
270
|
|
271 def setComponentList(self, l, key):
|
|
272 """Remove all components, set them to `l`"""
|
|
273 self.assertNoTickets()
|
|
274
|
|
275 c = self.db().cursor()
|
|
276 c.execute("DELETE FROM component")
|
|
277 for comp in l:
|
|
278 print " inserting component '%s', owner '%s'" % \
|
|
279 (comp[key], comp['owner'])
|
|
280 c.execute("INSERT INTO component (name, owner) VALUES (%s, %s)",
|
|
281 (comp[key].encode('utf-8'),
|
|
282 comp['owner'].encode('utf-8')))
|
|
283 self.db().commit()
|
|
284
|
|
285 def setVersionList(self, v, key):
|
|
286 """Remove all versions, set them to `v`"""
|
|
287 self.assertNoTickets()
|
|
288
|
|
289 c = self.db().cursor()
|
|
290 c.execute("DELETE FROM version")
|
|
291 for vers in v:
|
|
292 print " inserting version '%s'" % (vers[key])
|
|
293 c.execute("INSERT INTO version (name) VALUES (%s)",
|
|
294 (vers[key].encode('utf-8'),))
|
|
295 self.db().commit()
|
|
296
|
|
297 def setMilestoneList(self, m, key):
|
|
298 """Remove all milestones, set them to `m`"""
|
|
299 self.assertNoTickets()
|
|
300
|
|
301 c = self.db().cursor()
|
|
302 c.execute("DELETE FROM milestone")
|
|
303 for ms in m:
|
|
304 milestone = ms[key]
|
|
305 print " inserting milestone '%s'" % (milestone)
|
|
306 c.execute("INSERT INTO milestone (name) VALUES (%s)",
|
|
307 (milestone.encode('utf-8'),))
|
|
308 self.db().commit()
|
|
309
|
|
310 def addTicket(self, id, time, changetime, component, severity, priority,
|
|
311 owner, reporter, cc, version, milestone, status, resolution,
|
|
312 summary, description, keywords):
|
|
313 c = self.db().cursor()
|
|
314
|
|
315 desc = description.encode('utf-8')
|
|
316 type = "defect"
|
|
317
|
|
318 if severity.lower() == "enhancement":
|
|
319 severity = "minor"
|
|
320 type = "enhancement"
|
|
321
|
|
322 if PREFORMAT_COMMENTS:
|
|
323 desc = '{{{\n%s\n}}}' % desc
|
|
324
|
|
325 if REPLACE_BUG_NO:
|
|
326 if BUG_NO_RE.search(desc):
|
|
327 desc = re.sub(BUG_NO_RE, BUG_NO_REPL, desc)
|
|
328
|
|
329 if PRIORITIES_MAP.has_key(priority):
|
|
330 priority = PRIORITIES_MAP[priority]
|
|
331
|
|
332 print " inserting ticket %s -- %s" % (id, summary)
|
|
333
|
|
334 c.execute("""INSERT INTO ticket (id, type, time, changetime, component,
|
|
335 severity, priority, owner, reporter,
|
|
336 cc, version, milestone, status,
|
|
337 resolution, summary, description,
|
|
338 keywords)
|
|
339 VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s,
|
|
340 %s, %s, %s, %s, %s, %s, %s, %s)""",
|
|
341 (id, type.encode('utf-8'), time.strftime('%s'),
|
|
342 changetime.strftime('%s'), component.encode('utf-8'),
|
|
343 severity.encode('utf-8'), priority.encode('utf-8'), owner,
|
|
344 reporter, cc, version, milestone.encode('utf-8'),
|
|
345 status.lower(), resolution, summary.encode('utf-8'), desc,
|
|
346 keywords))
|
|
347
|
|
348 self.db().commit()
|
|
349 return self.db().get_last_id(c, 'ticket')
|
|
350
|
|
351 def addTicketComment(self, ticket, time, author, value):
|
|
352 comment = value.encode('utf-8')
|
|
353
|
|
354 if PREFORMAT_COMMENTS:
|
|
355 comment = '{{{\n%s\n}}}' % comment
|
|
356
|
|
357 if REPLACE_BUG_NO:
|
|
358 if BUG_NO_RE.search(comment):
|
|
359 comment = re.sub(BUG_NO_RE, BUG_NO_REPL, comment)
|
|
360
|
|
361 c = self.db().cursor()
|
|
362 c.execute("""INSERT INTO ticket_change (ticket, time, author, field,
|
|
363 oldvalue, newvalue)
|
|
364 VALUES (%s, %s, %s, %s, %s, %s)""",
|
|
365 (ticket, time.strftime('%s'), author, 'comment', '', comment))
|
|
366 self.db().commit()
|
|
367
|
|
368 def addTicketChange(self, ticket, time, author, field, oldvalue, newvalue):
|
|
369 c = self.db().cursor()
|
|
370
|
|
371 if field == "owner":
|
|
372 if LOGIN_MAP.has_key(oldvalue):
|
|
373 oldvalue = LOGIN_MAP[oldvalue]
|
|
374 if LOGIN_MAP.has_key(newvalue):
|
|
375 newvalue = LOGIN_MAP[newvalue]
|
|
376
|
|
377 if field == "priority":
|
|
378 if PRIORITIES_MAP.has_key(oldvalue.lower()):
|
|
379 oldvalue = PRIORITIES_MAP[oldvalue.lower()]
|
|
380 if PRIORITIES_MAP.has_key(newvalue.lower()):
|
|
381 newvalue = PRIORITIES_MAP[newvalue.lower()]
|
|
382
|
|
383 # Doesn't make sense if we go from highest -> highest, for example.
|
|
384 if oldvalue == newvalue:
|
|
385 return
|
|
386
|
|
387 c.execute("""INSERT INTO ticket_change (ticket, time, author, field,
|
|
388 oldvalue, newvalue)
|
|
389 VALUES (%s, %s, %s, %s, %s, %s)""",
|
|
390 (ticket, time.strftime('%s'), author, field,
|
|
391 oldvalue.encode('utf-8'), newvalue.encode('utf-8')))
|
|
392 self.db().commit()
|
|
393
|
|
394 def addAttachment(self, author, a):
|
|
395 description = a['description'].encode('utf-8')
|
|
396 id = a['bug_id']
|
|
397 filename = a['filename'].encode('utf-8')
|
|
398 filedata = StringIO.StringIO(a['thedata'].tostring())
|
|
399 filesize = len(filedata.getvalue())
|
|
400 time = a['creation_ts']
|
|
401 print " ->inserting attachment '%s' for ticket %s -- %s" % \
|
|
402 (filename, id, description)
|
|
403
|
|
404 attachment = Attachment(self.env, 'ticket', id)
|
|
405 attachment.author = author
|
|
406 attachment.description = description
|
|
407 attachment.insert(filename, filedata, filesize, time.strftime('%s'))
|
|
408 del attachment
|
|
409
|
|
410 def getLoginName(self, cursor, userid):
|
|
411 if userid not in self.loginNameCache:
|
|
412 cursor.execute("SELECT * FROM profiles WHERE userid = %s", (userid))
|
|
413 loginName = cursor.fetchall()
|
|
414
|
|
415 if loginName:
|
|
416 loginName = loginName[0]['login_name']
|
|
417 else:
|
|
418 print """WARNING: unknown bugzilla userid %d, recording as
|
|
419 anonymous""" % (userid)
|
|
420 loginName = "anonymous"
|
|
421
|
|
422 loginName = LOGIN_MAP.get(loginName, loginName)
|
|
423
|
|
424 self.loginNameCache[userid] = loginName
|
|
425
|
|
426 return self.loginNameCache[userid]
|
|
427
|
|
428 def getFieldName(self, cursor, fieldid):
|
|
429 if fieldid not in self.fieldNameCache:
|
|
430 cursor.execute("SELECT * FROM fielddefs WHERE fieldid = %s",
|
|
431 (fieldid))
|
|
432 fieldName = cursor.fetchall()
|
|
433
|
|
434 if fieldName:
|
|
435 fieldName = fieldName[0]['name'].lower()
|
|
436 else:
|
|
437 print "WARNING: unknown bugzilla fieldid %d, \
|
|
438 recording as unknown" % (userid)
|
|
439 fieldName = "unknown"
|
|
440
|
|
441 self.fieldNameCache[fieldid] = fieldName
|
|
442
|
|
443 return self.fieldNameCache[fieldid]
|
|
444
|
|
445 def makeWhereClause(fieldName, values, negative=False):
|
|
446 if not values:
|
|
447 return ''
|
|
448 if negative:
|
|
449 connector, op = ' AND ', '!='
|
|
450 else:
|
|
451 connector, op = ' OR ', '='
|
|
452 clause = connector.join(["%s %s '%s'" % (fieldName, op, value) for value in values])
|
|
453 return ' ' + clause
|
|
454
|
|
455 def convert(_db, _host, _user, _password, _env, _force):
|
|
456 activityFields = FieldTranslator()
|
|
457
|
|
458 # account for older versions of bugzilla
|
|
459 print "Using Bugzilla v%s schema." % BZ_VERSION
|
|
460 if BZ_VERSION == 2110:
|
|
461 activityFields['removed'] = "oldvalue"
|
|
462 activityFields['added'] = "newvalue"
|
|
463
|
|
464 # init Bugzilla environment
|
|
465 print "Bugzilla MySQL('%s':'%s':'%s':'%s'): connecting..." % \
|
|
466 (_db, _host, _user, ("*" * len(_password)))
|
|
467 mysql_con = MySQLdb.connect(host=_host,
|
|
468 user=_user, passwd=_password, db=_db, compress=1,
|
|
469 cursorclass=MySQLdb.cursors.DictCursor)
|
|
470 mysql_cur = mysql_con.cursor()
|
|
471
|
|
472 # init Trac environment
|
|
473 print "Trac SQLite('%s'): connecting..." % (_env)
|
|
474 trac = TracDatabase(_env)
|
|
475
|
|
476 # force mode...
|
|
477 if _force == 1:
|
|
478 print "\nCleaning all tickets..."
|
|
479 c = trac.db().cursor()
|
|
480 c.execute("DELETE FROM ticket_change")
|
|
481 trac.db().commit()
|
|
482
|
|
483 c.execute("DELETE FROM ticket")
|
|
484 trac.db().commit()
|
|
485
|
|
486 c.execute("DELETE FROM attachment")
|
|
487 attachments_dir = os.path.join(os.path.normpath(trac.env.path),
|
|
488 "attachments")
|
|
489 # Straight from the Python documentation.
|
|
490 for root, dirs, files in os.walk(attachments_dir, topdown=False):
|
|
491 for name in files:
|
|
492 os.remove(os.path.join(root, name))
|
|
493 for name in dirs:
|
|
494 os.rmdir(os.path.join(root, name))
|
|
495 if not os.stat(attachments_dir):
|
|
496 os.mkdir(attachments_dir)
|
|
497 trac.db().commit()
|
|
498 print "All tickets cleaned..."
|
|
499
|
|
500
|
|
501 print "\n0. Filtering products..."
|
|
502 mysql_cur.execute("SELECT name FROM products")
|
|
503 products = []
|
|
504 for line in mysql_cur.fetchall():
|
|
505 product = line['name']
|
|
506 if PRODUCTS and product not in PRODUCTS:
|
|
507 continue
|
|
508 if product in IGNORE_PRODUCTS:
|
|
509 continue
|
|
510 products.append(product)
|
|
511 PRODUCTS[:] = products
|
|
512 print " Using products", " ".join(PRODUCTS)
|
|
513
|
|
514 print "\n1. Import severities..."
|
|
515 trac.setSeverityList(SEVERITIES)
|
|
516
|
|
517 print "\n2. Import components..."
|
|
518 if not COMPONENTS_FROM_PRODUCTS:
|
|
519 if BZ_VERSION >= 2180:
|
|
520 sql = """SELECT DISTINCT c.name AS name, c.initialowner AS owner
|
|
521 FROM components AS c, products AS p
|
|
522 WHERE c.product_id = p.id AND"""
|
|
523 sql += makeWhereClause('p.name', PRODUCTS)
|
|
524 else:
|
|
525 sql = "SELECT value AS name, initialowner AS owner FROM components"
|
|
526 sql += " WHERE" + makeWhereClause('program', PRODUCTS)
|
|
527 mysql_cur.execute(sql)
|
|
528 components = mysql_cur.fetchall()
|
|
529 for component in components:
|
|
530 component['owner'] = trac.getLoginName(mysql_cur,
|
|
531 component['owner'])
|
|
532 trac.setComponentList(components, 'name')
|
|
533 else:
|
|
534 sql = """SELECT program AS product, value AS comp, initialowner AS owner
|
|
535 FROM components"""
|
|
536 sql += " WHERE" + makeWhereClause('program', PRODUCTS)
|
|
537 mysql_cur.execute(sql)
|
|
538 lines = mysql_cur.fetchall()
|
|
539 all_components = {} # product -> components
|
|
540 all_owners = {} # product, component -> owner
|
|
541 for line in lines:
|
|
542 product = line['product']
|
|
543 comp = line['comp']
|
|
544 owner = line['owner']
|
|
545 all_components.setdefault(product, []).append(comp)
|
|
546 all_owners[(product, comp)] = owner
|
|
547 component_list = []
|
|
548 for product, components in all_components.items():
|
|
549 # find best default owner
|
|
550 default = None
|
|
551 for comp in DEFAULT_COMPONENTS:
|
|
552 if comp in components:
|
|
553 default = comp
|
|
554 break
|
|
555 if default is None:
|
|
556 default = components[0]
|
|
557 owner = all_owners[(product, default)]
|
|
558 owner_name = trac.getLoginName(mysql_cur, owner)
|
|
559 component_list.append({'product': product, 'owner': owner_name})
|
|
560 trac.setComponentList(component_list, 'product')
|
|
561
|
|
562 print "\n3. Import priorities..."
|
|
563 trac.setPriorityList(PRIORITIES)
|
|
564
|
|
565 print "\n4. Import versions..."
|
|
566 if BZ_VERSION >= 2180:
|
|
567 sql = """SELECT DISTINCTROW versions.value AS value
|
|
568 FROM products, versions"""
|
|
569 sql += " WHERE" + makeWhereClause('products.name', PRODUCTS)
|
|
570 else:
|
|
571 sql = "SELECT DISTINCTROW value FROM versions"
|
|
572 sql += " WHERE" + makeWhereClause('program', PRODUCTS)
|
|
573 mysql_cur.execute(sql)
|
|
574 versions = mysql_cur.fetchall()
|
|
575 trac.setVersionList(versions, 'value')
|
|
576
|
|
577 print "\n5. Import milestones..."
|
|
578 sql = "SELECT DISTINCT value FROM milestones"
|
|
579 sql += " WHERE" + makeWhereClause('value', IGNORE_MILESTONES, negative=True)
|
|
580 mysql_cur.execute(sql)
|
|
581 milestones = mysql_cur.fetchall()
|
|
582 trac.setMilestoneList(milestones, 'value')
|
|
583
|
|
584 print "\n6. Retrieving bugs..."
|
|
585 sql = """SELECT DISTINCT b.*, c.name AS component, p.name AS product
|
|
586 FROM bugs AS b, components AS c, products AS p """
|
|
587 sql += " WHERE (" + makeWhereClause('p.name', PRODUCTS)
|
|
588 sql += ") AND b.product_id = p.id"
|
|
589 sql += " AND b.component_id = c.id"
|
|
590 sql += " ORDER BY b.bug_id"
|
|
591 mysql_cur.execute(sql)
|
|
592 bugs = mysql_cur.fetchall()
|
|
593
|
|
594
|
|
595 print "\n7. Import bugs and bug activity..."
|
|
596 for bug in bugs:
|
|
597 bugid = bug['bug_id']
|
|
598
|
|
599 ticket = {}
|
|
600 keywords = []
|
|
601 ticket['id'] = bugid
|
|
602 ticket['time'] = bug['creation_ts']
|
|
603 ticket['changetime'] = bug['delta_ts']
|
|
604 if COMPONENTS_FROM_PRODUCTS:
|
|
605 ticket['component'] = bug['product']
|
|
606 else:
|
|
607 ticket['component'] = bug['component']
|
|
608 ticket['severity'] = bug['bug_severity']
|
|
609 ticket['priority'] = bug['priority'].lower()
|
|
610
|
|
611 ticket['owner'] = trac.getLoginName(mysql_cur, bug['assigned_to'])
|
|
612 ticket['reporter'] = trac.getLoginName(mysql_cur, bug['reporter'])
|
|
613
|
|
614 mysql_cur.execute("SELECT * FROM cc WHERE bug_id = %s", bugid)
|
|
615 cc_records = mysql_cur.fetchall()
|
|
616 cc_list = []
|
|
617 for cc in cc_records:
|
|
618 cc_list.append(trac.getLoginName(mysql_cur, cc['who']))
|
|
619 cc_list = [cc for cc in cc_list if '@' in cc and cc not in IGNORE_CC]
|
|
620 ticket['cc'] = string.join(cc_list, ', ')
|
|
621
|
|
622 ticket['version'] = bug['version']
|
|
623
|
|
624 target_milestone = bug['target_milestone']
|
|
625 if target_milestone in IGNORE_MILESTONES:
|
|
626 target_milestone = ''
|
|
627 ticket['milestone'] = target_milestone
|
|
628
|
|
629 bug_status = bug['bug_status'].lower()
|
|
630 ticket['status'] = statusXlator[bug_status]
|
|
631 ticket['resolution'] = bug['resolution'].lower()
|
|
632
|
|
633 # a bit of extra work to do open tickets
|
|
634 if bug_status == 'open':
|
|
635 if owner != '':
|
|
636 ticket['status'] = 'assigned'
|
|
637 else:
|
|
638 ticket['status'] = 'new'
|
|
639
|
|
640 ticket['summary'] = bug['short_desc']
|
|
641
|
|
642 mysql_cur.execute("SELECT * FROM longdescs WHERE bug_id = %s" % bugid)
|
|
643 longdescs = list(mysql_cur.fetchall())
|
|
644
|
|
645 # check for empty 'longdescs[0]' field...
|
|
646 if len(longdescs) == 0:
|
|
647 ticket['description'] = ''
|
|
648 else:
|
|
649 ticket['description'] = longdescs[0]['thetext']
|
|
650 del longdescs[0]
|
|
651
|
|
652 for desc in longdescs:
|
|
653 ignore = False
|
|
654 for comment in IGNORE_COMMENTS:
|
|
655 if re.match(comment, desc['thetext']):
|
|
656 ignore = True
|
|
657
|
|
658 if ignore:
|
|
659 continue
|
|
660
|
|
661 trac.addTicketComment(ticket=bugid,
|
|
662 time = desc['bug_when'],
|
|
663 author=trac.getLoginName(mysql_cur, desc['who']),
|
|
664 value = desc['thetext'])
|
|
665
|
|
666 mysql_cur.execute("""SELECT * FROM bugs_activity WHERE bug_id = %s
|
|
667 ORDER BY bug_when""" % bugid)
|
|
668 bugs_activity = mysql_cur.fetchall()
|
|
669 resolution = ''
|
|
670 ticketChanges = []
|
|
671 keywords = []
|
|
672 for activity in bugs_activity:
|
|
673 field_name = trac.getFieldName(mysql_cur, activity['fieldid']).lower()
|
|
674
|
|
675 removed = activity[activityFields['removed']]
|
|
676 added = activity[activityFields['added']]
|
|
677
|
|
678 # statuses and resolutions are in lowercase in trac
|
|
679 if field_name == "resolution" or field_name == "bug_status":
|
|
680 removed = removed.lower()
|
|
681 added = added.lower()
|
|
682
|
|
683 # remember most recent resolution, we need this later
|
|
684 if field_name == "resolution":
|
|
685 resolution = added.lower()
|
|
686
|
|
687 add_keywords = []
|
|
688 remove_keywords = []
|
|
689
|
|
690 # convert bugzilla field names...
|
|
691 if field_name == "bug_severity":
|
|
692 field_name = "severity"
|
|
693 elif field_name == "assigned_to":
|
|
694 field_name = "owner"
|
|
695 elif field_name == "bug_status":
|
|
696 field_name = "status"
|
|
697 if removed in STATUS_KEYWORDS:
|
|
698 remove_keywords.append(STATUS_KEYWORDS[removed])
|
|
699 if added in STATUS_KEYWORDS:
|
|
700 add_keywords.append(STATUS_KEYWORDS[added])
|
|
701 added = statusXlator[added]
|
|
702 removed = statusXlator[removed]
|
|
703 elif field_name == "short_desc":
|
|
704 field_name = "summary"
|
|
705 elif field_name == "product" and COMPONENTS_FROM_PRODUCTS:
|
|
706 field_name = "component"
|
|
707 elif ((field_name == "product" and not COMPONENTS_FROM_PRODUCTS) or
|
|
708 (field_name == "component" and COMPONENTS_FROM_PRODUCTS)):
|
|
709 if MAP_ALL_KEYWORDS or removed in KEYWORDS_MAPPING:
|
|
710 kw = KEYWORDS_MAPPING.get(removed, removed)
|
|
711 if kw:
|
|
712 remove_keywords.append(kw)
|
|
713 if MAP_ALL_KEYWORDS or added in KEYWORDS_MAPPING:
|
|
714 kw = KEYWORDS_MAPPING.get(added, added)
|
|
715 if kw:
|
|
716 add_keywords.append(kw)
|
|
717 if field_name == "component":
|
|
718 # just keep the keyword change
|
|
719 added = removed = ""
|
|
720 elif field_name == "target_milestone":
|
|
721 field_name = "milestone"
|
|
722 if added in IGNORE_MILESTONES:
|
|
723 added = ""
|
|
724 if removed in IGNORE_MILESTONES:
|
|
725 removed = ""
|
|
726
|
|
727 ticketChange = {}
|
|
728 ticketChange['ticket'] = bugid
|
|
729 ticketChange['time'] = activity['bug_when']
|
|
730 ticketChange['author'] = trac.getLoginName(mysql_cur,
|
|
731 activity['who'])
|
|
732 ticketChange['field'] = field_name
|
|
733 ticketChange['oldvalue'] = removed
|
|
734 ticketChange['newvalue'] = added
|
|
735
|
|
736 if add_keywords or remove_keywords:
|
|
737 # ensure removed ones are in old
|
|
738 old_keywords = keywords + [kw for kw in remove_keywords if kw
|
|
739 not in keywords]
|
|
740 # remove from new
|
|
741 keywords = [kw for kw in keywords if kw not in remove_keywords]
|
|
742 # add to new
|
|
743 keywords += [kw for kw in add_keywords if kw not in keywords]
|
|
744 if old_keywords != keywords:
|
|
745 ticketChangeKw = ticketChange.copy()
|
|
746 ticketChangeKw['field'] = "keywords"
|
|
747 ticketChangeKw['oldvalue'] = ' '.join(old_keywords)
|
|
748 ticketChangeKw['newvalue'] = ' '.join(keywords)
|
|
749 ticketChanges.append(ticketChangeKw)
|
|
750
|
|
751 if field_name in IGNORED_ACTIVITY_FIELDS:
|
|
752 continue
|
|
753
|
|
754 # Skip changes that have no effect (think translation!).
|
|
755 if added == removed:
|
|
756 continue
|
|
757
|
|
758 # Bugzilla splits large summary changes into two records.
|
|
759 for oldChange in ticketChanges:
|
|
760 if (field_name == "summary"
|
|
761 and oldChange['field'] == ticketChange['field']
|
|
762 and oldChange['time'] == ticketChange['time']
|
|
763 and oldChange['author'] == ticketChange['author']):
|
|
764 oldChange['oldvalue'] += " " + ticketChange['oldvalue']
|
|
765 oldChange['newvalue'] += " " + ticketChange['newvalue']
|
|
766 break
|
|
767 # cc sometime appear in different activities with same time
|
|
768 if (field_name == "cc" \
|
|
769 and oldChange['time'] == ticketChange['time']):
|
|
770 oldChange['newvalue'] += ", " + ticketChange['newvalue']
|
|
771 break
|
|
772 else:
|
|
773 ticketChanges.append (ticketChange)
|
|
774
|
|
775 for ticketChange in ticketChanges:
|
|
776 trac.addTicketChange (**ticketChange)
|
|
777
|
|
778 # For some reason, bugzilla v2.11 seems to clear the resolution
|
|
779 # when you mark a bug as closed. Let's remember it and restore
|
|
780 # it if the ticket is closed but there's no resolution.
|
|
781 if not ticket['resolution'] and ticket['status'] == "closed":
|
|
782 ticket['resolution'] = resolution
|
|
783
|
|
784 bug_status = bug['bug_status']
|
|
785 if bug_status in STATUS_KEYWORDS:
|
|
786 kw = STATUS_KEYWORDS[bug_status]
|
|
787 if kw not in keywords:
|
|
788 keywords.append(kw)
|
|
789
|
|
790 product = bug['product']
|
|
791 if product in KEYWORDS_MAPPING and not COMPONENTS_FROM_PRODUCTS:
|
|
792 kw = KEYWORDS_MAPPING.get(product, product)
|
|
793 if kw and kw not in keywords:
|
|
794 keywords.append(kw)
|
|
795
|
|
796 component = bug['component']
|
|
797 if (COMPONENTS_FROM_PRODUCTS and \
|
|
798 (MAP_ALL_KEYWORDS or component in KEYWORDS_MAPPING)):
|
|
799 kw = KEYWORDS_MAPPING.get(component, component)
|
|
800 if kw and kw not in keywords:
|
|
801 keywords.append(kw)
|
|
802
|
|
803 ticket['keywords'] = string.join(keywords)
|
|
804 ticketid = trac.addTicket(**ticket)
|
|
805
|
|
806 mysql_cur.execute("SELECT * FROM attachments WHERE bug_id = %s" % bugid)
|
|
807 attachments = mysql_cur.fetchall()
|
|
808 for a in attachments:
|
|
809 author = trac.getLoginName(mysql_cur, a['submitter_id'])
|
|
810 trac.addAttachment(author, a)
|
|
811
|
|
812 print "\n8. Importing users and passwords..."
|
|
813 if BZ_VERSION >= 2180:
|
|
814 mysql_cur.execute("SELECT login_name, cryptpassword FROM profiles")
|
|
815 users = mysql_cur.fetchall()
|
|
816 htpasswd = file("htpasswd", 'w')
|
|
817 for user in users:
|
|
818 if LOGIN_MAP.has_key(user['login_name']):
|
|
819 login = LOGIN_MAP[user['login_name']]
|
|
820 else:
|
|
821 login = user['login_name']
|
|
822 htpasswd.write(login + ":" + user['cryptpassword'] + "\n")
|
|
823
|
|
824 htpasswd.close()
|
|
825 print " Bugzilla users converted to htpasswd format, see 'htpasswd'."
|
|
826
|
|
827 print "\nAll tickets converted."
|
|
828
|
|
829 def log(msg):
|
|
830 print "DEBUG: %s" % (msg)
|
|
831
|
|
832 def usage():
|
|
833 print """bugzilla2trac - Imports a bug database from Bugzilla into Trac.
|
|
834
|
|
835 Usage: bugzilla2trac.py [options]
|
|
836
|
|
837 Available Options:
|
|
838 --db <MySQL dbname> - Bugzilla's database name
|
|
839 --tracenv /path/to/trac/env - Full path to Trac db environment
|
|
840 -h | --host <MySQL hostname> - Bugzilla's DNS host name
|
|
841 -u | --user <MySQL username> - Effective Bugzilla's database user
|
|
842 -p | --passwd <MySQL password> - Bugzilla's user password
|
|
843 -c | --clean - Remove current Trac tickets before
|
|
844 importing
|
|
845 --help | help - This help info
|
|
846
|
|
847 Additional configuration options can be defined directly in the script.
|
|
848 """
|
|
849 sys.exit(0)
|
|
850
|
|
851 def main():
|
|
852 global BZ_DB, BZ_HOST, BZ_USER, BZ_PASSWORD, TRAC_ENV, TRAC_CLEAN
|
|
853 if len (sys.argv) > 1:
|
|
854 if sys.argv[1] in ['--help','help'] or len(sys.argv) < 4:
|
|
855 usage()
|
|
856 iter = 1
|
|
857 while iter < len(sys.argv):
|
|
858 if sys.argv[iter] in ['--db'] and iter+1 < len(sys.argv):
|
|
859 BZ_DB = sys.argv[iter+1]
|
|
860 iter = iter + 1
|
|
861 elif sys.argv[iter] in ['-h', '--host'] and iter+1 < len(sys.argv):
|
|
862 BZ_HOST = sys.argv[iter+1]
|
|
863 iter = iter + 1
|
|
864 elif sys.argv[iter] in ['-u', '--user'] and iter+1 < len(sys.argv):
|
|
865 BZ_USER = sys.argv[iter+1]
|
|
866 iter = iter + 1
|
|
867 elif sys.argv[iter] in ['-p', '--passwd'] and iter+1 < len(sys.argv):
|
|
868 BZ_PASSWORD = sys.argv[iter+1]
|
|
869 iter = iter + 1
|
|
870 elif sys.argv[iter] in ['--tracenv'] and iter+1 < len(sys.argv):
|
|
871 TRAC_ENV = sys.argv[iter+1]
|
|
872 iter = iter + 1
|
|
873 elif sys.argv[iter] in ['-c', '--clean']:
|
|
874 TRAC_CLEAN = 1
|
|
875 else:
|
|
876 print "Error: unknown parameter: " + sys.argv[iter]
|
|
877 sys.exit(0)
|
|
878 iter = iter + 1
|
|
879
|
|
880 convert(BZ_DB, BZ_HOST, BZ_USER, BZ_PASSWORD, TRAC_ENV, TRAC_CLEAN)
|
|
881
|
|
882 if __name__ == '__main__':
|
|
883 main()
|