Skip to content

Commit 886ffef

Browse files
committed
test bpo in codeblocks
1 parent 8a9ee48 commit 886ffef

2 files changed

Lines changed: 70 additions & 11 deletions

File tree

‎bedevere/bpo.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,30 @@ def create_hyperlink_in_comment_body(body):
140140
new_body = ""
141141
leftover_body = body
142142
ISSUE_RE = re.compile(r"bpo-(?P<issue>\d+)")
143-
144-
# leave code blocks, inline or otherwise, untouched
145-
CODE_BLOCK = re.compile(r"(`{1,3})(.|\s)*(`{1,3})")
146-
if CODE_BLOCK.search(body):
147-
return body
143+
CODE_BLOCK = re.compile(r"(`{3}([^`]*?)`{3})|(^`([^`]*?)`$)") # leave code blocks, inline or otherwise, untouched
148144

149145
while True:
150146
match = ISSUE_RE.search(leftover_body)
147+
codeblock_match = CODE_BLOCK.search(leftover_body)
148+
151149
if match is None:
152150
break
151+
153152
presence = check_hyperlink(match)
153+
if codeblock_match:
154+
temp = leftover_body
155+
new_body += leftover_body[:codeblock_match.end()]
156+
# new_body += leftover_body[codeblock_match.end():] leftover_body = leftover_body[codeblock_match.end():]
157+
new_body = temp
158+
break
154159
if presence is False:
155160
new_body = new_body + leftover_body[:match.start()]
156161
leftover_body = leftover_body[match.end():]
157162
new_body = new_body + match.expand(r"[bpo-\g<issue>](https://bugs.python.org/issue\g<issue>)")
158163
else:
159164
new_body = new_body + leftover_body[:presence]
160165
leftover_body = leftover_body[presence:]
166+
161167
new_body = new_body + leftover_body
162168

163169
return new_body

‎tests/test_bpo.py‎

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,8 @@ async def test_set_pull_request_body_already_hyperlinked_bpo(action):
384384
"statuses_url": "https://api.github.com/blah/blah/git-sha",
385385
"issue_url": "https://api.github.com/repos/blah/blah/issues/comments/123456",
386386
"body": ("bpo-123"
387-
"[bpo-123](https://bugs.python.org/issue123)"
388-
"<a href='https://bugs.python.org/issue123'>bpo-123</a>"
389-
)
387+
"[bpo-123](https://bugs.python.org/issue123)"
388+
"<a href='https://bugs.python.org/issue123'>bpo-123</a>")
390389
},
391390
"changes": {"stuff": "thingy"}
392391
}
@@ -407,9 +406,8 @@ async def test_set_comment_body_already_hyperlinked_bpo(action):
407406
"comment": {
408407
"url": "https://api.github.com/repos/blah/blah/issues/comments/123456",
409408
"body": ("bpo-123"
410-
"[bpo-123](https://bugs.python.org/issue123)"
411-
"<a href='https://bugs.python.org/issue123'>bpo-123</a>"
412-
)
409+
"[bpo-123](https://bugs.python.org/issue123)"
410+
"<a href='https://bugs.python.org/issue123'>bpo-123</a>")
413411
}
414412
}
415413

@@ -419,3 +417,58 @@ async def test_set_comment_body_already_hyperlinked_bpo(action):
419417
body_data = gh.patch_data
420418
assert body_data["body"].count("[bpo-123](https://bugs.python.org/issue123)") == 2
421419
assert "123456" in gh.patch_url
420+
421+
422+
@pytest.mark.asyncio
423+
@pytest.mark.parametrize("action", ["opened", "edited"])
424+
async def test_bpo_text_within_codeblocks_in_comments(action):
425+
data = {
426+
"action": action,
427+
"pull_request": {
428+
"title": "[3.6] bpo-12345: some issue",
429+
"url": "https://api.github.com/repos/python/cpython/pulls/4321",
430+
"statuses_url": "https://api.github.com/blah/blah/git-sha",
431+
"issue_url": "https://api.github.com/repos/blah/blah/issues/comments/123456",
432+
"body": ("bpo-123"
433+
"```bpo-123```"
434+
"`bpo-123`"
435+
"[bpo-123](https://bugs.python.org/issue123)"
436+
"<a href='https://bugs.python.org/issue123'>bpo-123</a>")
437+
},
438+
"changes": {"stuff": "thingy"}
439+
}
440+
441+
event = sansio.Event(data, event="pull_request", delivery_id="123123")
442+
gh = FakeGH()
443+
await bpo.router.dispatch(event, gh)
444+
body_data = gh.patch_data
445+
assert body_data["body"].count("[bpo-123](https://bugs.python.org/issue123)") == 2
446+
assert "123456" in gh.patch_url
447+
448+
449+
@pytest.mark.asyncio
450+
@pytest.mark.parametrize("action", ["opened", "edited"])
451+
async def test_bpo_text_within_codeblocks_in_pull_request(action):
452+
data = {
453+
"action": action,
454+
"pull_request": {
455+
"title": "[3.6] bpo-12345: some issue",
456+
"url": "https://api.github.com/repos/python/cpython/pulls/4321",
457+
"statuses_url": "https://api.github.com/blah/blah/git-sha",
458+
"issue_url": "https://api.github.com/repos/blah/blah/issues/comments/123456",
459+
"body": ("bpo-123"
460+
"```bpo-123```"
461+
"`bpo-123`"
462+
"[bpo-123](https://bugs.python.org/issue123)"
463+
"<a href='https://bugs.python.org/issue123'>bpo-123</a>")
464+
},
465+
"changes": {"stuff": "thingy"}
466+
}
467+
468+
event = sansio.Event(data, event="pull_request", delivery_id="123123")
469+
gh = FakeGH()
470+
await bpo.router.dispatch(event, gh)
471+
body_data = gh.patch_data
472+
assert body_data["body"].count("[bpo-123](https://bugs.python.org/issue123)") == 2
473+
assert "123456" in gh.patch_url
474+
assert 1 == 2

0 commit comments

Comments
 (0)