Skip to content

Commit

Permalink
Add PR Links as List Items to Issues (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
saadmk11 authored Nov 16, 2022
1 parent 4abd68d commit ea0353f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
13 changes: 6 additions & 7 deletions bedevere/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,16 @@
ISSUE_BODY_CLOSING_TAG = f'<!-- /{ISSUE_BODY_TAG_NAME} -->'
ISSUE_BODY_TASK_LIST_TEMPLATE = f"""\n
{ISSUE_BODY_OPENING_TAG}
```[tasklist]
### Linked PRs
- [ ] gh-{{pr_number}}
```
* gh-{{pr_number}}
{ISSUE_BODY_CLOSING_TAG}
"""

# Regex pattern to search for tasklist in the issue body
ISSUE_BODY_TASK_LIST_PATTERN = re.compile(
rf"(?P<start>{ISSUE_BODY_OPENING_TAG}\r?\n```\[tasklist\])"
rf"(?P<start>{ISSUE_BODY_OPENING_TAG})"
rf"(?P<tasks>.*?)"
rf"(?P<end>```\r?\n{ISSUE_BODY_CLOSING_TAG})",
rf"(?P<end>{ISSUE_BODY_CLOSING_TAG})",
flags=re.DOTALL
)

Expand Down Expand Up @@ -151,8 +149,9 @@ def build_issue_body(pr_number: int, body: str) -> str:

# If the body already contains a tasklist, only append the new PR to the list
return ISSUE_BODY_TASK_LIST_PATTERN.sub(
fr"\g<start>\g<tasks>- [ ] gh-{pr_number}\n\g<end>",
body
fr"\g<start>\g<tasks>* gh-{pr_number}\n\g<end>",
body,
count=1,
)


Expand Down
18 changes: 9 additions & 9 deletions tests/test_gh_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ async def test_set_status_success_issue_found_on_gh(action, monkeypatch, issue_n
assert len(gh.patch_data) > 0
assert f"<!-- gh-issue-number: gh-{issue_number} -->" in gh.patch_data[0]["body"]
assert (
"\n\n<!-- gh-linked-prs -->\n```[tasklist]\n"
f"### Linked PRs\n- [ ] gh-{issue_number}\n"
"```\n<!-- /gh-linked-prs -->\n"
"\n\n<!-- gh-linked-prs -->\n"
f"### Linked PRs\n* gh-{issue_number}\n"
"<!-- /gh-linked-prs -->\n"
) in gh.patch_data[1]["body"]
assert len(gh.patch_url) == 2
assert gh.patch_url[0] == data["pull_request"]["url"]
Expand Down Expand Up @@ -227,9 +227,9 @@ async def test_set_status_success_issue_found_on_gh_ignore_case(action, monkeypa
assert len(gh.patch_data) > 0
assert f"<!-- gh-issue-number: gh-{issue_number} -->" in gh.patch_data[0]["body"]
assert (
"\n\n<!-- gh-linked-prs -->\n```[tasklist]\n"
f"### Linked PRs\n- [ ] gh-{issue_number}\n"
"```\n<!-- /gh-linked-prs -->\n"
"\n\n<!-- gh-linked-prs -->\n"
f"### Linked PRs\n* gh-{issue_number}\n"
"<!-- /gh-linked-prs -->\n"
) in gh.patch_data[1]["body"]
assert len(gh.patch_url) == 2
assert gh.patch_url[0] == data["pull_request"]["url"]
Expand Down Expand Up @@ -352,9 +352,9 @@ async def test_no_body_when_edit_title(monkeypatch, issue_number):
assert len(gh.patch_data) > 0
assert f"<!-- gh-issue-number: gh-{issue_number} -->" in gh.patch_data[0]["body"]
assert (
"\n\n<!-- gh-linked-prs -->\n```[tasklist]\n"
f"### Linked PRs\n- [ ] gh-{issue_number}\n"
"```\n<!-- /gh-linked-prs -->\n"
"\n\n<!-- gh-linked-prs -->\n"
f"### Linked PRs\n* gh-{issue_number}\n"
"<!-- /gh-linked-prs -->\n"
) in gh.patch_data[1]["body"]
assert len(gh.patch_url) == 2
assert gh.patch_url[0] == data["pull_request"]["url"]
Expand Down
18 changes: 9 additions & 9 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,24 +247,24 @@ async def test_patch_body_adds_pr_if_not_present():
await util.patch_body(gh, util.ISSUE, vals, 1234)
data = {
"body": (
"\n\n<!-- gh-linked-prs -->\n```[tasklist]\n"
"### Linked PRs\n- [ ] gh-1234\n"
"```\n<!-- /gh-linked-prs -->\n"
"\n\n<!-- gh-linked-prs -->\n"
"### Linked PRs\n* gh-1234\n"
"<!-- /gh-linked-prs -->\n"
)
}
mock.assert_called_once_with("https://fake.com", data=data)
with patch.object(gh, "patch") as mock:
vals["body"] = (
"\n\n<!-- gh-linked-prs -->\n```[tasklist]\n"
"### Linked PRs\n- [ ] gh-1234\n"
"```\n<!-- /gh-linked-prs -->\n"
"\n\n<!-- gh-linked-prs -->\n"
"### Linked PRs\n* gh-1234\n"
"<!-- /gh-linked-prs -->\n"
)
await util.patch_body(gh, util.ISSUE, vals, 54321)
data = {
"body": (
"\n\n<!-- gh-linked-prs -->\n```[tasklist]\n"
"### Linked PRs\n- [ ] gh-1234\n- [ ] gh-54321\n"
"```\n<!-- /gh-linked-prs -->\n"
"\n\n<!-- gh-linked-prs -->\n"
"### Linked PRs\n* gh-1234\n* gh-54321\n"
"<!-- /gh-linked-prs -->\n"
)
}
mock.assert_called_once_with("https://fake.com", data=data)
Expand Down

0 comments on commit ea0353f

Please sign in to comment.