incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [05/23] git commit: [#6441] ticket:398 fixed hyperlinks in tickets imported from trac
Date Wed, 31 Jul 2013 19:14:25 GMT
[#6441]  ticket:398 fixed hyperlinks in tickets imported from trac


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

Branch: refs/heads/cj/6461
Commit: b3fea697bf281649e6dc8cd63a878f9cb45859f3
Parents: fff526f
Author: Yuriy Arhipov <yuriyarhipovua@yandex.ru>
Authored: Fri Jul 19 14:39:19 2013 +0400
Committer: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Committed: Wed Jul 31 12:21:13 2013 +0000

----------------------------------------------------------------------
 ForgeTracker/forgetracker/import_support.py     | 37 +++++++++++-
 .../forgetracker/tests/functional/data/sf.json  |  6 ++
 .../tests/functional/test_import.py             | 59 ++++++++++++++++++++
 3 files changed, 101 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/b3fea697/ForgeTracker/forgetracker/import_support.py
----------------------------------------------------------------------
diff --git a/ForgeTracker/forgetracker/import_support.py b/ForgeTracker/forgetracker/import_support.py
index a94e70c..268f727 100644
--- a/ForgeTracker/forgetracker/import_support.py
+++ b/ForgeTracker/forgetracker/import_support.py
@@ -18,6 +18,7 @@
 #-*- python -*-
 import logging
 import json
+import re
 from datetime import datetime
 from cStringIO import StringIO
 
@@ -211,9 +212,43 @@ class ImportSupport(object):
         ticket.update(remapped)
         return ticket
 
+    def ticket_link(self, m):
+        return '(%s)' % m.groups()[0]
+
+    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):
+        ticket, comment = m.groups()
+        return '(%s#%s)' % (ticket, self.get_slug_by_id(ticket, comment))
+
+    def brackets_escaping(self, m):
+        return '[%s]' % m.groups()[0]
+
+    def link_processing(self, text):
+        comment_pattern = re.compile('\(\S*/(\d+)#comment:(\d+)\)')
+        ticket_pattern = re.compile('(?<=\])\(\S*ticket/(\d+)\)')
+        brackets_pattern = re.compile('\[\[(.*)\]\]')
+
+        text = comment_pattern.sub(self.comment_link, text.replace('\n', ''))
+        text = ticket_pattern.sub(self.ticket_link, text)
+        text = brackets_pattern.sub(self.brackets_escaping, text)
+        return text
+
     def make_comment(self, thread, comment_dict):
         ts = self.parse_date(comment_dict['date'])
-        comment = thread.post(text=comment_dict['comment'], timestamp=ts)
+        comment = thread.post(text=self.link_processing(comment_dict['comment']), timestamp=ts)
         comment.author_id = self.get_user_id(comment_dict['submitter'])
         comment.import_id = c.api_token.api_key
 

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/b3fea697/ForgeTracker/forgetracker/tests/functional/data/sf.json
----------------------------------------------------------------------
diff --git a/ForgeTracker/forgetracker/tests/functional/data/sf.json b/ForgeTracker/forgetracker/tests/functional/data/sf.json
index 58e322c..5a11379 100644
--- a/ForgeTracker/forgetracker/tests/functional/data/sf.json
+++ b/ForgeTracker/forgetracker/tests/functional/data/sf.json
@@ -41,6 +41,12 @@
               "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)",
+              "date": "2009-07-21T15:44:32Z",
+              "submitter": "ctsai"
             }
           ], 
           "date": "2009-04-13T08:49:13Z", 

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/b3fea697/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 06fde25..202c757 100644
--- a/ForgeTracker/forgetracker/tests/functional/test_import.py
+++ b/ForgeTracker/forgetracker/tests/functional/test_import.py
@@ -24,10 +24,13 @@ from nose.tools import assert_equal
 
 import ming
 from pylons import app_globals as g
+from pylons import tmpl_context as c
 
 from allura import model as M
 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):
 
@@ -164,3 +167,59 @@ class TestImportController(TestRestApiBase):
         assert ticket_json['summary'] in r
         r = self.app.get('/p/test/bugs/')
         assert ticket_json['summary'] in r
+
+    @td.with_tracker
+    def test_link_processing(self):
+        import_support = ImportSupport()
+        result = import_support.link_processing('''test link [[2496]](http://testlink.com)
+                                       test ticket ([#201](http://sourceforge.net/apps/trac/sourceforge/ticket/201))
+                                       [test comment](http://sourceforge.net/apps/trac/sourceforge/ticket/204#comment:1)''')
+
+        assert "test link [2496](http://testlink.com)" in result
+        assert '[test comment](204#)' in result
+        assert 'test link [2496](http://testlink.com)' in result
+        assert 'test ticket ([#201](201))' in result
+
+    @td.with_tracker
+    def test_links(self):
+        api_ticket = M.ApiTicket(user_id=self.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()
+        self.api_post('/rest/p/test/bugs/perform_import',
+                      doc=doc_text, options='{"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
+
+    @td.with_tracker
+    def test_slug(self):
+        api_ticket = M.ApiTicket(user_id=self.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()
+        self.api_post('/rest/p/test/bugs/perform_import',
+                      doc=doc_text, options='{"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 = 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