Skip to content

Instantly share code, notes, and snippets.

@JaredMiller
Created May 11, 2012 13:41
Show Gist options
  • Save JaredMiller/2659713 to your computer and use it in GitHub Desktop.
Save JaredMiller/2659713 to your computer and use it in GitHub Desktop.
require 'json'
require 'httparty'
require 'redis'
require 'resque'
require 'securerandom'
require 'csv'
class ResponseEnumerator
include Enumerable
def initialize(party, response)
@party = party
@response = response
end
def each
@response.each do |e|
yield(e)
end
while next_page do
@response.each do |e|
yield(e)
end
end
end
private
def next_page
if /<(?<url>[^>]+)>; rel="next"/ =~ @response.headers['link'] then
@response = @party.get(url)
end
end
end
class GitHub
include HTTParty
base_uri 'https://api.github.com/repos/<userORcompany>/<repo>'
basic_auth '<username>', '<password>'
#debug_output
def self.pulls
ResponseEnumerator.new(self, get("/pulls", :query => {"state" => "closed"} )) #2012-05-10T14:42Z
end
def self.get_pull(number)
get "/pulls/#{number}"
end
def self.pull_files(number)
get "/pulls/#{number}/files"
end
def self.delete_comment(number)
delete "/issues/comments/#{number}"
end
def self.notify_started(number, url)
self.comment number, "Testing: #{url}"
end
def self.notify_status(number, status, head, base, url)
url << 'console' unless status
self.comment number, <<-EOD.undent
> Testing: #{url}
:#{status ? '+' : '-'}1:
Merged #{head} into #{base} and #{status ? 'won' : 'FAILED'}.
EOD
end
def self.comment_on_file(number, hash, file, position, body)
post "/pulls/#{number}/comments", :body => {:body => body, :commit_id => hash, :path => file, :position => position}.to_json
end
def self.comment(number, body)
post "/issues/#{number}/comments", :body => {:body => body}.to_json
end
def self.pull_comments(number)
get "/issues/#{number}/comments"
end
def self.parse_diff(patch)
patch ||= ''
ranges = []
i = 1
patch.each_line do |line|
line.match(/^@@ -\d+,\d+ \+(\d+),(\d+) @@/) do |m|
ranges << {:position => i, :range => Range.new(m[1].to_i, m[1].to_i + m[2].to_i)}
end
i += 1
end
{:patch => patch, :ranges => ranges}
end
end
puts "Link, submitter, merger, creation date, merge date, num comments, num commits, num additions, num deletions, num changed files"
#Use this to get each pull request magically
# GitHub.pulls.each do |p|
#use this if you want it to be reliable and have a good idea of which PR's you want
(3..5000).each do |i|
#puts JSON.pretty_generate(p)
pull = GitHub.get_pull(i)
if !pull.nil? and !pull['merged_at'].nil?
puts pull['_links']['html']['href'] + "," + pull['user']['login'] + "," + pull['merged_by']['login'] + "," + pull['created_at'] + "," + pull['merged_at'] + "," + pull['comments'].to_s + "," + pull['commits'].to_s + "," + pull['additions'].to_s + "," + pull['deletions'].to_s + "," + pull['changed_files'].to_s
end
#end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment