Skip to content

Commit

Permalink
Fix corner cases in recognizing links to issues and revisions.
Browse files Browse the repository at this point in the history
Add the patterns for these two in ``config.py`` so they can be
overridden.

Also add the template to use when recognizing a link to another issue
there.

Fixes http://plone.org/products/poi/issues/262
  • Loading branch information
mauritsvanrees committed Oct 18, 2013
1 parent 49ea2eb commit a5df1ca
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
7 changes: 6 additions & 1 deletion CHANGES.rst
Expand Up @@ -5,7 +5,12 @@ Changelog for Poi
2.2.6 (unreleased)
------------------

- Nothing changed yet.
- Fix corner cases in recognizing links to issues and revisions. Add
the patterns for these two in ``config.py`` so they can be
overridden. Also add the template to use when recognizing a link to
another issue there.
Fixes http://plone.org/products/poi/issues/262
[maurits]


2.2.5 (2013-08-22)
Expand Down
8 changes: 8 additions & 0 deletions Products/Poi/config.py
Expand Up @@ -18,3 +18,11 @@
# issue/response text.
ISSUE_MIME_TYPES = ('text/x-web-intelligent', 'text/plain')
DEFAULT_ISSUE_MIME_TYPE = 'text/x-web-intelligent'
# Patterns used for recognizing links to issues and revisions:
ISSUE_RECOGNITION_PATTERNS = \
[r'\B#[1-9][0-9]*\b', r'\bissue:[1-9][0-9]*\b',
r'\bticket:[1-9][0-9]*\b', r'\bbug:[1-9][0-9]*\b']
REVISION_RECOGNITION_PATTERNS = \
[r'\br[0-9]+\b', r'\bchangeset:[0-9]+\b', r'\B\[[0-9]+\]\B']
# Template to use when recognizing a link to another issue:
ISSUE_LINK_TEMPLATE = '<a href="../%(bug)s">%(linktext)s</a>'
12 changes: 4 additions & 8 deletions Products/Poi/content/PoiTracker.py
Expand Up @@ -43,6 +43,8 @@
from Products.Poi import PoiMessageFactory as _
from Products.Poi import permissions
from Products.Poi.config import PROJECTNAME
from Products.Poi.config import ISSUE_RECOGNITION_PATTERNS
from Products.Poi.config import REVISION_RECOGNITION_PATTERNS
from Products.Poi.interfaces import ITracker
from Products.Poi.utils import linkBugs
from Products.Poi.utils import linkSvn
Expand Down Expand Up @@ -321,15 +323,9 @@ def linkDetection(self, text):
issuefolder = self.restrictedTraverse('@@issuefolder')
issues = catalog.searchResults(issuefolder.buildIssueSearchQuery(None))
ids = frozenset([issue.id for issue in issues])

# XXX/TODO: should these patterns live in the config file?
text = linkBugs(text, ids,
['#[1-9][0-9]*', 'issue:[1-9][0-9]*',
'ticket:[1-9][0-9]*', 'bug:[1-9][0-9]*'])
text = linkBugs(text, ids, ISSUE_RECOGNITION_PATTERNS)
svnUrl = self.getSvnUrl()
text = linkSvn(text, svnUrl,
['r[0-9]+', 'changeset:[0-9]+', '\[[0-9]+\]'])

text = linkSvn(text, svnUrl, REVISION_RECOGNITION_PATTERNS)
return text

security.declareProtected(permissions.View, 'isUsingReleases')
Expand Down
23 changes: 21 additions & 2 deletions Products/Poi/tests/linkdetection.txt
Expand Up @@ -45,6 +45,8 @@ Look for problems::
True
>>> getNumberFromString('#007')
'7'
>>> getNumberFromString('my.html#7')
'7'

With more numbers, the first one is taken::

Expand All @@ -64,6 +66,7 @@ text with possibly links::

Now we test this with patterns::

