when would the associatedPullRequests connection on a Commit object return multiple PRs

This question relates to the GitHub V4 GraphQL API, which I’m fairly new to.

In what scenarios would a single commit, merged to the default branch, have multiple PRs returned by its associatedPullRequests? (my perhaps incorrect assumption is that the associated PR would be a 1:1 relationship with the commit which and the PR merge that resulted in it landing).

I was able to create this commit on my test repository that is associated with two pull requests at the same time. Here is the query I used to verify:

query {
  repository(owner: "lee-dohm", name: "test-repo") {
    object(expression: "8357005afac5208b220402aadb596633229cfeaf") {
      ... on Commit {
        url
        message
        associatedPullRequests(first: 10) {
          nodes {
            number
            author {
              login
            }
            url
          }
        }
      }
    }
  }
}

and this was the response:

{
  "data": {
    "repository": {
      "object": {
        "url": "https://github.com/lee-dohm/test-repo/commit/8357005afac5208b220402aadb596633229cfeaf",
        "message": "Create multiple-associated-pull-requests.md",
        "associatedPullRequests": {
          "nodes": [
            {
              "number": 21,
              "author": {
                "login": "lee-dohm"
              },
              "url": "https://github.com/lee-dohm/test-repo/pull/21"
            },
            {
              "number": 22,
              "author": {
                "login": "lee-dohm"
              },
              "url": "https://github.com/lee-dohm/test-repo/pull/22"
            },
            {
              "number": 23,
              "author": {
                "login": "lee-dohm"
              },
              "url": "https://github.com/lee-dohm/test-repo/pull/23"
            }
          ]
        }
      }
    }
  }
}

Here’s how I did it:

  1. Create a file named multiple-associated-pull-requests.md in the repository on branch lee-dohm-patch-3
  2. Create a pull request against master from lee-dohm-patch-3
  3. Update the file on branch lee-dohm-patch-4
  4. Create a pull request against lee-dohm-patch-3 from lee-dohm-patch-4
  5. Create a pull request against master from lee-dohm-patch-4

I then merged the second pull request (using squash and merge) and re-ran my query which returned the following:

{
  "data": {
    "repository": {
      "object": {
        "url": "https://github.com/lee-dohm/test-repo/commit/8357005afac5208b220402aadb596633229cfeaf",
        "message": "Create multiple-associated-pull-requests.md",
        "associatedPullRequests": {
          "nodes": [
            {
              "number": 21,
              "author": {
                "login": "lee-dohm"
              },
              "url": "https://github.com/lee-dohm/test-repo/pull/21"
            },
            {
              "number": 23,
              "author": {
                "login": "lee-dohm"
              },
              "url": "https://github.com/lee-dohm/test-repo/pull/23"
            }
          ]
        }
      }
    }
  }
}

notably missing the second pull request.

I hope that helps!

1 Like