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:

Hi, I am adding to this thread since the problem I am facing is similar.
I am trying to write a query to retrieve the Pull Requests mentioned in one Issue (Note: Not the ones linked to the issue).

For example, looking at this issue #11803, there is a PR mentioned in this comment.

CrossReferencedEvent does not include the PRs mentioned in this case.

query {
  repository(owner: "ARMmbed", name: "mbed-os") {
    issue(number: 11803) {
      timelineItems(first: 100) {
        nodes {
          ... on CrossReferencedEvent {
	          source {
              ... on PullRequest {
                number
                title
              }
          	}
        	}
      	}
    	}
  	}
  }
}

Result:

{
  "data": {
    "repository": {
      "issue": {
        "timelineItems": {
          "nodes": [
            {},
            {
              "source": {}
            },
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
            {
              "source": {}
            }
          ]
        }
      }
    }
  }
}

I have also tried exploring the different IssueTimelineItems but I could not find any that solved the problem.

This also does not solve it:

query {
  repository(owner: "ARMmbed", name: "mbed-os") {
    issue(number: 11803) {
      timelineItems(first: 100) {
        nodes {
          ... on ReferencedEvent {
             subject{
              ... on PullRequest {
                title
                number
              }
            }
          }
        }
      }
    }
  }
}