1. Tetsuya Morimoto
  2. trac.plugins.ticketref

Commits

Tetsuya Morimoto  committed 762716e

changed to use CommitTicketUpdater to parse and get ticket id in comment

  • Participants
  • Parent commits d73c302
  • Branches default

Comments (0)

Files changed (4)

File tests/test_api_ticketref_plugin.py

View file
  • Ignore whitespace
 
 import pytest
 from trac.ticket.model import Ticket
+from tracopt.ticket.commit_updater import CommitTicketUpdater
 
 from ticketref.model import TICKETREF
 
 from utils import (create_ticket, create_tickets,
                    make_simple_ticketref, make_multi_ticketref)
 
+
+# CommitTicketUpdater settings for test
+CommitTicketUpdater.commands_close = "close closed closes fix fixed fixes"
+CommitTicketUpdater.commands_refs = "ref ref. refs refer reference dup see"
+
+
+@pytest.mark.parametrize(("message", "except_ids", "expected"), [
+    (None, None, set([])),
+    (u"", None, set([])),
+    (u"refs. #1", None, set([1])),
+    (u"refs. #1ab", None, set([1])),
+    (u"refs. a#1b", None, set([])),
+    (u"some refer #1, #3, some", None, set([1, 3])),
+    (u"some refer #1,#3,#5 # 7 some", None, set([1, 3, 5])),
+    (u"ref #1,#3  ref, #5 #7 refs", None, set([1, 3, 5, 7])),
+    (u"ref #1,#3  ref #5, #7", None, set([1, 3, 5, 7])),
+    (u"refs#1", None, set([1])),
+    (u"reference#1 #3, #5", None, set([1, 3, 5])),
+    (u"adding.\nrefs #1", None, set([1])),
+    (u"adding.\nrefs #1 #3, #5", None, set([1, 3, 5])),
+    (u"adding.\nsecond\nrefs #2", None, set([2])),
+    (u"except ids\nrefs #1 #3", [3], set([1])),
+    (u"except ids\nrefs #1 #3 #5", [1, 5], set([3])),
+    (u"ref. #21", [23], set([21])),
+    (u"ref. #21 #22", [23, 24], set([21, 22])),
+    (u"ref. #3 #4", [1], set([3, 4])),
+    (u"ref. #3 #4", [1, 3], set([4])),
+    (u"ref. #6\ndup #7\nsee #8", None, set([6, 7, 8])),
+    (u"ref. #6\ndup #7\nsee #8", [6], set([7, 8])),
+    (u"fix #7\n - test\nfixed #8\n - test\nfixes #9", None, set([7, 8, 9])),
+])
+def test_tref_get_refs(tref, message, except_ids, expected):
+    assert expected == tref._get_refs(message, except_ids)
+
 def test_tref_has_ticket_refs(env, tref):
     ticket = create_ticket(env)
     assert not tref.has_ticket_refs(ticket)

File tests/test_utils.py

View file
  • Ignore whitespace
 
 from ticketref import utils
 