>>> from Products.Poi.config import ISSUE_RECOGNITION_PATTERNS
>>> patterns = ['test']
>>> linkBugs(text, ids, patterns)
'issue:1 #2 r3 [4] ticket:5.'
Expand All @@ -76,13 +79,17 @@ Now we test this with patterns::
>>> patterns = ['[[1-9][0-9]*]']
>>> linkBugs(text, ids, patterns)
'issue:1 #2 r3 <a href="../4">[4]</a> ticket:5.'
>>> patterns = ['#[1-9][0-9]*', 'issue:[1-9][0-9]*', 'ticket:[1-9][0-9]*', 'bug:[1-9][0-9]*']
>>> patterns = ISSUE_RECOGNITION_PATTERNS
>>> linkBugs(text, ids, patterns)
'<a href="../1">issue:1</a> <a href="../2">#2</a> r3 [4] <a href="../5">ticket:5</a>.'
>>> linkBugs(text, [], patterns)
'issue:1 #2 r3 [4] ticket:5.'
>>> linkBugs("#9#9#9", ['9'], ["#9"])
'<a href="../9">#9</a><a href="../9">#9</a><a href="../9">#9</a>'
>>> linkBugs("foo#9 #9a #99 #9", ['9'], patterns)
'foo#9 #9a #99 <a href="../9">#9</a>'
>>> linkBugs("noticket:5 ticket:5", ['5'], patterns)
'noticket:5 <a href="../5">ticket:5</a>'


linkSvn
Expand All @@ -92,6 +99,7 @@ Replace patterns with links to changesets in a repository. It does
not need to be subversion of course. Specify something to test with::

>>> from Products.Poi.utils import linkSvn
>>> from Products.Poi.config import REVISION_RECOGNITION_PATTERNS
>>> text = "r1 #22 changeset:333 [4444]"
>>> svnUrl = "someurl?rev=%(rev)s"

Expand All @@ -103,11 +111,22 @@ And test it::
>>> patterns = ["r1"]
>>> linkSvn(text, svnUrl, patterns)
'<a href="someurl?rev=1">r1</a> #22 changeset:333 [4444]'
>>> patterns = ['r[0-9]+', 'changeset:[0-9]+', '\[[0-9]+\]']
>>> patterns = REVISION_RECOGNITION_PATTERNS
>>> linkSvn(text, svnUrl, patterns)
'<a href="someurl?rev=1">r1</a> #22 <a href="someurl?rev=333">changeset:333</a> <a href="someurl?rev=4444">[4444]</a>'

Of course if you want to be silly, you can::

>>> linkSvn(text, "here", patterns)
'<a href="here">r1</a> #22 <a href="here">changeset:333</a> <a href="here">[4444]</a>'

Test with multiple similar revisions. This is more a text of our
regular expression skills::

>>> text = "r1 r2 norevisionr3 r4nope (r5) r6."
>>> linkSvn(text, svnUrl, patterns)
'<a href="someurl?rev=1">r1</a> <a href="someurl?rev=2">r2</a> norevisionr3 r4nope (<a href="someurl?rev=5">r5</a>) <a href="someurl?rev=6">r6</a>.'
>>> text = "[1] link[2] [3]."
>>> linkSvn(text, svnUrl, patterns)
'<a href="someurl?rev=1">[1]</a> link[2] <a href="someurl?rev=3">[3]</a>.'

5 changes: 3 additions & 2 deletions Products/Poi/utils.py
@@ -1,5 +1,7 @@
import re

from .config import ISSUE_LINK_TEMPLATE


def getNumberFromString(linktext):
"""
Expand Down Expand Up @@ -30,8 +32,7 @@ def linkBugs(text, ids, patterns):
bug = getNumberFromString(linktext)

if bug is not None and bug in ids:
# XXX/TODO: this is a little too hardcoded for my taste
link = '<a href="../' + bug + '">' + linktext + '</a>'
link = ISSUE_LINK_TEMPLATE % dict(bug=bug, linktext=linktext)
text = text[0:pos] + link + text[res.end():]
pos += len(link)
else:
Expand Down

0 comments on commit a5df1ca

Please sign in to comment.