Get all open pull requests from an organisation using the Github API Ruby gem

github, github-api, ruby

Solution

OK, so I think I've cracked this now. Pull requests are issues, so I can get all issues, and loop through the issues like so:

pulls = 0
issues = connection.issues.list(:org => GITHUB_ORGANISATION, :filter => 'all', :auto_pagination => true)
issues.each do |issue|
  if issue["pull_request"]
    pulls += 1
  end
end

Once you remember that pull requests are issues too, everything just falls into place.

Problem

For our organisation's dashboard, I'd like to keep a count of all the open PRs on all our repositories. At the moment, all I've got is to loop through all the repos, and count through all the open PRs on each repo like so (which often results in a rate limit error): ``` connection = Github.new oauth_token: MY_OAUTH_TOKEN pulls = 0 connection.repos.list(:org => GITHUB_ORGANISATION).each do |repo| pulls += connection.pull_requests.list(:user => repo['owner']['login'], :repo => repo['name']).count end ``` I know there must be a nicer way round this. Any ideas? (short of screen scraping!)

Original source