incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [1/3] git commit: [#6610] Move Trac-specific parsing to subclass
Date Fri, 01 Nov 2013 17:09:59 GMT
Updated Branches:
  refs/heads/master e0b8c01d8 -> 63cbafe8e


[#6610] Move Trac-specific parsing to subclass

Signed-off-by: Tim Van Steenburgh <tvansteenburgh@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/63cbafe8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/63cbafe8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/63cbafe8

Branch: refs/heads/master
Commit: 63cbafe8e0b0d173277bc54b3f29451f7106454d
Parents: 68bd5ae
Author: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Authored: Wed Oct 30 05:14:03 2013 +0000
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Fri Nov 1 17:09:53 2013 +0000

----------------------------------------------------------------------
 .../trac/tests/data/trac-export.json            | 67 +++++++++++++++++++
 .../forgeimporters/trac/tests/test_tickets.py   | 66 +++++++++++++++++-
 ForgeImporters/forgeimporters/trac/tickets.py   | 70 +++++++++++++++++++-
 ForgeTracker/forgetracker/import_support.py     | 53 +++------------
 .../tests/functional/test_import.py             | 63 ------------------
 5 files changed, 207 insertions(+), 112 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/63cbafe8/ForgeImporters/forgeimporters/trac/tests/data/trac-export.json
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/trac/tests/data/trac-export.json b/ForgeImporters/forgeimporters/trac/tests/data/trac-export.json
new file mode 100644
index 0000000..82d7b89
--- /dev/null
+++ b/ForgeImporters/forgeimporters/trac/tests/data/trac-export.json
@@ -0,0 +1,67 @@
+{
+  "class": "PROJECT", 
+  "trackers": {
+    "default": {
+      "artifacts": [
+        {
+          "assigned_to": "hinojosa4", 
+          "attachments": [
+            {
+              "by": "ma_boehm", 
+              "date": "2009-04-13T08:49:52Z", 
+              "description": "Shows the display error", 
+              "filename": "SF%20display%20error.JPG", 
+              "size": 184728, 
+              "url": "http://sourceforge.net/apps/trac/sourceforge/raw-attachment/ticket/204/SF%20display%20error.JPG"
+            }
+          ], 
+          "cc": "ma_boehm", 
+          "class": "ARTIFACT", 
+          "comments": [
+            {
+              "class": "COMMENT", 
+              "comment": "  * **attachment** set to _SF display error.JPG_\n\nShows the display
error\n\n", 
+              "date": "2009-04-13T08:49:52Z", 
+              "submitter": "ma_boehm"
+            }, 
+            {
+              "class": "COMMENT", 
+              "comment": "  * **owner** set to _hinojosa_\n\n  * **status** changed from
_new_ to _assigned_\n\nHi,\n\nI'm investigating this. I need to fire up my system with this
browser.\n\nIf you need anything else, please post a response to this request.\n\nCheers!,\n\nDaniel
Hinojosa - Sr. Manager, [SourceForge][1].net Support\n\nP.S., Be sure to monitor our Site
Status page,\n[https://sourceforge.net/sitestatus][2].\n\n   [1]: http://sourceforge.net/apps/trac/sourceforge/wiki/SourceForge\n\n
  [2]: https://sourceforge.net/sitestatus\n\n", 
+              "date": "2009-04-13T15:46:18Z", 
+              "submitter": "hinojosa"
+            }, 
+            {
+              "class": "COMMENT", 
+              "comment": "  * **keywords** _ENGR_ added; _IE_ _7,_ _Internet_ _Explorer_
removed\n\n  * **status** changed from _assigned_ to _accepted_\n\n  * **summary** changed
from _Public Info page not displayed properly_ to\n_ENGR: Public Info page not displayed properly_\n\nHi
again,\n\nI have reproduced this on Vista/ IE7. We are also seeing this on XP/IE7. I\nhave
engaged the engineering team on this issue.\n\nI'll update this item when I have news.\n\n",

+              "date": "2009-04-13T18:53:52Z", 
+              "submitter": "hinojosa"
+            }, 
+            {
+              "class": "COMMENT", 
+              "comment": "  * **status** changed from _accepted_ to _closed_\n\n  * **resolution**
set to _fixed_\n\nHello,\n\nThis issue is should be resolved with the site redesign.\n\nRegards,
Chris Tsai, SourceForge.net Support\n\n", 
+              "date": "2009-07-20T15:44:32Z", 
+              "submitter": "ctsai"
+            },
+            {
+              "class": "COMMENT",
+              "comment": "test link [[2496]](http://testlink.com)  test ticket ([#201](http://sourceforge.net/apps/trac/sourceforge/ticket/201))
\n [test comment](http://sourceforge.net/apps/trac/sourceforge/ticket/204#comment:1) \n [test
comment](http://sourceforge.net/apps/trac/sourceforge/ticket/204#comment:45)",
+              "date": "2009-07-21T15:44:32Z",
+              "submitter": "ctsai"
+            }
+          ], 
+          "date": "2009-04-13T08:49:13Z", 
+          "date_updated": "2009-07-20T15:44:32Z", 
+          "description": "This problem occurs with IE 7, Windows Vista:\r\nOn the project's
public info page (for example:\r\nhttps://sourceforge.net/project/admin/public_info.php?group_id=258655),
the text boxes next to \"Descriptive Name\" and \"Project Description\" are not aligned properly;
see the screenshot attached. ",
+          "id": 204, 
+          "keywords": "ENGR",
+          "milestone": "test_milestone",
+          "private": false, 
+          "resolution": "fixed", 
+          "status": "open", 
+          "submitter": "ma_boehm", 
+          "summary": "ENGR: Public Info page not displayed properly"
+        }
+      ]
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/63cbafe8/ForgeImporters/forgeimporters/trac/tests/test_tickets.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/trac/tests/test_tickets.py b/ForgeImporters/forgeimporters/trac/tests/test_tickets.py
index 63b5484..f55ac67 100644
--- a/ForgeImporters/forgeimporters/trac/tests/test_tickets.py
+++ b/ForgeImporters/forgeimporters/trac/tests/test_tickets.py
@@ -16,19 +16,24 @@
 #       under the License.
 
 import json
+import os
+
 from unittest import TestCase
 from mock import Mock, patch
 from ming.orm import ThreadLocalORMSession
-
-from tg import config
+from pylons import tmpl_context as c
 
 from allura.tests import TestController
 from allura.tests.decorators import with_tracker
+from alluratest.controller import TestRestApiBase
 
 from allura import model as M
+from forgetracker import model as TM
+
 from forgeimporters.trac.tickets import (
     TracTicketImporter,
     TracTicketImportController,
+    TracImportSupport,
     )
 
 
@@ -36,7 +41,7 @@ class TestTracTicketImporter(TestCase):
     @patch('forgeimporters.trac.tickets.session')
     @patch('forgeimporters.trac.tickets.g')
     @patch('forgeimporters.trac.tickets.AuditLog')
-    @patch('forgeimporters.trac.tickets.ImportSupport')
+    @patch('forgeimporters.trac.tickets.TracImportSupport')
     @patch('forgeimporters.trac.tickets.export')
     def test_import_tool(self, export, ImportSupport, AuditLog, g, session):
         user_map = {"orig_user":"new_user"}
@@ -143,3 +148,58 @@ class TestTracTicketImportController(TestController, TestCase):
                 status=302).follow()
         self.assertIn('Please wait and try again', r)
         self.assertEqual(import_tool.post.call_count, 0)
+
+
+class TestTracImportSupport(TestCase):
+    def test_link_processing(self):
+        import_support = TracImportSupport()
+        import_support.get_slug_by_id = lambda ticket, comment: '123'
+        result = import_support.link_processing('''\
+                test link [[2496]](http://testlink.com)
+                test ticket ([#201](http://sourceforge.net/apps/trac/sourceforge/ticket/201))
+                Replying to [someuser](http://sourceforge.net/apps/trac/sourceforge/ticket/204#comment:1)
+                #200 unchanged''')
+
+        assert "test link [\[2496\]](http://testlink.com)" in result
+        assert 'test ticket ([#201](201))' in result
+        assert 'Replying to [someuser](204/#123)' in result
+        assert '#200 unchanged' in result, result
+
+
+class TestTracImportSupportFunctional(TestRestApiBase, TestCase):
+    @with_tracker
+    def test_links(self):
+        doc_text = open(os.path.dirname(__file__) + '/data/trac-export.json').read()
+
+        TracImportSupport().perform_import(doc_text,
+                '{"user_map": {"hinojosa4": "test-admin", "ma_boehm": "test-user"}}')
+
+        r = self.app.get('/p/test/bugs/204/')
+        ticket = TM.Ticket.query.get(app_config_id=c.app.config._id,
+                                    ticket_num=204)
+        slug = ticket.discussion_thread.post_class().query.find(dict(
+            discussion_id=ticket.discussion_thread.discussion_id,
+            thread_id=ticket.discussion_thread._id,
+            status={'$in': ['ok', 'pending']})).sort('timestamp').all()[0].slug
+
+        assert '[test comment](204/#%s)' % slug in r
+        assert 'test link [\[2496\]](http://testlink.com)' in r
+        assert 'test ticket ([#201](201))' in r
+
+    @with_tracker
+    def test_slug(self):
+        doc_text = open(os.path.dirname(__file__) + '/data/trac-export.json').read()
+
+        TracImportSupport().perform_import(doc_text,
+                '{"user_map": {"hinojosa4": "test-admin", "ma_boehm": "test-user"}}')
+
+        ticket = TM.Ticket.query.get(app_config_id=c.app.config._id,
+                                    ticket_num=204)
+        comments = ticket.discussion_thread.post_class().query.find(dict(
+            discussion_id=ticket.discussion_thread.discussion_id,
+            thread_id=ticket.discussion_thread._id,
+            status={'$in': ['ok', 'pending']})).sort('timestamp').all()
+
+        import_support = TracImportSupport()
+        self.assertEqual(import_support.get_slug_by_id('204', '1'), comments[0].slug)
+        self.assertEqual(import_support.get_slug_by_id('204', '2'), comments[1].slug)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/63cbafe8/ForgeImporters/forgeimporters/trac/tickets.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/trac/tickets.py b/ForgeImporters/forgeimporters/trac/tickets.py
index 2b17ef7..d86f965 100644
--- a/ForgeImporters/forgeimporters/trac/tickets.py
+++ b/ForgeImporters/forgeimporters/trac/tickets.py
@@ -16,6 +16,7 @@
 #       under the License.
 
 import json
+import re
 
 from formencode import validators as fev
 
@@ -49,6 +50,7 @@ from forgeimporters.base import (
         )
 from forgetracker.tracker_main import ForgeTrackerApp
 from forgetracker.import_support import ImportSupport
+from forgetracker import model as TM
 
 
 class TracTicketImportForm(ToolImportForm):
@@ -118,7 +120,7 @@ class TracTicketImporter(ToolImporter):
         session(app.globals).flush(app.globals)
         try:
             with h.push_config(c, app=app):
-                ImportSupport().perform_import(
+                TracImportSupport().perform_import(
                         json.dumps(export(trac_url), cls=DateJSONEncoder),
                         json.dumps({
                             'user_map': json.loads(user_map) if user_map else {},
@@ -151,3 +153,69 @@ class TracTicketImporter(ToolImporter):
 
         """
         return False
+
+
+class TracImportSupport(ImportSupport):
+    """Provides Trac-specific ticket and comment text processing."""
+
+    def ticket_link(self, m):
+        return '(%s)' % m.groups()[0]
+
+    def get_slug_by_id(self, ticket, comment):
+        """Given the id of an imported Trac comment, return it's Allura slug.
+
+        """
+        comment = int(comment)
+        ticket = TM.Ticket.query.get(app_config_id=c.app.config._id,
+                                     ticket_num=int(ticket))
+        if not ticket:
+            return ''
+        comments = ticket.discussion_thread.post_class().query.find(dict(
+            discussion_id=ticket.discussion_thread.discussion_id,
+            thread_id=ticket.discussion_thread._id,
+            status={'$in': ['ok', 'pending']})).sort('timestamp')
+
+        if comment <= comments.count():
+            return comments.all()[comment-1].slug
+
+    def comment_link(self, m):
+        """Convert a Trac-style comment url to it's equivalent Allura url."""
+        text, ticket, comment = m.groups()
+        ticket = ticket.replace('\n', '')
+        text = text.replace('\n', ' ')
+        slug = self.get_slug_by_id(ticket, comment)
+        if slug:
+            return '[%s](%s/#%s)' % (text, ticket, slug)
+        else:
+            return text
+
+    def brackets_escaping(self, m):
+        """Escape double brackets."""
+        return '[\[%s\]]' % m.groups()[0]
+
+    def link_processing(self, text):
+        """Fix up links in text imported from Trac::
+
+            * Convert comment anchors from Trac ids to Allura slugs
+            * Convert absolute links to Trac tickets into relative links to
+              their Allura counterparts
+            * Escape double-brackets
+
+        """
+        comment_pattern = re.compile('\[(\S*\s*\S*)\]\(\S*/(\d+\n*\d*)#comment:(\d+)\)')
+        ticket_pattern = re.compile('(?<=\])\(\S*ticket/(\d+)\)')
+        brackets_pattern = re.compile('\[\[(.*)\]\]')
+
+        text = comment_pattern.sub(self.comment_link, text)
+        text = ticket_pattern.sub(self.ticket_link, text)
+        text = brackets_pattern.sub(self.brackets_escaping, text)
+        return text
+
+    def comment_processing(self, comment_text):
+        """Modify comment text before comment is created."""
+        return self.link_processing(comment_text)
+
+    def description_processing(self, description_text):
+        """Modify ticket description before ticket is created."""
+        return self.link_processing(description_text)
+

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/63cbafe8/ForgeTracker/forgetracker/import_support.py
----------------------------------------------------------------------
diff --git a/ForgeTracker/forgetracker/import_support.py b/ForgeTracker/forgetracker/import_support.py
index 053c925..b5daa12 100644
--- a/ForgeTracker/forgetracker/import_support.py
+++ b/ForgeTracker/forgetracker/import_support.py
@@ -18,7 +18,6 @@
 #-*- python -*-
 import logging
 import json
-import re
 from datetime import datetime
 from cStringIO import StringIO
 
@@ -205,7 +204,7 @@ class ImportSupport(object):
                 new_f, conv = transform
                 remapped[new_f] = conv(v)
 
-        description = h.really_unicode(self.link_processing(remapped['description']))
+        description = h.really_unicode(self.description_processing(remapped['description']))
         creator = owner = ''
         if ticket_dict.get('submitter') and not remapped.get('reported_by_id'):
             creator = u'*Originally created by:* {0}\n'.format(
@@ -235,54 +234,18 @@ class ImportSupport(object):
         ticket.update(remapped)
         return ticket
 
-    def ticket_link(self, m):
-        return '(%s)' % m.groups()[0]
-
-    def ticket_bracket_link(self, m):
-        text = m.groups()[0]
-        return '[\[%s\]](%s:#%s)' % (text, c.app.config.options.mount_point, text)
-
-    def get_slug_by_id(self, ticket, comment):
-        comment = int(comment)
-        ticket = TM.Ticket.query.get(app_config_id=c.app.config._id,
-                                     ticket_num=int(ticket))
-        if not ticket:
-            return ''
-        comments = ticket.discussion_thread.post_class().query.find(dict(
-            discussion_id=ticket.discussion_thread.discussion_id,
-            thread_id=ticket.discussion_thread._id,
-            status={'$in': ['ok', 'pending']})).sort('timestamp')
-
-        if comment <= comments.count():
-            return comments.all()[comment-1].slug
-
-    def comment_link(self, m):
-        text, ticket, comment = m.groups()
-        ticket = ticket.replace('\n', '')
-        text = text.replace('\n', ' ')
-        slug = self.get_slug_by_id(ticket, comment)
-        if slug:
-            return '[%s](%s/#%s)' % (text, ticket, slug)
-        else:
-            return text
-
-    def brackets_escaping(self, m):
-        return '[\[%s\]]' % m.groups()[0]
-
-    def link_processing(self, text):
-        comment_pattern = re.compile('\[(\S*\s*\S*)\]\(\S*/(\d+\n*\d*)#comment:(\d+)\)')
-        ticket_pattern = re.compile('(?<=\])\(\S*ticket/(\d+)\)')
-        brackets_pattern = re.compile('\[\[(.*)\]\]')
+    def comment_processing(self, comment_text):
+        """Modify comment text before comment is created."""
+        return comment_text
 
-        text = comment_pattern.sub(self.comment_link, text)
-        text = ticket_pattern.sub(self.ticket_link, text)
-        text = brackets_pattern.sub(self.brackets_escaping, text)
-        return text
+    def description_processing(self, description_text):
+        """Modify ticket description before ticket is created."""
+        return description_text
 
     def make_comment(self, thread, comment_dict):
         ts = self.parse_date(comment_dict['date'])
         author_id = self.get_user_id(comment_dict['submitter'])
-        text = h.really_unicode(self.link_processing(comment_dict['comment']))
+        text = h.really_unicode(self.comment_processing(comment_dict['comment']))
         if not author_id and comment_dict['submitter']:
             text = u'*Originally posted by:* {0}\n\n{1}'.format(
                     h.really_unicode(comment_dict['submitter']), text)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/63cbafe8/ForgeTracker/forgetracker/tests/functional/test_import.py
----------------------------------------------------------------------
diff --git a/ForgeTracker/forgetracker/tests/functional/test_import.py b/ForgeTracker/forgetracker/tests/functional/test_import.py
index 30fff74..da26ecf 100644
--- a/ForgeTracker/forgetracker/tests/functional/test_import.py
+++ b/ForgeTracker/forgetracker/tests/functional/test_import.py
@@ -30,8 +30,6 @@ from allura import model as M
 from allura.lib import helpers as h
 from alluratest.controller import TestRestApiBase
 from allura.tests import decorators as td
-from forgetracker import model as TM
-from forgetracker.import_support import ImportSupport
 
 
 class TestImportController(TestRestApiBase):
@@ -202,64 +200,3 @@ class TestImportController(TestRestApiBase):
                         assert milestone['complete'] == False
                     if milestone['name'] == 'closed_milestone':
                         assert milestone['complete'] == True
-
-    @td.with_tracker
-    def test_link_processing(self):
-        import_support = ImportSupport()
-        import_support.get_slug_by_id = lambda ticket, comment: '123'
-        result = import_support.link_processing('''test link [[2496]](http://testlink.com)
-                                       test ticket ([#201](http://sourceforge.net/apps/trac/sourceforge/ticket/201))
-                                       Replying to [someuser](http://sourceforge.net/apps/trac/sourceforge/ticket/204#comment:1)
-                                       #200 unchanged''')
-
-        assert "test link [\[2496\]](http://testlink.com)" in result
-        assert 'Replying to [someuser](204/#123)' in result
-        assert 'test link [\[2496\]](http://testlink.com)' in result
-        assert 'test ticket ([#201](201))' in result
-        assert '#200 unchanged' in result, result
-
-    @td.with_tracker
-    def test_links(self):
-        api_ticket = M.ApiTicket(user_id=c.user._id, capabilities={'import': ['Projects','test']},
-                                 expires=datetime.utcnow() + timedelta(days=1))
-        ming.orm.session(api_ticket).flush()
-        self.set_api_token(api_ticket)
-
-        doc_text = open(os.path.dirname(__file__) + '/data/sf.json').read()
-        r = self.api_post('/rest/p/test/bugs/perform_import',
-                      doc=doc_text, options='{"user_map": {"hinojosa4": "test-admin", "ma_boehm":
"test-user"}}')
-        assert r.json['status'], r.json
-
-        r = self.app.get('/p/test/bugs/204/')
-        ticket = TM.Ticket.query.get(app_config_id=c.app.config._id,
-                                    ticket_num=204)
-        slug = ticket.discussion_thread.post_class().query.find(dict(
-            discussion_id=ticket.discussion_thread.discussion_id,
-            thread_id=ticket.discussion_thread._id,
-            status={'$in': ['ok', 'pending']})).sort('timestamp').all()[0].slug
-
-        assert '[test comment](204/#%s)' % slug in r
-        assert 'test link [\[2496\]](http://testlink.com)' in r
-        assert 'test ticket ([#201](201))' in r
-
-    @td.with_tracker
-    def test_slug(self):
-        api_ticket = M.ApiTicket(user_id=c.user._id, capabilities={'import': ['Projects','test']},
-                                 expires=datetime.utcnow() + timedelta(days=1))
-        ming.orm.session(api_ticket).flush()
-        self.set_api_token(api_ticket)
-
-        doc_text = open(os.path.dirname(__file__) + '/data/sf.json').read()
-        r = self.api_post('/rest/p/test/bugs/perform_import',
-                      doc=doc_text, options='{"user_map": {"hinojosa4": "test-admin", "ma_boehm":
"test-user"}}')
-        assert r.json['status'], r.json
-        ticket = TM.Ticket.query.get(app_config_id=c.app.config._id,
-                                    ticket_num=204)
-        comments = ticket.discussion_thread.post_class().query.find(dict(
-            discussion_id=ticket.discussion_thread.discussion_id,
-            thread_id=ticket.discussion_thread._id,
-            status={'$in': ['ok', 'pending']})).sort('timestamp').all()
-
-        import_support = ImportSupport()
-        assert_equal(import_support.get_slug_by_id('204', '1'), comments[0].slug)
-        assert_equal(import_support.get_slug_by_id('204', '2'), comments[1].slug)


Mime
View raw message