Search code examples
githubgithub-apipull-requestgithub-api-v3

Getting Linked issues and Projects associated with a pull request form github api v3


How can I get the Projects and linked issues for a given pull request from github API V3? The pulls endpoint does not give information on either of them. In the github's pull requests section's sidebar there are Projects and Linked issues mentioned. But I could not find a way to get this information via API calls.

Screenshot of the sidebar for reference

I want to find out which issues a pull request closes on merging successfully.


Solution

  • To get the projects with cards linking to a specific pull request you can use Github GraphQL API using this payload :

    {
      repository(owner: "twbs", name: "bootstrap") {
        pullRequest(number: 30342) {
          projectCards {
            nodes {
              project {
                name
              }
            }
          }
        }
      }
    }
    

    But for the linked issues I don't think the API is available yet. You can still scrape the list from github.com if the repo is public. The following script get the list of issues URL using :

    import requests
    from bs4 import BeautifulSoup
    import re
    
    repo = "twbs/bootstrap"
    pr = "30342"
    
    r = requests.get(f"https://github.com/{repo}/pull/{pr}")
    soup = BeautifulSoup(r.text, 'html.parser')
    issueForm = soup.find("form", { "aria-label": re.compile('Link issues')})
    
    print([ i["href"] for i in issueForm.find_all("a")])