Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 6

Get all issues linked to a pull request

There is a way to get the list of all the issues that are linked, at least, to a pull requests ?

5 Replies
Highlighted
Commander Lvl 2
Message 2 of 6

Re: Get all issues linked to a pull request

You can get the comments currently (which would include the linked issues if they are added there). I suggest reaching out to GitHub support since they just rolled out the ability to Link Issues through the actual GitHub UI but I don't see the corresponding API here: https://developer.github.com/v3/issues/#get-a-single-issue under the issues section. 



*Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!*
Highlighted
Copilot Lvl 3
Message 3 of 6

Re: Get all issues linked to a pull request

Hi Logan -

 

I am trying to do this as well, and it actually does not appear that the comments include any linked issues - the comments only appear to include actual comments, and not the conversation snippets regarding connected PR's.

 

I set up a "catchall" webhook on the repo, and it also does not appear that linking a PR to an issue is even captured by a webhook.

 

Can anyone offer any insight as to how to capture this information (linking a PR to another issue)?

Highlighted
Copilot Lvl 2
Message 4 of 6

Re: Get all issues linked to a pull request

I need the same functionality.

 

I've looked into Issue/PR Events, Comments, and Timelimes but nothing shows up wrt to Linked Issues.

 

I've also tried searching but to no avail e.g. the following API search request yields an error:

 

- Request: https://api.github.com/search/issues?q=linked:pr+repo:{ORGNAME}/{REPONAME}

- Result: "None of the search qualifiers apply to this search type."

 

The "linked:pr" is supposed to return issues linked to PRs (it works in the GH UI).

 

Highlighted
Ground Controller Lvl 1
Message 5 of 6

Re: Get all issues linked to a pull request

I'm also in need of this functionality via the API.  

 

I can currently use the API to track which commits occurred between commit X and commit Y (by walking the parent commits), and trace those commits to PRs, but I can't trace those PRs to the linked issues they resolve.  

 

This API addition would allow our org to trace fixes much more easily!

Highlighted
Ground Controller Lvl 2
Message 6 of 6

Re: Get all issues linked to a pull request

There's no direct way of getting a list of Issues linked to a Pull Request.

 

But each Pull Request includes a timeline of events and one of those events is when an Issue gets linked. Using the timeline of events I was able to write a GitHub APIv4 API request and some javascript to get the Issues linked to a PR:

 

First, here's the GraphQL query:

{
  resource(url: "https://github.com/Org/Project/pull/1234") {
    ... on PullRequest {
      timelineItems(itemTypes: [CONNECTED_EVENT, DISCONNECTED_EVENT], first: 100) {
        nodes {
          ... on ConnectedEvent {
            id
            subject {
              ... on Issue {
                number
              }
            }
          }
          ... on DisconnectedEvent {
            id
            subject {
              ... on Issue {
                number
              }
            }
          }
        }
      }
    }
  }
}

 

If you run this in the GitHub GraphQL Explorer (https://developer.github.com/v4/explorer/), you'll see all the events that an Issue was connected and disconnected to a Pull Request.

 

Using that query, I pass the response to this code that I wrote for a nodejs app that then determines which Issue is still linked to the Pull Request

const issues = {};
resource.timelineItems.nodes.map(node => {
    if (issues.hasOwnProperty(node.subject.number)) {
        issues[node.subject.number]++;
    } else {
        issues[node.subject.number] = 1;
    }
});
const linkedIssues = [];
for (const [issue, count] of Object.entries(issues)) {
    if (count % 2 != 0) {
        linkedIssues.push(issue);
    }
}
console.log(issues);

console.log(linkedIssues);

The logic here is as follows:

  1. Get a list of all events on a Pull Request of the type CONNECTED_EVENT and DISCONNECTED_EVENT
  2. Create a map, keyed by Issue number and keep a count of how may times the issue is CONNECTED and DISCONNECTED
  3. From that map, look for keys that have an odd-numbered count, as these are the events that have been CONNECTED that don't have a corresponding DISCONNECTED event.

 

It's not a super elegant solution, but it solves what I need, which was to find those Linked issues.

 

Hopefully this helps someone else out