Identify relationship between issues and pull requests (v3 API) #24492
-
Via the GH UI, I have linked a pull request P and an issue I. I would like to be able to identify such linkages via the v3 API. However, when I go to repo/pull/P, repo/issues/I, and repo/issues/P, I don’t see any information in the returned data that would indicate I’ve linked P and I via the GH UI. Is there any way to find this information via the v3 API? I believe my issue is similar to the issue in https://github.community/t/get-all-issues-linked-to-a-pull-request/14653 and https://github.community/t/get-referenced-pull-request-from-issue/14027 but I am looking for a v3 based solution as opposed to v4. Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
In case anyone has a similar issue in the future, my workaround was as follows. First I grabbed the “html_url” for the issue, retrieved the contents of the URL and used an HTML parser to grab the div with class “css-truncate my-1” and then the href of the anchor within that. The href is a relative URL to the linked pull request. With that, I could break up the path and get the pull number, and use that number to retrieve the pull request information via the REST API. It’s brittle, but it’s working… |
Beta Was this translation helpful? Give feedback.
-
Bump, this being only available from the v4 API is quite frustrating to v3 users as the access methods are so different and the v4 API is often not better or more readable. |
Beta Was this translation helpful? Give feedback.
-
🎉 Clean solution here. Find linked PRs from Issues and vice-versa. Fully working Python implementation (PyGithub): def get_linked_pr_from_issue(issue: Issue) -> PullRequest | None:
"""Check if the given issue has a linked pull request.
This function iterates over the timeline of the issue and checks if there is a 'cross-referenced' event.
If such an event is found, it checks if the source of the event is an issue and if so, it returns the issue as a pull request.
Usage:
issue: Issue = repo.get_issue(number=8)
pr_or_none = check_if_issue_has_linked_pr(issue)
Args:
issue (Issue): The issue to check for a linked pull request.
Returns:
PullRequest: The linked pull request if it exists, None otherwise.
"""
events_pages: PaginatedList[TimelineEvent] = issue.get_timeline()
pg_num = 0
while events_pages.get_page(pg_num):
page = events_pages.get_page(pg_num)
pg_num += 1
for e in page:
if str(e.event) == 'cross-referenced':
if e.source and e.source.issue:
return e.source.issue.as_pull_request()
def get_linked_issue_from_pr(pr: PullRequest) -> Issue | None:
"""Check if the given pull request has a linked issue.
This function iterates over the timeline of the pull request and checks if there is a 'cross-referenced' event.
If such an event is found, it checks if the source of the event is a pull request and if so, it returns the pull request as an issue.
Usage:
pr: PullRequest = repo.get_pull(number=8)
issue_or_none = check_if_pr_has_linked_issue(pr)
Args:
pr (PullRequest): The pull request to check for a linked issue.
Returns:
Issue: The linked issue if it exists, None otherwise.
"""
events_pages: PaginatedList[TimelineEvent] = pr.as_issue().get_timeline()
pg_num = 0
while events_pages.get_page(pg_num):
page = events_pages.get_page(pg_num)
pg_num += 1
for e in page:
if str(e.event) == 'cross-referenced':
if e.source and e.source.issue:
return e.source.issue |
Beta Was this translation helpful? Give feedback.
In case anyone has a similar issue in the future, my workaround was as follows.
First I grabbed the “html_url” for the issue, retrieved the contents of the URL and used an HTML parser to grab the div with class “css-truncate my-1” and then the href of the anchor within that. The href is a relative URL to the linked pull request. With that, I could break up the path and get the pull number, and use that number to retrieve the pull request information via the REST API.
It’s brittle, but it’s working…