Last active
June 1, 2021 19:32
-
-
Save Dawenster/55878aa33a529bbec418f471f162b88a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env/ruby pr-summary.rb | |
# USAGE: | |
# ./pr-summary.rb QUERY_TERM1 [QUERY_TERM2] ... | |
# Example: ./pr-summary.rb is:closed 'created:>2017-10-01' repo:aptible/foo | |
# This will print a CSV summary of all PRs opened after 10/1/17, now closed, | |
# and in the aptible/foo repo | |
# Example: | |
# GITHUB_TOKEN=TOKEN rails runner pr-summary.rb is:closed 'created:<2019-10-10' org:aptible | |
# This will print for all closed PRs at the org 'aptible' before 2019-10-10 | |
# NOTES AND GOTCHA's: SKIP AT YOUR OWN PERIL! | |
# 1. In order to authenticate to GitHub, ENV['GITHUB_TOKEN'] must be set to a | |
# valid, permissioned OAuth/Personal access token; | |
# 2. Make sure to enable SSO on your personal access token; | |
# 3. If there are more than 1000 PRs in the query, you will need to run the | |
# script multiple times, each time changing the 'created' date; | |
# 4. Github is deprecating API authentication through query parameters: | |
# https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param/; | |
# 5. If this doesn't work from the shell, run it in rails console. | |
require 'uri_template' | |
require 'csv' | |
require 'uri' | |
require 'net/http' | |
require 'json' | |
TOKEN = ENV.fetch('GITHUB_TOKEN') | |
template = URITemplate.new('/search/issues{?q,access_token,page}') | |
query = (%w(is:pr) + ARGV).join(' ') | |
page = 1 | |
sleeps = 0 | |
max_sleeps = 5 | |
output = CSV.generate do |csv| | |
csv << %w(url title date) | |
loop do | |
path = template.expand(q: query, access_token: TOKEN, page: page) | |
uri = URI("https://api.github.com#{path}") | |
begin | |
json = JSON.parse(Net::HTTP.get(uri)) | |
break unless json['items'].any? | |
rescue => e | |
if sleeps < max_sleeps && json['message'].include?('API rate limit exceeded') | |
sleeps += 1 | |
sleep 3 | |
retry | |
end | |
break | |
end | |
json['items'].each do |item| | |
csv << [item['html_url'], item['title'], item['created_at']] | |
end | |
page += 1 | |
end | |
end | |
puts output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment