Skip to content

Add Pull Request Link to Issue #511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 31, 2022
Merged
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
26 changes: 21 additions & 5 deletions bedevere/gh_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
"gh": "https://github.com/python/cpython/issues/{issue_number}",
"bpo": "https://bugs.python.org/issue?@action=redirect&bpo={issue_number}"
}
ISSUE_CHECK_URL: Dict[IssueKind, str] = {
"gh": "https://api.github.com/repos/python/cpython/issues/{issue_number}",
"bpo": "https://bugs.python.org/issue{issue_number}"
}


@router.register("pull_request", action="opened")
Expand All @@ -33,12 +37,15 @@ async def set_status(
event, gh: GitHubAPI, *args, session: ClientSession, **kwargs
):
"""Set the issue number status on the pull request."""
issue = await util.issue_for_PR(gh, event.data["pull_request"])
pull_request = event.data["pull_request"]
issue = await util.issue_for_PR(gh, pull_request)

if util.skip("issue", issue):
await util.post_status(gh, event, SKIP_ISSUE_STATUS)
return

issue_number_found = ISSUE_RE.search(event.data["pull_request"]["title"])
issue_number_found = ISSUE_RE.search(pull_request["title"])

if not issue_number_found:
status = create_failure_status_no_issue()
else:
Expand All @@ -50,7 +57,16 @@ async def set_status(
if issue_found:
status = create_success_status(issue_number, kind=issue_kind)
if issue_kind == "gh":
await util.patch_body(gh, event.data["pull_request"], issue_number)
# Add the issue number to the pull request's body
await util.patch_body(gh, pull_request, issue_number, "Issue")
# Get GitHub Issue data
issue_data = await gh.getitem(
ISSUE_CHECK_URL[issue_kind].format(issue_number=issue_number)
)
# Add the pull request number to the issue's body
await util.patch_body(
gh, issue_data, pull_request["number"], "PR"
)
else:
status = create_failure_status_issue_not_present(
issue_number, kind=issue_kind
Expand Down Expand Up @@ -123,14 +139,14 @@ async def _validate_issue_number(
) -> bool:
"""Ensure the GitHub Issue number is valid."""
if kind == "bpo":
url = f"https://bugs.python.org/issue{issue_number}"
url = ISSUE_CHECK_URL[kind].format(issue_number=issue_number)
async with session.head(url) as res:
return res.status != 404

if kind != "gh":
raise ValueError(f"Unknown issue kind {kind}")

url = f"/repos/python/cpython/issues/{issue_number}"
url = ISSUE_CHECK_URL[kind].format(issue_number=issue_number)
try:
response = await gh.getitem(url)
except gidgethub.BadRequest:
Expand Down
42 changes: 26 additions & 16 deletions bedevere/util.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import enum
import re
import sys
from typing import Any, Dict, Union

import gidgethub

from gidgethub.abc import GitHubAPI

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

<!-- {TAG_NAME}: gh-{{issue_number}} -->
* Issue: gh-{{issue_number}}
<!-- {TAG_NAME}: gh-{{pr_or_issue_number}} -->
* {{key}}: gh-{{pr_or_issue_number}}
{CLOSING_TAG}
"""

Expand Down Expand Up @@ -93,23 +94,32 @@ 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.
async def patch_body(
gh: GitHubAPI,
pr_or_issue: Dict[str, Any],
pr_or_issue_number: int,
key: str
) -> Union[bytes, None]:
"""Updates the description of a PR/Issue with the gh issue/pr number if it exists.

returns if body exists with issue_number
returns if body exists with issue/pr number
"""
if "body" not in pull_request or pull_request["body"] is None:
return await gh.patch(
pull_request["url"],
data={"body": BODY.format(body=DEFAULT_BODY, issue_number=issue_number)},
)
body = pr_or_issue.get("body", None) or DEFAULT_BODY
body_search_pattern = rf"(^|\b)(GH-|gh-|#){pr_or_issue_number}\b"

if not re.search(rf"(^|\b)(GH-|gh-|#){issue_number}\b", pull_request["body"]):
if not body or not re.search(body_search_pattern, body):
return await gh.patch(
pull_request["url"],
data={"body": BODY.format(body=pull_request["body"], issue_number=issue_number)},
pr_or_issue["url"],
data={
"body": BODY.format(
body=body,
pr_or_issue_number=pr_or_issue_number,
key=key,
tag_type=key.lower(),
)
},
)
return
return None


async def is_core_dev(gh, username):
Expand Down
Loading