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