From 5442b82bbf22d0e246e36f4e8a32060cfc649e51 Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: Feb 11 2017 14:14:42 +0000
Subject: [PATCH 1/3] Also migrate changes history


Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>

---

diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py
index ffaee9d..1254e08 100644
--- a/pagure_importer/utils/importer_trac.py
+++ b/pagure_importer/utils/importer_trac.py
@@ -288,8 +288,10 @@ class TracImporter(object):
             if comment[2] == 'comment' and comment[4] != '':
                 if ts in comments:
                     attachment = comments[ts].attachment
+                    changes = comments[ts].changes
                 else:
                     attachment = []
+                    changes = {}
 
                 pagure_issue_comment_body = comment[4]
                 pagure_issue_comment_created_at = ts
@@ -301,6 +303,7 @@ class TracImporter(object):
                     comment=pagure_issue_comment_body,
                     date_created=pagure_issue_comment_created_at,
                     attachment=attachment,
+                    changes=changes,
                     user=pagure_issue_comment_user.to_json())
 
             elif comment[2] == 'attachment':
@@ -315,4 +318,18 @@ class TracImporter(object):
                         attachment=[comment[4]],
                         user=pagure_issue_comment_user.to_json())
 
+            elif comment[2] != 'comment':  # We exclude the (comment, nr, )
+                change = (comment[3], comment[4])
+                if ts in comments:
+                    comments[ts].changes[comment[2]] = change
+                else:
+                    pagure_issue_comment_user = self.get_comment_user(comment)
+                    comments[ts] = IssueComment(
+                        id=None,
+                        comment='Fields changed',
+                        changes={comment[2]: change},
+                        date_created=ts,
+                        attachment=[comment[4]],
+                        user=pagure_issue_comment_user.to_json())
+
         return comments
diff --git a/pagure_importer/utils/models.py b/pagure_importer/utils/models.py
index 7a17ea6..659f086 100644
--- a/pagure_importer/utils/models.py
+++ b/pagure_importer/utils/models.py
@@ -63,7 +63,8 @@ class IssueComment():
 
     def __init__(
             self, id, comment, date_created,
-            user, attachment, parent=None, edited_on=None, editor=None):
+            user, attachment, parent=None, edited_on=None, editor=None,
+            changes=None):
 
         self.id = id
         self.comment = comment
@@ -73,6 +74,9 @@ class IssueComment():
         self.attachment = attachment
         self.edited_on = edited_on
         self.editor = editor
+        self.changes = changes
+        if not changes:
+            self.changes = {}
 
     def to_json(self):
         ''' Returns a dictionary representation of the issue. '''
@@ -87,6 +91,13 @@ class IssueComment():
             'editor': self.editor or None
         }
 
+        if len(self.changes) > 0:
+            ctext = '\n'
+            for change in self.changes:
+                ctext += '\n%s: %s => %s' % (change, self.changes[change][0],
+                                             self.changes[change][1])
+            output['comment'] += ctext
+
         return output
 
 

From 012cb5dafeaee0521b3d618cd063f561a022e1b4 Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: Feb 11 2017 14:14:42 +0000
Subject: [PATCH 2/3] Also migrate some standard fields


Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>

---

diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py
index 1254e08..6d158b7 100644
--- a/pagure_importer/utils/importer_trac.py
+++ b/pagure_importer/utils/importer_trac.py
@@ -12,6 +12,15 @@ from pagure_importer.utils import (
 from pagure_importer.utils.models import User, Issue, IssueComment
 
 
+# These are fields that are in a standard Trac setup, but we handle them like
+# custom fields, since they're not in Pagure natively
+STANDARD_CUSTOM_FIELDS = [
+    'type',
+    'component',
+    'version',
+]
+
+
 def to_timestamp(tm):
     ''' Convert to timestamp which can be jsonified '''
 
@@ -84,7 +93,8 @@ class TracImporter(object):
         all_ticket_fields = self.request('ticket.getTicketFields')
         custom_fields = []
         for field in all_ticket_fields:
-            if field.get('custom') is True:
+            if field.get('custom') is True or \
+                    field['name'] in STANDARD_CUSTOM_FIELDS:
                 current_field = {}
                 current_field['name'] = field['name']
                 key_type = 'text'
@@ -176,6 +186,7 @@ class TracImporter(object):
 
         pagure_custom_fields = self.get_custom_fields_of_ticket(trac_ticket)
         pagure_issue_title = trac_ticket['summary']
+        pagure_issue_priority = trac_ticket['priority']
 
         pagure_issue_content = trac_ticket['description']
         if pagure_issue_content == '':
@@ -225,6 +236,7 @@ class TracImporter(object):
         pagure_issue = Issue(
             id=ticket_id + self.offset,
             title=pagure_issue_title,
+            priority=pagure_issue_priority,
             content=pagure_issue_content,
             status=issue_status,
             close_status=close_status,
diff --git a/pagure_importer/utils/models.py b/pagure_importer/utils/models.py
index 659f086..0ad830b 100644
--- a/pagure_importer/utils/models.py
+++ b/pagure_importer/utils/models.py
@@ -9,7 +9,7 @@ class Issue():
             self, id, title, content,
             status, date_created, user, private, attachment, tags,
             depends, blocks, assignee, close_status, comments=None,
-            milestone=None, custom_fields=None):
+            milestone=None, custom_fields=None, priority=None):
 
         self.id = id
         self.title = title
