Skip to content

Commit

Permalink
gh-428: Add GitHub issue to description of PR (#430)
Browse files Browse the repository at this point in the history
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
  • Loading branch information
3 people committed Jun 16, 2022
1 parent aba911f commit ad03d53
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 22 deletions.
34 changes: 33 additions & 1 deletion bedevere/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@

import gidgethub


DEFAULT_BODY = ""
TAG_NAME = "gh-issue-number"
NEWS_NEXT_DIR = "Misc/NEWS.d/next/"
CLOSING_TAG = f"<!-- /{TAG_NAME} -->"
BODY = f"""\
{{body}}
<!-- {TAG_NAME}: gh-{{issue_number}} -->
* gh-{{issue_number}}
{CLOSING_TAG}
"""


@enum.unique
Expand Down Expand Up @@ -67,7 +78,10 @@ async def files_for_PR(gh, pull_request):
data = []
async for filedata in gh.getiter(files_url): # pragma: no branch
data.append(
{"file_name": filedata["filename"], "patch": filedata.get("patch", ""),}
{
"file_name": filedata["filename"],
"patch": filedata.get("patch", ""),
}
)
return data

Expand All @@ -77,6 +91,24 @@ async def issue_for_PR(gh, pull_request):
return await gh.getitem(pull_request["issue_url"])


async def patch_body(gh, pull_request, issue_number):
"""Updates the description of a PR with the gh issue number if it exists.
returns if body exists with issue_number
"""
if "body" not in pull_request or pull_request["body"] is None:
return await gh.patch(
pull_request["url"],
data=BODY.format(body=DEFAULT_BODY, issue_number=issue_number),
)
if f"GH-{issue_number}\n" not in pull_request["body"]:
return await gh.patch(
pull_request["url"],
data=BODY.format(body=pull_request["body"], issue_number=issue_number),
)
return


async def is_core_dev(gh, username):
"""Check if the user is a CPython core developer."""
org_teams = "/orgs/python/teams"
Expand Down
28 changes: 24 additions & 4 deletions tests/test_gh_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@

class FakeGH:

def __init__(self, *, getitem=None, post=None):
def __init__(self, *, getitem=None, post=None, patch=None):
self._getitem_return = getitem
self._post_return = post
self._patch_return = patch
self.post_url = []
self.post_data = []
self.patch_url = []
self.patch_data = []

async def getitem(self, url):
if isinstance(self._getitem_return, Exception):
Expand All @@ -28,7 +31,6 @@ async def post(self, url, *, data):
self.post_data.append(data)
return self._post_return


@pytest.mark.asyncio
@pytest.mark.parametrize("action", ["opened", "synchronize", "reopened"])
async def test_set_status_failure(action, monkeypatch):
Expand All @@ -40,6 +42,7 @@ async def test_set_status_failure(action, monkeypatch):
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"title": "No issue in title",
"issue_url": "issue URL",
"url": "url",
},
}
issue_data = {
Expand Down Expand Up @@ -68,6 +71,8 @@ async def test_set_status_failure_via_issue_not_found_on_github(action, monkeypa
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"title": "gh-123: Invalid issue number",
"issue_url": "issue URL",
"url": "url",
},
}
event = sansio.Event(data, event="pull_request", delivery_id="12345")
Expand All @@ -88,7 +93,9 @@ async def test_set_status_success_issue_found_on_bpo(action):
"action": action,
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"title": "bpo-12345: an issue!",
"title": "bpo-12345: An issue on b.p.o",
"issue_url": "issue URL",
"url": "url",
},
}
event = sansio.Event(data, event="pull_request", delivery_id="12345")
Expand All @@ -112,6 +119,7 @@ async def test_set_status_success(action, monkeypatch):
"action": action,
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"url": "",
"title": "[3.6] gh-1234: an issue!",
},
}
Expand All @@ -137,6 +145,7 @@ async def test_set_status_success_issue_found_on_gh(action, monkeypatch):
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"title": "gh-12345: an issue!",
"url": "url",
},
}
event = sansio.Event(data, event="pull_request", delivery_id="12345")
Expand All @@ -161,6 +170,7 @@ async def test_set_status_success_issue_found_on_gh_ignore_case(action, monkeypa
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"title": "GH-12345: an issue!",
"url": "url",
},
}
event = sansio.Event(data, event="pull_request", delivery_id="12345")
Expand All @@ -186,6 +196,7 @@ async def test_set_status_success_via_skip_issue_label(action, monkeypatch):
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"title": "No issue in title",
"issue_url": "issue URL",
"url": "url",
},
}
issue_data = {
Expand All @@ -211,6 +222,7 @@ async def test_edit_title(monkeypatch):
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"title": "gh-1234: an issue!",
"url": "url",
},
"action": "edited",
"changes": {"title": "thingy"},
Expand Down Expand Up @@ -251,6 +263,7 @@ async def test_edit_other_than_title(monkeypatch):
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"title": "bpo-1234: an issue!",
"url": "url",
},
"action": "edited",
"changes": {"stuff": "thingy"},
Expand All @@ -270,6 +283,7 @@ async def test_new_label_skip_issue_no_issue():
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"title": "An easy fix",
"url": "url",
},
}
event = sansio.Event(data, event="pull_request", delivery_id="12345")
Expand All @@ -287,6 +301,7 @@ async def test_new_label_skip_issue_with_issue_number():
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"title": "Revert gh-1234: revert an easy fix",
"url": "url",
},
}
event = sansio.Event(data, event="pull_request", delivery_id="12345")
Expand All @@ -308,6 +323,7 @@ async def test_new_label_skip_issue_with_issue_number_ignore_case():
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"title": "Revert Gh-1234: revert an easy fix",
"url": "url",
},
}
event = sansio.Event(data, event="pull_request", delivery_id="12345")
Expand All @@ -327,6 +343,7 @@ async def test_new_label_not_skip_issue():
"label": {"name": "non-trivial"},
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"url": "url",
},
}
event = sansio.Event(data, event="pull_request", delivery_id="12345")
Expand All @@ -347,6 +364,7 @@ async def test_removed_label_from_label_deletion(monkeypatch):
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"title": "gh-1234: an issue!",
"url": "url",
},
}
event = sansio.Event(data, event="pull_request", delivery_id="12345")
Expand All @@ -366,6 +384,7 @@ async def test_removed_label_skip_issue(monkeypatch):
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"title": "gh-1234: an issue!",
"url": "url",
},
}
event = sansio.Event(data, event="pull_request", delivery_id="12345")
Expand All @@ -389,6 +408,7 @@ async def test_removed_label_non_skip_issue(monkeypatch):
"label": {"name": "non-trivial"},
"pull_request": {
"statuses_url": "https://api.github.com/blah/blah/git-sha",
"url": "url",
},
}
event = sansio.Event(data, event="pull_request", delivery_id="12345")
Expand Down Expand Up @@ -422,7 +442,7 @@ async def test_validate_issue_number_is_pr_on_github():

gh = FakeGH(getitem={
"number": 123,
"pull_request": {"html_url": "https://github.com/python/cpython/pull/123"}
"pull_request": {"html_url": "https://github.com/python/cpython/pull/123", "url": "url",}
})
async with aiohttp.ClientSession() as session:
response = await gh_issue._validate_issue_number(gh, 123, session=session)
Expand Down
Loading

0 comments on commit ad03d53

Please sign in to comment.