Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion bedevere/bpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def check_hyperlink(match):
"""The span checking of regex matches takes care of cases like bpo-123 [bpo-123]…"""
issue = match.group("issue")
markdown_link_re = re.compile(r"""
\[\s*bpo-(?P<issue>{issue})\s*\]
\[\s*bpo-(?P<issue>{issue})\s*\]
\(\s*https://bugs.python.org/issue{issue}\s*\)""".format(issue=issue),
re.VERBOSE)
html_link_re = re.compile(r""" <a
Expand All @@ -140,17 +140,30 @@ def create_hyperlink_in_comment_body(body):
new_body = ""
leftover_body = body
ISSUE_RE = re.compile(r"bpo-(?P<issue>\d+)")
CODE_BLOCK = re.compile(r"(`{3}([^`]*?)`{3})|(^`([^`]*?)`$)") # leave code blocks, inline or otherwise, untouched

while True:
match = ISSUE_RE.search(leftover_body)
codeblock_match = CODE_BLOCK.search(leftover_body)

if match is None:
break

presence = check_hyperlink(match)
if codeblock_match:
temp = leftover_body
new_body += leftover_body[:codeblock_match.end()]
# new_body += leftover_body[codeblock_match.end():] leftover_body = leftover_body[codeblock_match.end():]
new_body = temp
break
if presence is False:
new_body = new_body + leftover_body[:match.start()]
leftover_body = leftover_body[match.end():]
new_body = new_body + match.expand(r"[bpo-\g<issue>](https://bugs.python.org/issue\g<issue>)")
else:
new_body = new_body + leftover_body[:presence]
leftover_body = leftover_body[presence:]

new_body = new_body + leftover_body

return new_body
65 changes: 59 additions & 6 deletions tests/test_bpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,8 @@ async def test_set_pull_request_body_already_hyperlinked_bpo(action):
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"issue_url": "https://api.github.com/repos/blah/blah/issues/comments/123456",
"body": ("bpo-123"
"[bpo-123](https://bugs.python.org/issue123)"
"<a href='https://bugs.python.org/issue123'>bpo-123</a>"
)
"[bpo-123](https://bugs.python.org/issue123)"
"<a href='https://bugs.python.org/issue123'>bpo-123</a>")
},
"changes": {"stuff": "thingy"}
}
Expand All @@ -407,9 +406,8 @@ async def test_set_comment_body_already_hyperlinked_bpo(action):
"comment": {
"url": "https://api.github.com/repos/blah/blah/issues/comments/123456",
"body": ("bpo-123"
"[bpo-123](https://bugs.python.org/issue123)"
"<a href='https://bugs.python.org/issue123'>bpo-123</a>"
)
"[bpo-123](https://bugs.python.org/issue123)"
"<a href='https://bugs.python.org/issue123'>bpo-123</a>")
}
}

Expand All @@ -419,3 +417,58 @@ async def test_set_comment_body_already_hyperlinked_bpo(action):
body_data = gh.patch_data
assert body_data["body"].count("[bpo-123](https://bugs.python.org/issue123)") == 2
assert "123456" in gh.patch_url


@pytest.mark.asyncio
@pytest.mark.parametrize("action", ["opened", "edited"])
async def test_bpo_text_within_codeblocks_in_comments(action):
data = {
"action": action,
"pull_request": {
"title": "[3.6] bpo-12345: some issue",
"url": "https://api.github.com/repos/python/cpython/pulls/4321",
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"issue_url": "https://api.github.com/repos/blah/blah/issues/comments/123456",
"body": ("bpo-123"
"```bpo-123```"
"`bpo-123`"
"[bpo-123](https://bugs.python.org/issue123)"
"<a href='https://bugs.python.org/issue123'>bpo-123</a>")
},
"changes": {"stuff": "thingy"}
}

event = sansio.Event(data, event="pull_request", delivery_id="123123")
gh = FakeGH()
await bpo.router.dispatch(event, gh)
body_data = gh.patch_data
assert body_data["body"].count("[bpo-123](https://bugs.python.org/issue123)") == 2
assert "123456" in gh.patch_url


@pytest.mark.asyncio
@pytest.mark.parametrize("action", ["opened", "edited"])
async def test_bpo_text_within_codeblocks_in_pull_request(action):
data = {
"action": action,
"pull_request": {
"title": "[3.6] bpo-12345: some issue",
"url": "https://api.github.com/repos/python/cpython/pulls/4321",
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"issue_url": "https://api.github.com/repos/blah/blah/issues/comments/123456",
"body": ("bpo-123"
"```bpo-123```"
"`bpo-123`"
"[bpo-123](https://bugs.python.org/issue123)"
"<a href='https://bugs.python.org/issue123'>bpo-123</a>")
},
"changes": {"stuff": "thingy"}
}

event = sansio.Event(data, event="pull_request", delivery_id="123123")
gh = FakeGH()
await bpo.router.dispatch(event, gh)
body_data = gh.patch_data
assert body_data["body"].count("[bpo-123](https://bugs.python.org/issue123)") == 2
assert "123456" in gh.patch_url
assert 1 == 2