Skip to content

Instantly share code, notes, and snippets.

@mccutchen
Last active April 26, 2024 12:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mccutchen/9a0530a440470b4dee57c53284a18b52 to your computer and use it in GitHub Desktop.
Save mccutchen/9a0530a440470b4dee57c53284a18b52 to your computer and use it in GitHub Desktop.
A couple of ways to look up a GitHub pull request via the v4 GraphQL API

GitHub GraphQL API v4 Examples

Here are two queries that look up information about a pull request via GitHub's GraphQL API v4. The first looks up a pull request by number, the second looks up a pull request by ref (i.e. commit hash, branch name, tag name).

Why tho

While it is trivial to use the RESTful v3 API to look up a pull request by number, the GraphQL API is the only way to do the same thing for an arbitrary reference in a single HTTP request.

These took a bit of trial and error in the GitHub's GraphQL Explorer and various support threads to get right, so I'm putting them up here for posterity.

query ($repoOwner: String!, $repoName: String!, $pullRequestID: Int!) {
repository(owner: $repoOwner, name: $repoName) {
pullRequest(number: $pullRequestID) {
author {
login
}
baseRefOid
headRefName
headRefOid
mergeable
reviewDecision
state
title
}
}
}
# Example variables
#
# {
# "repoOwner": "mccutchen",
# "repoName": "go-httpbin",
# "pullRequestID": 49
# }
query ($repoOwner: String!, $repoName: String!, $ref: String!) {
repository(name: $repoName, owner: $repoOwner) {
commit: object(expression: $ref) {
... on Commit {
associatedPullRequests(first: 100) {
nodes {
author {
login
}
baseRefOid
headRefName
headRefOid
mergeable
number
reviewDecision
state
title
}
}
}
}
}
}
# Example variables
#
# {
# "repoOwner": "mccutchen",
# "repoName": "go-httpbin",
# "ref": "8f5ca46"
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment