From c5534c3969838973a879291670779b8f6643a8af Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Jul 14 2016 19:58:40 +0000
Subject: [PATCH 1/12] Manage trac attachment

Modified model to add attachment field
Modified trac utility to manage attachment

---

diff --git a/pagure_importer/utils/models.py b/pagure_importer/utils/models.py
index 3aa3d83..87bddd3 100644
--- a/pagure_importer/utils/models.py
+++ b/pagure_importer/utils/models.py
@@ -1,15 +1,15 @@
 # -*- coding: utf-8 -*-
-
 import datetime
 import json
 import uuid
 
+
 class Issue():
     ''' Represents an Issue '''
 
     def __init__(
             self, id, title, content,
-            status, date_created, user, private, tags,
+            status, date_created, user, private, attachment, tags,
             depends, blocks, assignee, comments=None):
 
         self.id = id
@@ -19,6 +19,7 @@ class Issue():
         self.date_created = date_created
         self.user = user
         self.private = private
+        self.attachment = attachment
         self.tags = tags
         self.depends = depends
         self.blocks = blocks
@@ -47,6 +48,9 @@ class Issue():
 
         return output
 
+    def get_attachment(self):
+        return self.attachment
+
     @property
     def isa(self):
         return 'issue'
diff --git a/pagure_importer/utils/trac.py b/pagure_importer/utils/trac.py
index b5e131b..62bece8 100644
--- a/pagure_importer/utils/trac.py
+++ b/pagure_importer/utils/trac.py
@@ -56,9 +56,17 @@ def populate_comments(fasclient, trac_comments):
 
 def populate_issue(trac, fasclient, ticket_id, tags):
     trac_ticket = trac.ticket.get(ticket_id)[3]
+    trac_attachments = trac.ticket.listAttachments(ticket_id)
+
+    pagure_attachment = {}
+    for attachment in trac_attachments:
+        filename = attachment[0]
+        content = trac.ticket.getAttachment(ticket_id, filename)
+        pagure_attachment[filename] = content
+
     pagure_issue_title = trac_ticket['summary']
-    pagure_issue_content = trac_ticket['description']
 
+    pagure_issue_content = trac_ticket['description']
     if pagure_issue_content == '':
         pagure_issue_content = '#No Description Provided'
 
@@ -98,6 +106,7 @@ def populate_issue(trac, fasclient, ticket_id, tags):
         date_created=pagure_issue_created_at,
         user=pagure_issue_user.to_json(),
         private=pagure_issue_is_private,
+        attachment=pagure_attachment,
         tags=pagure_issue_tags,
         depends=pagure_issue_depends,
         blocks=pagure_issue_blocks,

From 90fa13d08121f37be7e3284f662294432d56133e Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Jul 14 2016 20:00:06 +0000
Subject: [PATCH 2/12] Changed how we get tags


---

diff --git a/pagure_importer/utils/trac.py b/pagure_importer/utils/trac.py
index 62bece8..d5a3911 100644
--- a/pagure_importer/utils/trac.py
+++ b/pagure_importer/utils/trac.py
@@ -85,14 +85,10 @@ def populate_issue(trac, fasclient, ticket_id, tags):
 
     pagure_issue_tags = []
     if tags:
-        if trac_ticket['type'] != '':
-            pagure_issue_tags.append(trac_ticket['type'])
-        if trac_ticket['milestone'] != '':
-            pagure_issue_tags.append(trac_ticket['milestone'])
-        if trac_ticket['component'] != '':
-            pagure_issue_tags.append(trac_ticket['component'])
-        if trac_ticket['version'] != '':
-            pagure_issue_tags.append(trac_ticket['version'])
+        pagure_issue_tags = filter(lambda x: x != '', trac_ticket['keywords'].split(' '))
+
+        if meta[3]['milestone'] != '':
+            pagure_issue_tags.append(str(trac_ticket['milestone']))
 
     pagure_issue_depends = []
     pagure_issue_blocks = []

From 96b75a245d1f7a99ff991d4524e3544180ae08c6 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Jul 17 2016 20:09:44 +0000
Subject: [PATCH 3/12] Commit attached files in the cloned ticket repository