@@ -28,6 +28,7 @@ class Issue():
         self.uid = uuid.uuid4().hex
         self.milestone = milestone
         self.custom_fields = custom_fields if custom_fields else []
+        self.priority = priority
 
     def to_json(self):
         ''' Returns a dictionary representation of the issue.
@@ -49,6 +50,7 @@ class Issue():
             'comments': self.comments,
             'milestone': self.milestone,
             'custom_fields': self.custom_fields,
+            'priority': self.priority
         }
 
         return output

From 2cce5f113ba733cdf7f89a041c66d08fcfc20b4d Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: Feb 11 2017 14:14:42 +0000
Subject: [PATCH 3/3] More info migration


Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>

---

diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py
index 6d158b7..91ee45d 100644
--- a/pagure_importer/utils/importer_trac.py
+++ b/pagure_importer/utils/importer_trac.py
@@ -12,6 +12,15 @@ from pagure_importer.utils import (
 from pagure_importer.utils.models import User, Issue, IssueComment
 
 
+PRIORITY_TO_NR = {
+    'blocker': 1,
+    'critical': 2,
+    'major': 3,
+    'minor': 4,
+    'trivial': 5
+}
+
+
 # These are fields that are in a standard Trac setup, but we handle them like
 # custom fields, since they're not in Pagure natively
 STANDARD_CUSTOM_FIELDS = [
@@ -52,6 +61,7 @@ class TracImporter(object):
                              emails=['some@body.com'])
         self.reqid = 0
         self.custom_fields = []
+        self.lists_to_create = {}
 
     def __enter__(self):
         return self
@@ -92,6 +102,7 @@ class TracImporter(object):
 
         all_ticket_fields = self.request('ticket.getTicketFields')
         custom_fields = []
+        priorities = {}
         for field in all_ticket_fields:
             if field.get('custom') is True or \
                     field['name'] in STANDARD_CUSTOM_FIELDS:
@@ -100,9 +111,14 @@ class TracImporter(object):
                 key_type = 'text'
                 if field['type'] == 'checkbox':
                     key_type = 'boolean'
+                elif field['type'] == 'select':
+                    key_type = 'list'
+                    self.lists_to_create[field['name']] = ','.join(field['options'])
                 current_field['key_type'] = key_type
                 custom_fields.append(current_field)
-        return custom_fields
+            elif field.get('name') == 'priority':
+                priorities = field['options']
+        return custom_fields, priorities
 
     def import_issues(self, repo_name, trac_query='max=0&order=id'):
         ''' Queries the trac instance via its jsonrpc API and convert the
@@ -117,7 +133,15 @@ class TracImporter(object):
         '''
 
         tickets_id = self.request('ticket.query', trac_query)
-        self.custom_fields = self.get_custom_fields()
+        self.custom_fields, priorities = self.get_custom_fields()
+
+        for priority in priorities:
+            if not priority in PRIORITY_TO_NR:
+                raise Exception('Priority %s does not have a value' % priority)
+
+        for lst in self.lists_to_create:
+            print('Create custom field list %s values %s' %
+                  (lst, self.lists_to_create[lst]))
 
         for ticket_id in tickets_id:
             pagure_issue = self.create_issue(ticket_id)
@@ -186,7 +210,7 @@ class TracImporter(object):
 
         pagure_custom_fields = self.get_custom_fields_of_ticket(trac_ticket)
         pagure_issue_title = trac_ticket['summary']
-        pagure_issue_priority = trac_ticket['priority']
+        pagure_issue_priority = PRIORITY_TO_NR[trac_ticket['priority']]
 
         pagure_issue_content = trac_ticket['description']
         if pagure_issue_content == '':

