Commits

jonas  committed a5c7a46

A few improvements for the bugzilla2trac script, thanks Dirk

  • Participants
  • Parent commits 4bdefa0

Comments (0)

Files changed (2)

 Christopher Armstrong   radix
-Juanma Barranquero
+Juanma Barranquero      lektu@terra.es
 Rocky Burt              rocky.burt@myrealbox.com
 Toni Brkic              toni.brkic@switchcore.com
 Daragh Fitzpatrick      Daragh@i2i-Tech.com

File contrib/bugzilla2trac.py

 #
 # If you run this script on a version not listed here and it is successful,
 # please report it to the Trac mailing list so we can update the list.
-BZ_VERSION = '2.11'
+BZ_VERSION = '2.16.5'
 
 # MySQL connection parameters for the Bugzilla database.  These can also 
 # be specified on the command line.
 class Attachment:
     def __init__(self, name, data):
         self.filename = name
-        self.file = StringIO.StringIO(data)
+        self.file = StringIO.StringIO(data.tostring())
   
 # simple field translation mapping.  if string not in
 # mapping, just return string, otherwise return value
         c = self.db().cursor()
         c.execute("""DELETE FROM component""")
         for comp in l:
-            print "inserting component ", comp[key]
-            c.execute("""INSERT INTO component (name) VALUES (%s)""",
-                      comp[key].encode('utf-8'))
+            print "inserting component '",comp[key],"', owner",  comp['owner']
+            c.execute("""INSERT INTO component (name, owner) VALUES (%s, %s)""",
+                      comp[key].encode('utf-8'), comp['owner'].encode('utf-8'))
         self.db().commit()
     
     def setVersionList(self, v, key):
                   severity.encode('utf-8'), priority.encode('utf-8'), owner, reporter, cc,
                   version, milestone.encode('utf-8'), status.lower(), resolution,
                   summary.encode('utf-8'), desc, keywords)
+        
         self.db().commit()
         return self.db().db.sqlite_last_insert_rowid()
     
         
     def addAttachment(self, id, attachment, description, author):
         print 'inserting attachment for ticket %s -- %s' % (id, description)
-        self.env.create_attachment(self.db(), 'ticket', str(id), attachment, description,
+        attachment.filename = attachment.filename.encode('utf-8')
+        self.env.create_attachment(self.db(), 'ticket', str(id), attachment, description.encode('utf-8'),
             author, 'unknown')
         
     def getLoginName(self, cursor, userid):
 
     print
     print "2. import components..."
-    sql = "SELECT DISTINCTROW value FROM components"
+    sql = "SELECT value, initialowner AS owner FROM components"
     if PRODUCTS:
        sql += " WHERE %s" % productFilter('program', PRODUCTS)
     mysql_cur.execute(sql)
     components = mysql_cur.fetchall()
+    for component in components:
+    		component['owner'] = trac.getLoginName(mysql_cur, component['owner'])
     trac.setComponentList(components, 'value')
 
     print
         mysql_cur.execute("SELECT * FROM bugs_activity WHERE bug_id = %s ORDER BY bug_when" % bugid)
         bugs_activity = mysql_cur.fetchall()
         resolution = ''
+        ticketChanges = []
         for activity in bugs_activity:
             field_name = trac.getFieldName(mysql_cur, activity['fieldid']).lower()
             
                     keywords.append(kw)
                     keywordChange = True
 
+            ticketChange = {}
+            ticketChange['ticket'] = bugid
+            ticketChange['time'] = activity['bug_when']
+            ticketChange['author'] = trac.getLoginName(mysql_cur, activity['who'])
+            ticketChange['field'] = field_name
+            ticketChange['oldvalue'] = removed
+            ticketChange['newvalue'] = added
+
             if keywordChange:
                 newKeywords = string.join(keywords, " ")
-                trac.addTicketChange(ticket=bugid, time=activity['bug_when'],
-                    author=trac.getLoginName(mysql_cur, activity['who']),
-                    field='keywords', oldvalue=oldKeywords, newvalue=newKeywords)
+                ticketChangeKw = ticketChange
+                ticketChangeKw['field'] = 'keywords'
+                ticketChangeKw['oldvalue'] = oldKeywords
+                ticketChangeKw['newvalue'] = newKeywords
+                #trac.addTicketChange(ticket=bugid, time=activity['bug_when'],
+                #    author=trac.getLoginName(mysql_cur, activity['who']),
+                #    field='keywords', oldvalue=oldKeywords, newvalue=newKeywords)
+                ticketChanges.append(ticketChangeKw)
 
             if field_name in IGNORED_ACTIVITY_FIELDS:
                 continue
             if added == removed:
                 continue
                 
-            trac.addTicketChange(ticket=bugid, time=activity['bug_when'],
-                author=trac.getLoginName(mysql_cur, activity['who']),
-                field=field_name, oldvalue=removed, newvalue=added)
+            # bugzilla splits large summary changes into two records
+            for oldChange in ticketChanges:
+              if (field_name == 'summary'
+                  and oldChange['field'] == ticketChange['field'] 
+                  and oldChange['time'] == ticketChange['time'] 
+                  and oldChange['author'] == ticketChange['author']):
+                  oldChange['oldvalue'] += " " + ticketChange['oldvalue'] 
+                  oldChange['newvalue'] += " " + ticketChange['newvalue']
+                  break
+            else:
+                #trac.addTicketChange(ticket=bugid, time=activity['bug_when'],
+                #    author=trac.getLoginName(mysql_cur, activity['who']),
+                #    field=field_name, oldvalue=removed, newvalue=added)
+                ticketChanges.append (ticketChange)
+
+        for ticketChange in ticketChanges:
+            trac.addTicketChange (**ticketChange)
+
 
         # for some reason, bugzilla v2.11 seems to clear the resolution
         # when you mark a bug as closed.  let's remember it and restore
         attachments = mysql_cur.fetchall()
         for a in attachments:
             author = trac.getLoginName(mysql_cur, a['submitter_id'])
-
+            
             tracAttachment = Attachment(a['filename'], a['thedata'])
             trac.addAttachment(bugid, tracAttachment, a['description'], author)