Is there any api to get all issues associated with a given list of commit

Given a list of commits, I want to get all the issues linked to those commits.

Is there any rest or graphql api available today to fetch above information?

There isn’t an API for this because it is done by pattern recognition in the commit comment. When a commit is pushed that references an issue or PR, an event is added to the issue to refer back to the commit but no such metadata is created for the commit.

1 Like

https://help.github.com/articles/searching-issues-and-pull-requests/#search-by-commit-sha

The above api says "If you know the specific SHA hash of a commit, you can use it to search for pull requests that contain that SHA."

Isn’t there any way to get issues using search api? I think pull request are treated as issues only by GitHub.

There are ways to get a list of issues via the search API but not via a commit SHA, no, for the reasons I stated before.

(Posting this here in case someone else finds it useful as I was having a hard time figuring out how to get 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 

1 Like