n9iels
n9iels

Reputation: 895

Count open pull requests and issues on GitHub

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?

Upvotes: 4

Views: 3255

Answers (5)

bpiv400
bpiv400

Reputation: 11

In case anyone is wondering, the accepted answer of constructing a search query with the Github Search API can take an is:merged argument instead of is:open, even though that option isn't well documented in the Github API:

For example:

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

Upvotes: 0

Bertrand Martel
Bertrand Martel

Reputation: 45362

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
      }
    }
  }
}

Upvotes: 2

bmunk
bmunk

Reputation: 1578

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.

Upvotes: 4

Olivier Liechti
Olivier Liechti

Reputation: 3188

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.

Upvotes: 1

kfb
kfb

Reputation: 6532

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))

Upvotes: 2

Related Questions