---

diff --git a/pagure_importer/utils/git.py b/pagure_importer/utils/git.py
index 33eba48..d09a59f 100644
--- a/pagure_importer/utils/git.py
+++ b/pagure_importer/utils/git.py
@@ -1,7 +1,8 @@
 ''' Code taken from https://pagure.io/pagure/blob/master/f/pagure/lib/git.py
     by pingou@pingoured.fr
-'''
 
+    Modified by Clement Verna <cverna@tutanota.com> to add attachment support
+'''
 import shutil
 import os
 import pygit2
@@ -10,6 +11,7 @@ import json
 
 from repo import *
 
+
 def update_git(obj, repo_path, repo_folder):
     """ Update the given issue in its git.
     This method forks the provided repo, add/edit the issue whose file name
@@ -37,6 +39,18 @@ def update_git(obj, repo_path, repo_folder):
     if not os.path.exists(file_path):
         added = True
 
+    # If we have attachments
+    attachments = obj.get_attachment()
+    if attachments:
+        if not os.path.exists(os.path.join(newpath, 'files')):
+            os.mkdir(os.path.join(newpath, 'files'))
+
+        for key in attachments.keys():
+            attach_path = os.path.join(newpath, 'files', key)
+            with open(attach_path, 'w') as stream:
+                stream.write(str(attachments[key]))
+            index.add('files/'+key)
+
     # Write down what changed
     with open(file_path, 'w') as stream:
         stream.write(json.dumps(

From 61c3329a24da4ce79a453d84cfdd20252a1e4377 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Jul 23 2016 16:01:21 +0000
Subject: [PATCH 4/12] Add the issue hash to attachment filename

So we can have a unique filename

---

diff --git a/pagure_importer/utils/git.py b/pagure_importer/utils/git.py
index d09a59f..419fab5 100644
--- a/pagure_importer/utils/git.py
+++ b/pagure_importer/utils/git.py
@@ -46,10 +46,11 @@ def update_git(obj, repo_path, repo_folder):
             os.mkdir(os.path.join(newpath, 'files'))
 
         for key in attachments.keys():
-            attach_path = os.path.join(newpath, 'files', key)
+            attach_path = os.path.join(newpath, 'files', obj.uid+key)
+            print attach_path
             with open(attach_path, 'w') as stream:
                 stream.write(str(attachments[key]))
-            index.add('files/'+key)
+            index.add('files/'+obj.uid+key)
 
     # Write down what changed
     with open(file_path, 'w') as stream:

From 5b2ce33587d3a1018869b1b9757169a313043257 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Jul 26 2016 20:42:56 +0000
Subject: [PATCH 5/12] Pushed changes to attachment managment


---

diff --git a/pagure_importer/commands/fedorahosted.py b/pagure_importer/commands/fedorahosted.py
index 44c234a..e11d794 100644
--- a/pagure_importer/commands/fedorahosted.py
+++ b/pagure_importer/commands/fedorahosted.py
@@ -1,5 +1,4 @@
 import click
-import os
 import getpass
 import pagure_importer
 from pagure_importer.app import app, REPO_PATH
@@ -24,8 +23,7 @@ def fedorahosted(project_url, tags):
     if repos:
         repo_index = raw_input('Choose the import destination repo (default 1) : ') or 1
         repo_name = repos[int(repo_index)-1]
-        trac_importer = importer_trac.TracImporter(rpc_url, fasclient)
-        trac_importer.import_issues(repo_name=repo_name, repo_folder=REPO_PATH,
-                                    tags=tags)
+        trac_importer = importer_trac.TracImporter(rpc_url, fasclient, tags)
+        trac_importer.import_issues(repo_name=repo_name, repo_folder=REPO_PATH)
     else:
         click.echo('No ticket repository found. Use pgimport clone command')
diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py
index b0ea1b5..85cd5c2 100644
--- a/pagure_importer/utils/importer_trac.py
+++ b/pagure_importer/utils/importer_trac.py
@@ -1,33 +1,134 @@
 from xmlrpclib import ServerProxy
+from datetime import datetime
 from pagure_importer.utils.git import update_git
-from pagure_importer.utils import trac
+from pagure_importer.utils.models import User, Issue, IssueComment
 
 
 class TracImporter():
     '''Pagure importer for trac instance'''
 
-    def __init__(self, trac_project_url, fasclient=None):
-        self.tracclient = ServerProxy(trac_project_url)
-        self.fasclient = fasclient
+    def __init__(self, trac_project_url, fasclient=None, tags=False):
+        self.trac = ServerProxy(trac_project_url)
+        self.fas = fasclient
+        self.tags = tags
 
-    def import_issues(self, repo_name, repo_folder, tags,
+    def import_issues(self, repo_name, repo_folder,
                       trac_query='max=0&order=id'):
         '''Import issues from trac instance using xmlrpc API'''
-        tickets_id = self.tracclient.ticket.query(trac_query)
+        tickets_id = self.trac.ticket.query(trac_query)
 
         for ticket_id in tickets_id:
 
-            pagure_issue = trac.populate_issue(self.tracclient,
-                                               self.fasclient, ticket_id, tags)
+            pagure_issue = self.create_issue(ticket_id)
 
-            pagure_issue_comments = self.tracclient.ticket.changeLog(ticket_id)
-            comments = trac.populate_comments(self.fasclient,
-                                              pagure_issue_comments)
+            pagure_issue_comments = self.trac.ticket.changeLog(ticket_id)
+            comments = self.create_comments(pagure_issue_comments)
 
             # add all the comments to the issue object
-            pagure_issue.comments = comments
+            for key in comments:
+                pagure_issue.comments.append(comments[key].to_json())
 
             # update the local git repo
-            print 'Update ' + repo_name + ' with issue :' + str(ticket_id) + '/' +\
-                str(tickets_id[-1])
+            print 'Update ' + repo_name + ' with issue :' + str(ticket_id) +\
+                '/' + str(tickets_id[-1])
             update_git(pagure_issue, repo_name, repo_folder)
+
+    def create_issue(self, ticket_id):
+
+        trac_ticket = self.trac.ticket.get(ticket_id)[3]
+        trac_attachments = self.trac.ticket.listAttachments(ticket_id)
+
+        pagure_attachment = {}
+        for attachment in trac_attachments:
+            filename = attachment[0]
+            content = self.trac.ticket.getAttachment(ticket_id, filename)
+            pagure_attachment[filename] = content
+
+        pagure_issue_title = trac_ticket['summary']
+
+        pagure_issue_content = trac_ticket['description']
+        if pagure_issue_content == '':
+            pagure_issue_content = '#No Description Provided'
+
+        pagure_issue_status = self.get_ticket_status(trac_ticket)
+
+        pagure_issue_created_at = datetime.strptime(
+            self.trac.ticket.get(ticket_id)[1].value, "%Y%m%dT%H:%M:%S")
+
+        if self.fas:
+            pagure_issue_assignee = self.fas.find_fas_user(trac_ticket['owner'])
+            pagure_issue_user = self.fas.find_fas_user(trac_ticket['reporter'])
+        else:
+            pagure_issue_assignee = User(name='', fullname='', emails=[])
+            pagure_issue_user = User(name='', fullname='', emails=[])
+
+        pagure_issue_tags = []
+        if self.tags:
+            pagure_issue_tags = filter(lambda x: x != '', trac_ticket['keywords'].split(' '))
+
+            if trac_ticket['milestone'] != '':
+                pagure_issue_tags.append(str(trac_ticket['milestone']))
+
+        pagure_issue_depends = []
+        pagure_issue_blocks = []
+        pagure_issue_is_private = False
+
+        pagure_issue = Issue(
+            id=ticket_id,
+            title=pagure_issue_title,
+            content=pagure_issue_content,
+            status=pagure_issue_status,
+            date_created=pagure_issue_created_at,
+            user=pagure_issue_user.to_json(),
+            private=pagure_issue_is_private,
+            attachment=pagure_attachment,
+            tags=pagure_issue_tags,
+            depends=pagure_issue_depends,
+            blocks=pagure_issue_blocks,
+            assignee=pagure_issue_assignee.to_json())
+        return pagure_issue
+
+    def get_ticket_status(self, trac_ticket):
+
+        if trac_ticket['status'] != 'closed':
+            ticket_status = 'Open'
+        else:
+            ticket_status = 'Fixed'
+        return ticket_status
+
+    def create_comments(self, trac_comments):
+        comments = {}
+        attachments = {}
+        for comment in trac_comments:
+            ts = datetime.strptime(comment[0].value, "%Y%m%dT%H:%M:%S")
+            if comment[2] == 'comment' and comment[4] != '':
+                if ts in comments:
+                    attachment = attachments[ts]
+                else:
+                    attachment = []
+                pagure_issue_comment_body = comment[4]
+                pagure_issue_comment_created_at = ts
+
+                # The User who commented
+                if self.fas:
+                    pagure_issue_comment_user = self.fas.find_fas_user(comment[1])
+                else:
+                    pagure_issue_comment_user = User(name='', fullname='', emails=[])
+
+                # Object to represent comment on an issue
+                pagure_issue_comment = IssueComment(
+                    id=ts,
+                    comment=pagure_issue_comment_body,
+                    date_created=pagure_issue_comment_created_at,
+                    attachment=attachment,
+                    user=pagure_issue_comment_user.to_json())
+
+                comments[ts] = pagure_issue_comment
+
+            elif comment[2] == 'attachment':
+                if ts in comments:
+                    comments[ts].attachment.append(comment[4])
+                else:
+                    attachments[ts] = comment[4]
+
+        return comments
diff --git a/pagure_importer/utils/models.py b/pagure_importer/utils/models.py
index 87bddd3..6b64b68 100644
--- a/pagure_importer/utils/models.py
+++ b/pagure_importer/utils/models.py
@@ -10,7 +10,7 @@ class Issue():
     def __init__(
             self, id, title, content,
             status, date_created, user, private, attachment, tags,
-            depends, blocks, assignee, comments=None):
+            depends, blocks, assignee, comments=[]):
 
         self.id = id
         self.title = title
@@ -61,13 +61,14 @@ class IssueComment():
 
     def __init__(
             self, id, comment, date_created,
-            user, parent=None, edited_on=None, editor=None):
+            user, attachment, parent=None, edited_on=None, editor=None):
 
         self.id = id
         self.comment = comment
         self.parent = parent
         self.date_created = date_created
         self.user = user
+        self.attachment = attachment
         self.edited_on = edited_on
         self.editor = editor
 
diff --git a/pagure_importer/utils/trac.py b/pagure_importer/utils/trac.py
index d5a3911..c8cf710 100644
--- a/pagure_importer/utils/trac.py
+++ b/pagure_importer/utils/trac.py
@@ -2,109 +2,9 @@ from pagure_importer.utils.models import IssueComment, Issue, User
 from datetime import datetime
 
 
-def get_ticket_status(trac_ticket):
-    ''' Converts Trac ticket status
-        to Pagure issue status'''
 
-    if trac_ticket['status'] != 'closed':
-        ticket_status = 'Open'
-    else:
-        ticket_status = 'Fixed'
-    return ticket_status
 
 
-def populate_comments(fasclient, trac_comments):
-    comments = []
-    for comment in trac_comments:
-        if comment[2] == 'comment' and comment[4] != '':
-            comment_user = comment[1]
-            pagure_issue_comment_user_email = None
-            pagure_issue_comment_body = comment[4]
-            pagure_issue_comment_created_at = datetime.strptime(
-                comment[0].value, "%Y%m%dT%H:%M:%S")
-            pagure_issue_comment_updated_at = None
-
-            # No idea what to do with this right now
-            # editor: not supported by github api
-            pagure_issue_comment_parent = None
-            pagure_issue_comment_editor = None
-
-            # comment updated at
-            pagure_issue_comment_edited_on = None
-
-            # The User who commented
-            if fasclient:
-                pagure_issue_comment_user = fasclient.find_fas_user(comment[1])
-            else:
-                pagure_issue_comment_user = User(name='',
-                                                 fullname='',
-                                                 emails=[])
-
-            # Object to represent comment on an issue
-            pagure_issue_comment = IssueComment(
-                id=None,
-                comment=pagure_issue_comment_body,
-                parent=pagure_issue_comment_parent,
-                date_created=pagure_issue_comment_created_at,
-                user=pagure_issue_comment_user.to_json(),
-                edited_on=pagure_issue_comment_edited_on,
-                editor=pagure_issue_comment_editor)
-
-            comments.append(pagure_issue_comment.to_json())
-    return comments
-
 
 def populate_issue(trac, fasclient, ticket_id, tags):
-    trac_ticket = trac.ticket.get(ticket_id)[3]
-    trac_attachments = trac.ticket.listAttachments(ticket_id)
-
-    pagure_attachment = {}
-    for attachment in trac_attachments:
-        filename = attachment[0]
-        content = trac.ticket.getAttachment(ticket_id, filename)
-        pagure_attachment[filename] = content
-
-    pagure_issue_title = trac_ticket['summary']
-
-    pagure_issue_content = trac_ticket['description']
-    if pagure_issue_content == '':
-        pagure_issue_content = '#No Description Provided'
-
-    pagure_issue_status = get_ticket_status(trac_ticket)
-
-    pagure_issue_created_at = datetime.strptime(
-        trac.ticket.get(ticket_id)[1].value, "%Y%m%dT%H:%M:%S")
-
-    if fasclient:
-        pagure_issue_assignee = fasclient.find_fas_user(trac_ticket['owner'])
-        pagure_issue_user = fasclient.find_fas_user(trac_ticket['reporter'])
-    else:
-        anonymous = User(name='', fullname='', emails=[])
-        pagure_issue_assignee = anonymous
-        pagure_issue_user = anonymous
-
-    pagure_issue_tags = []
-    if tags:
-        pagure_issue_tags = filter(lambda x: x != '', trac_ticket['keywords'].split(' '))
-
-        if meta[3]['milestone'] != '':
-            pagure_issue_tags.append(str(trac_ticket['milestone']))
-
-    pagure_issue_depends = []
-    pagure_issue_blocks = []
-    pagure_issue_is_private = False
-
-    pagure_issue = Issue(
-        id=ticket_id,
-        title=pagure_issue_title,
-        content=pagure_issue_content,
-        status=pagure_issue_status,
-        date_created=pagure_issue_created_at,
-        user=pagure_issue_user.to_json(),
-        private=pagure_issue_is_private,
-        attachment=pagure_attachment,
-        tags=pagure_issue_tags,
-        depends=pagure_issue_depends,
-        blocks=pagure_issue_blocks,
-        assignee=pagure_issue_assignee.to_json())
     return pagure_issue

From f4662564756f5ffb7f35e4194e50bcbb7b8b4fce Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Jul 27 2016 21:14:38 +0000
Subject: [PATCH 6/12] Adding Attachment to comment


---

diff --git a/pagure_importer/utils/git.py b/pagure_importer/utils/git.py
index 419fab5..70a2fe0 100644
--- a/pagure_importer/utils/git.py
+++ b/pagure_importer/utils/git.py
@@ -47,7 +47,6 @@ def update_git(obj, repo_path, repo_folder):
 
         for key in attachments.keys():
             attach_path = os.path.join(newpath, 'files', obj.uid+key)
-            print attach_path
             with open(attach_path, 'w') as stream:
                 stream.write(str(attachments[key]))
             index.add('files/'+obj.uid+key)
diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py
index 85cd5c2..21dff2b 100644
--- a/pagure_importer/utils/importer_trac.py
+++ b/pagure_importer/utils/importer_trac.py
@@ -26,6 +26,11 @@ class TracImporter():
 
             # add all the comments to the issue object
             for key in comments:
+                if comments[key].attachment:
+                    project = repo_name.replace('.git', '')
+                    url = '/%s/issue/raw/files/%s' % \
+                        (project, pagure_issue.uid+comments[key].attachment)
+                    comments[key].comment += '\n[%s](%s)' % (comments[key].attachment, url)
                 pagure_issue.comments.append(comments[key].to_json())
 
             # update the local git repo
@@ -98,14 +103,14 @@ class TracImporter():
 
     def create_comments(self, trac_comments):
         comments = {}
-        attachments = {}
         for comment in trac_comments:
             ts = datetime.strptime(comment[0].value, "%Y%m%dT%H:%M:%S")
             if comment[2] == 'comment' and comment[4] != '':
                 if ts in comments:
-                    attachment = attachments[ts]
+                    attachment = comments[ts]
                 else:
                     attachment = []
+
                 pagure_issue_comment_body = comment[4]
                 pagure_issue_comment_created_at = ts
 
@@ -117,7 +122,7 @@ class TracImporter():
 
                 # Object to represent comment on an issue
                 pagure_issue_comment = IssueComment(
-                    id=ts,
+                    id=None,
                     comment=pagure_issue_comment_body,
                     date_created=pagure_issue_comment_created_at,
                     attachment=attachment,
@@ -129,6 +134,5 @@ class TracImporter():
                 if ts in comments:
                     comments[ts].attachment.append(comment[4])
                 else:
-                    attachments[ts] = comment[4]
-
+                    comments[ts] = comment[4]
         return comments

From 06a1129d0aac4f1e117265ee4f25017c4fc96487 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Jul 28 2016 19:32:47 +0000
Subject: [PATCH 7/12] Attachment from fedorahosted are now included in the

pagure tickets

---

diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py
index 21dff2b..335a928 100644
--- a/pagure_importer/utils/importer_trac.py
+++ b/pagure_importer/utils/importer_trac.py
@@ -18,12 +18,10 @@ class TracImporter():
         tickets_id = self.trac.ticket.query(trac_query)
 
         for ticket_id in tickets_id:
-
             pagure_issue = self.create_issue(ticket_id)
-
+            pagure_issue.comments = []
             pagure_issue_comments = self.trac.ticket.changeLog(ticket_id)
             comments = self.create_comments(pagure_issue_comments)
-
             # add all the comments to the issue object
             for key in comments:
                 if comments[key].attachment:
@@ -32,11 +30,10 @@ class TracImporter():
                         (project, pagure_issue.uid+comments[key].attachment)
                     comments[key].comment += '\n[%s](%s)' % (comments[key].attachment, url)
                 pagure_issue.comments.append(comments[key].to_json())
-
             # update the local git repo
-            print 'Update ' + repo_name + ' with issue :' + str(ticket_id) +\
-                '/' + str(tickets_id[-1])
             update_git(pagure_issue, repo_name, repo_folder)
+            print 'Updated ' + repo_name + ' with issue :' + str(ticket_id) +\
+                '/' + str(tickets_id[-1])
 
     def create_issue(self, ticket_id):
 
diff --git a/pagure_importer/utils/trac.py b/pagure_importer/utils/trac.py
deleted file mode 100644
index c8cf710..0000000
--- a/pagure_importer/utils/trac.py
+++ /dev/null
@@ -1,10 +0,0 @@
-from pagure_importer.utils.models import IssueComment, Issue, User
-from datetime import datetime
-
-
-
-
-
-
-def populate_issue(trac, fasclient, ticket_id, tags):
-    return pagure_issue

From 980d5dd0436c2f9f24f23180fb9a53d60d01a40b Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Jul 28 2016 20:24:04 +0000
Subject: [PATCH 8/12] Create a comment issue when we have just an attachment


---

diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py
index 335a928..3877f14 100644
--- a/pagure_importer/utils/importer_trac.py
+++ b/pagure_importer/utils/importer_trac.py
@@ -98,11 +98,21 @@ class TracImporter():
             ticket_status = 'Fixed'
         return ticket_status
 
+    def get_comment_user(self, comment):
+        # The User who commented
+        if self.fas:
+            pagure_issue_comment_user = self.fas.find_fas_user(comment[1])
+        else:
+            pagure_issue_comment_user = User(name='', fullname='', emails=[])
+        return pagure_issue_comment_user
+
     def create_comments(self, trac_comments):
         comments = {}
         for comment in trac_comments:
             ts = datetime.strptime(comment[0].value, "%Y%m%dT%H:%M:%S")
+            
             if comment[2] == 'comment' and comment[4] != '':
+                print 'comment'
                 if ts in comments:
                     attachment = comments[ts]
                 else:
@@ -111,12 +121,7 @@ class TracImporter():
                 pagure_issue_comment_body = comment[4]
                 pagure_issue_comment_created_at = ts
 
-                # The User who commented
-                if self.fas:
-                    pagure_issue_comment_user = self.fas.find_fas_user(comment[1])
-                else:
-                    pagure_issue_comment_user = User(name='', fullname='', emails=[])
-
+                pagure_issue_comment_user = self.get_comment_user(comment)
                 # Object to represent comment on an issue
                 pagure_issue_comment = IssueComment(
                     id=None,
@@ -128,8 +133,16 @@ class TracImporter():
                 comments[ts] = pagure_issue_comment
 
             elif comment[2] == 'attachment':
+                print 'attachment'
                 if ts in comments:
                     comments[ts].attachment.append(comment[4])
                 else:
-                    comments[ts] = comment[4]
+                    pagure_issue_comment_user = self.get_comment_user(comment)
+                    comments[ts] = IssueComment(
+                        id=None,
+                        comment='attachment',
+                        date_created=ts,
+                        attachment=comment[4],
+                        user=pagure_issue_comment_user.to_json())
+
         return comments

From 7f50844d7b0944edd516c207acbcf0bdebabcdc1 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Jul 28 2016 20:29:58 +0000
Subject: [PATCH 9/12] Remove debug print statement


---

diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py
index 3877f14..4f4a6b2 100644
--- a/pagure_importer/utils/importer_trac.py
+++ b/pagure_importer/utils/importer_trac.py
@@ -110,9 +110,8 @@ class TracImporter():
         comments = {}
         for comment in trac_comments:
             ts = datetime.strptime(comment[0].value, "%Y%m%dT%H:%M:%S")
-            
+
             if comment[2] == 'comment' and comment[4] != '':
-                print 'comment'
                 if ts in comments:
                     attachment = comments[ts]
                 else:
@@ -133,7 +132,6 @@ class TracImporter():
                 comments[ts] = pagure_issue_comment
 
             elif comment[2] == 'attachment':
-                print 'attachment'
                 if ts in comments:
                     comments[ts].attachment.append(comment[4])
                 else:

From 88e2aeec779e2bb13986fbcb90ee40757c39ded5 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Jul 29 2016 20:17:15 +0000
Subject: [PATCH 10/12] Create a somebody user for comment, pagure does not support anonymous comment


---

diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py
index 4f4a6b2..8b20563 100644
--- a/pagure_importer/utils/importer_trac.py
+++ b/pagure_importer/utils/importer_trac.py
@@ -11,6 +11,7 @@ class TracImporter():
         self.trac = ServerProxy(trac_project_url)
         self.fas = fasclient
         self.tags = tags
+        self.somebody = User(name='somebody', fullname='somebody', emails=['some@body.com'])
 
     def import_issues(self, repo_name, repo_folder,
                       trac_query='max=0&order=id'):
@@ -102,8 +103,10 @@ class TracImporter():
         # The User who commented
         if self.fas:
             pagure_issue_comment_user = self.fas.find_fas_user(comment[1])
+            if not pagure_issue_comment_user.name:
+                pagure_issue_comment_user = self.somebody
         else:
-            pagure_issue_comment_user = User(name='', fullname='', emails=[])
+            pagure_issue_comment_user = self.somebody
         return pagure_issue_comment_user
 
     def create_comments(self, trac_comments):

From 7315e8b9b8ea15e0d451a54ddd8001bcecec8c65 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Jul 31 2016 16:13:42 +0000
Subject: [PATCH 11/12] Changed the way the attachment filename is build

to use the same method as pagure.io does

---

diff --git a/pagure_importer/utils/git.py b/pagure_importer/utils/git.py
index 70a2fe0..d996050 100644
--- a/pagure_importer/utils/git.py
+++ b/pagure_importer/utils/git.py
@@ -8,10 +8,18 @@ import os
 import pygit2
 import tempfile
 import json
+import hashlib
+import werkzeug
 
 from repo import *
 
 
+def get_secure_filename(attachment, filename):
+    filename = '%s-%s' % (hashlib.sha256(str(attachment)).hexdigest(),
+        werkzeug.secure_filename(str(filename)))
+    return filename
+
+
 def update_git(obj, repo_path, repo_folder):
     """ Update the given issue in its git.
     This method forks the provided repo, add/edit the issue whose file name
@@ -46,10 +54,11 @@ def update_git(obj, repo_path, repo_folder):
             os.mkdir(os.path.join(newpath, 'files'))
 
         for key in attachments.keys():
-            attach_path = os.path.join(newpath, 'files', obj.uid+key)
+            filename = get_secure_filename(attachments[key], key)
+            attach_path = os.path.join(newpath, 'files', filename)
             with open(attach_path, 'w') as stream:
                 stream.write(str(attachments[key]))
-            index.add('files/'+obj.uid+key)
+            index.add('files/'+filename)
 
     # Write down what changed
     with open(file_path, 'w') as stream:
diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py
index 8b20563..a298bb3 100644
--- a/pagure_importer/utils/importer_trac.py
+++ b/pagure_importer/utils/importer_trac.py
@@ -1,6 +1,6 @@
 from xmlrpclib import ServerProxy
 from datetime import datetime
-from pagure_importer.utils.git import update_git
+from pagure_importer.utils.git import update_git, get_secure_filename
 from pagure_importer.utils.models import User, Issue, IssueComment
 
 
@@ -26,10 +26,11 @@ class TracImporter():
             # add all the comments to the issue object
             for key in comments:
                 if comments[key].attachment:
+                    attach_name = comments[key].attachment
                     project = repo_name.replace('.git', '')
-                    url = '/%s/issue/raw/files/%s' % \
-                        (project, pagure_issue.uid+comments[key].attachment)
-                    comments[key].comment += '\n[%s](%s)' % (comments[key].attachment, url)
+                    filename = get_secure_filename(pagure_issue.attachment[attach_name], attach_name)
+                    url = '/%s/issue/raw/files/%s' % (project, filename)
+                    comments[key].comment += '\n[%s](%s)' % (attach_name, url)
                 pagure_issue.comments.append(comments[key].to_json())
             # update the local git repo
             update_git(pagure_issue, repo_name, repo_folder)
@@ -116,7 +117,7 @@ class TracImporter():
 
             if comment[2] == 'comment' and comment[4] != '':
                 if ts in comments:
-                    attachment = comments[ts]
+                    attachment = comments[ts].attachment
                 else:
                     attachment = []
 
@@ -125,15 +126,13 @@ class TracImporter():
 
                 pagure_issue_comment_user = self.get_comment_user(comment)
                 # Object to represent comment on an issue
-                pagure_issue_comment = IssueComment(
+                comments[ts] = IssueComment(
                     id=None,
                     comment=pagure_issue_comment_body,
                     date_created=pagure_issue_comment_created_at,
                     attachment=attachment,
                     user=pagure_issue_comment_user.to_json())
 
-                comments[ts] = pagure_issue_comment
-
             elif comment[2] == 'attachment':
                 if ts in comments:
                     comments[ts].attachment.append(comment[4])
diff --git a/pagure_importer/utils/models.py b/pagure_importer/utils/models.py
index 6b64b68..3c129d6 100644
--- a/pagure_importer/utils/models.py
+++ b/pagure_importer/utils/models.py
@@ -10,7 +10,7 @@ class Issue():
     def __init__(
             self, id, title, content,
             status, date_created, user, private, attachment, tags,
-            depends, blocks, assignee, comments=[]):
+            depends, blocks, assignee, comments=None):
 
         self.id = id
         self.title = title

From cab25d7e28e1ae0f66aa8b59e0ad1c12d8950d5b Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Jul 31 2016 17:56:36 +0000
Subject: [PATCH 12/12] Added missing module to requirements.txt


---

diff --git a/requirements.txt b/requirements.txt
index ba8d9e8..780699f 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -2,3 +2,4 @@ PyGithub
 click
 python-fedora
 pygit2 >= 0.20.1
+werkzeug

