Get referenced pull request from issue

Hey Community,

Github allows to see if pull requests reference an issue (or vice versa), e.g. see the following example (randomly picked):

issue with referenced pull request

This is great and I’m wondering if I can achieve the same via the API. While the API (v3) allows to see if commits are referenced in the issue (via its events) I wasn’t able to find a way to get referenced pull requests. I went through a couple of sources (e.g. here).

Is this even possible?

You’re correct. I wasn’t able to find a way to do what you’re asking via the REST API. But here’s a query that works for the GraphQL API:

query {
  repository(owner: "bigdatagenomics", name: "adam") {
    issue(number: 479) {
      timelineItems(first: 100) {
        nodes {
          ... on ClosedEvent {
            closer {
              ... on PullRequest {
                number
                title
              }
            }
          }
        }
      }
    }
  }
}

which results in:

{
  "data": {
    "repository": {
      "issue": {
        "timelineItems": {
          "nodes": [
            {},
            {},
            {},
            {
              "closer": {
                "number": 570,
                "title": "A few small conversion fixes"
              }
            }
          ]
        }
      }
    }
  }
}

I hope that helps!

1 Like

Thanks for your answer! Based on your query I played around with the v4 API and came up with something different which suits me better:

query {
  repository(owner: "bigdatagenomics", name: "adam") {
    issue(number: 479) {
      timelineItems(first: 100) {
        nodes {
          ... on CrossReferencedEvent {
            source {
              ... on PullRequest {
                number
                title
              }
            }
          }
        }
      }
    }
  }
}

In this case I get every referenced Pull Requests and not only those which closed the issue.

Thanks again!

1 Like

Awesome! Thanks for sharing your ultimate solution back :grinning: