4
votes

I like to count all open pull requests and issues in a repository with help of the GitHub API. I found out that the API endpoint /repos/:owner/:repo result contains a open_issues property. However this is the sum of the amount of issues and pull requests.

Is there a way to get or calculate the amount of open issue and pull requersts in a repository?

4
Can you iterate over the open issues returned from developer.github.com/v3/issues/#list-issues-for-a-repository passing in state=open - osowskit
state=open is the default for both listing issues and pull requests, so it's not necessary to specify it. - kfb

4 Answers

2
votes

A more efficient way (than the accepted answer) is to use the search API. To get the number of open issues you can call (replace with your org and repo):

https://api.github.com/search/issues?q=repo:realm/realm-java%20is:issue%20is:open&per_page=1

and to get the number of PR's:

https://api.github.com/search/issues?q=repo:realm/realm-java%20is:pr%20is:open&per_page=1

You can of course remove is:open if you want both open and closed issues/pr's.

Both will return "total_count" with the result. Note that I added per_page=1 to not actually retrieve all the issues.

1
votes

osowskit is correct, the easiest way to do this is to iterate over the list of issues and the list of pull requests in a repository (I'm assuming you would like to get separate counts for each, reading between the lines of your question).

The issues API will return both issues and pull requests, so you will need to count both and subtract the number of pull requests from the number of issues to get the count of issues that aren't also pull requests. For example, using the wonderful github3.py Python library:

import github3

gh = github3.login(token='your_api_token')

issues_count = len(list(gh.repository('owner', 'repo').issues()))
pulls_count = len(list(gh.repository('owner', 'repo').pull_requests()))

print('{} issues, {} pull requests'.format(issues_count - pulls_count, pulls_count))
1
votes

You don't need to iterate over all pull requests. The GitHub API returns pages and in the link header, you have access to the first, previous, next and last pages. You can use that to implement a more efficient algorithm:

1) Fetch the first page and specify a page size of 1 2) Get the value of the last page link (i.e. the number of pages) 3) You will thereby have the number of pages, hence of PRs and will not have to fetch all of these pages.

0
votes

With Github Graphql API, you can now do this in one single request:

{
  repository(owner: "mui-org", name: "material-ui") {
    issues(states: OPEN) {
      totalCount
    }
    pullRequests(states: OPEN) {
      totalCount
    }
  }
}

Output:

{
  "data": {
    "repository": {
      "issues": {
        "totalCount": 471
      },
      "pullRequests": {
        "totalCount": 47
      }
    }
  }
}