-@pytest.mark.parametrize(("comment", "except_ids", "expected"), [
-    (None, None, set([])),
-    (u"", None, set([])),
-    (u"refs. #1", None, set([1])),
-    (u"refs. #1ab", None, set([1])),
-    (u"refs. a#1b", None, set([])),
-    (u"some refer #1, #3, some", None, set([1, 3])),
-    (u"some refer #1,#3,#5 # 7 some", None, set([1, 3, 5])),
-    (u"ref #1,#3  ref, #5 #7 refs", None, set([1, 3])),
-    (u"ref #1,#3  ref #5, #7", None, set([1, 3])),
-    (u"refs#1", None, set([])),
-    (u"reference#1 #3, #5", None, set([1, 3, 5])),
-    (u"adding.\\nrefs #1", None, set([1])),
-    (u"adding.\\nrefs #1 #3, #5", None, set([1, 3, 5])),
-    (u"adding.\\nsecond\\nrefs #2", None, set([2])),
-    (u"except ids\\nrefs #1 #3", [3], set([1])),
-    (u"except ids\\nrefs #1 #3 #5", [1, 5], set([3])),
-    (u"ref. #21", [23], set([21])),
-    (u"ref. #21 #22", [23, 24], set([21, 22])),
-    (u"ref. #3 #4", [1], set([3, 4])),
-    (u"ref. #3 #4", [1, 3], set([4])),
-])
-def test_get_refs_in_comment(comment, except_ids, expected):
-    assert expected == utils.get_refs_in_comment(comment, except_ids)
-
-
-@pytest.mark.parametrize(("text", "expected"), [
-    ("", set([])),
-    ("text only", set([])),
-    ("#1", set([1])),
-    ("refs #1", set([1])),
-    ("refs #1, ", set([1])),
-    ("refs #1, #2", set([1, 2])),
-    ("refs #1,#2,   #3 ids", set([1, 2, 3])),
-])
-def test_get_ref_ids_in_comment(text, expected):
-    assert expected == utils.get_ref_ids_in_comment(text)
-
 
 @pytest.mark.parametrize(("refs_text", "expected"), [
     (None, set([])),

File ticketref/api.py

View file
  • Ignore whitespace
 from trac.ticket.api import ITicketChangeListener, ITicketManipulator
 from trac.ticket.model import Ticket
 from trac.util.translation import domain_functions
+from tracopt.ticket.commit_updater import CommitTicketUpdater
 
 from model import CUSTOM_FIELDS, TICKETREF, TicketLinks
-from utils import cnv_sorted_refs, get_refs_in_comment
+from utils import cnv_sorted_refs
 
 _, add_domain = domain_functions("ticketref", ("_", "add_domain"))
 
     # ITicketChangeListener methods
     def ticket_created(self, ticket):
         links = None
-        desc_refs = get_refs_in_comment(ticket["description"])
+        desc_refs = self._get_refs(ticket["description"])
         if desc_refs:
             ticket[TICKETREF] = cnv_sorted_refs(ticket[TICKETREF], desc_refs)
             links = TicketLinks(self.env, ticket)
         links = None
         need_change = TICKETREF in old_values
 
-        com_refs = get_refs_in_comment(comment, [ticket.id])
+        com_refs = self._get_refs(comment, [ticket.id])
         if com_refs:
             links = TicketLinks(self.env, ticket)
             links.add_reference(com_refs)
             except Exception, err:
                 self.log.error("TicketRefsPlugin: ticket_deleted %s" % err)
 
+    def _get_refs(self, message, except_ids=None):
+        ref_ids = set([])
+        ticket_updator = self._get_commit_ticket_updator()
+        tickets = ticket_updator._parse_message(message or u"")
+        if tickets:
+            ref_ids = set(tickets.keys())
+        if except_ids:
+            exc_ids = [i for i in ref_ids if i in except_ids]
+            for id_ in exc_ids:
+                ref_ids.remove(id_)
+        return ref_ids
+
+    def _get_commit_ticket_updator(self):
+        ticket_updator = self.compmgr.components.get(CommitTicketUpdater)
+        if not ticket_updator:
+            ticket_updator = CommitTicketUpdater(self.compmgr)
+            self.compmgr.components[CommitTicketUpdater] = ticket_updator
+        return ticket_updator
+
     # ITicketManipulator methods
     def prepare_ticket(self, req, ticket, fields, actions):
         pass

File ticketref/utils.py

View file
  • Ignore whitespace
 # -*- coding: utf-8 -*-
 
-import re
-
-_RE_REFS_WITH_IDS = re.compile(r"""
-    (?P<ref_text>(ref.*?)\s+(\#\d+[,|\s]*){1,})
-""", re.U | re.M | re.X)
-
-def get_refs_in_comment(comment, except_ids=None):
-    """ pick out reference text and convert text to ids
-    >>> get_refs_in_comment(u"refs. #1")
-    set([1])
-    >>> get_refs_in_comment(u"some refer #1, #3, some")
-    set([1, 3])
-    >>> get_refs_in_comment(u"ref. #3 #4", [1, 3])
-    set([4])
-    """
-    ref_ids = set([])
-    match = re.search(_RE_REFS_WITH_IDS, comment or u"")
-    if match:
-        ref_text = match.groupdict().get("ref_text")
-        ref_ids = get_ref_ids_in_comment(ref_text)
-        if except_ids:
-            exc_ids = [i for i in ref_ids if i in except_ids]
-            for id_ in exc_ids:
-                ref_ids.remove(id_)
-    return ref_ids
-
-_RE_TICKET_IDS = re.compile(r"(#\d+)", re.U)
-
-def get_ref_ids_in_comment(text):
-    """ return only id numbers with list
-    >>> get_ref_ids_in_comment("")
-    set([])
-    >>> get_ref_ids_in_comment("refs #1, #2")
-    set([1, 2])
-    """
-    ref_ids = re.findall(_RE_TICKET_IDS, text)
-    return set([int(id_.replace("#", "")) for id_ in ref_ids])
-
 def cnv_text2list(refs_text):
     """ convert text to list
     >>> cnv_text2list(None)