Search code examples
githubgithub-api

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?


Solution

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