Skip to content

Commit

Permalink
Merge pull request #26 from spacetelescope/eslavich-escape-github-men…
Browse files Browse the repository at this point in the history
…tions

Escape strings from Jira content that look like GitHub user mentions
  • Loading branch information
eslavich committed Aug 10, 2020
2 parents 9c65ead + 1f5df48 commit 3f36b87
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
0.2.2 (2020-08-10)
------------------

- Escape strings from Jira that would be interpreted by GitHub
as user mentions. [#26]

0.2.1 (2020-06-25)
------------------

Expand Down
11 changes: 11 additions & 0 deletions jirahub/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ class Formatter:
URL_WITH_TEXT_RE = re.compile(r"\[(.*?)\|(http.*?)\]")
URL_RE = re.compile(r"(\s|^)\[?(http.*?)\]?(\s|$)")
USER_MENTION_RE = re.compile(r"\[~(.+?)\]")
GITHUB_USER_MENTION_RE = re.compile(r"(^|\s)@(\w+?)\b")

def __init__(self, config, url_helper, jira_client):
self._config = config
Expand Down Expand Up @@ -431,7 +432,17 @@ def _format_user_mention(self, match):

return self.format_link(url, link_text)

def _format_github_user_mention(self, match):
# Insert an invisible character between the @ and the username
# to prevent GitHub from mentioning a user. U+2063 is the
# "invisible separator" code point.
return match.group(1) + "@\u2063" + match.group(2)

def _format_content(self, content):
# Perform this transformation early since we don't want it to end up escaping
# intentional user mentions:
content = Formatter.GITHUB_USER_MENTION_RE.sub(self._format_github_user_mention, content)

content = Formatter.HASH_NUMBER_RE.sub(lambda match: f"#⁠{match.group(1)}", content)
content = Formatter.H1_RE.sub("# ", content)
content = Formatter.H2_RE.sub("## ", content)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,10 @@ def test_format_link(self, formatter, url, link_text, expected):
"This is a link to a GitHub user profile: [https://github.com/username123]",
"This is a link to a GitHub user profile: @username123",
),
(
"This looks like a GitHub user mention but should be escaped: @username123",
"This looks like a GitHub user mention but should be escaped: @\u2063username123",
),
(
"This is a body with *bold*, _italic_, and {{monospaced}} text.",
"This is a body with **bold**, *italic*, and `monospaced` text.",
Expand Down

0 comments on commit 3f36b87

Please sign in to comment.