Created
December 7, 2015 21:56
-
-
Save olkyled/bb3d9c63812340c01fea to your computer and use it in GitHub Desktop.
PR Counter
This file contains 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
source 'https://rubygems.org' | |
gem 'github_api' |
This file contains 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
require 'github_api' | |
require 'date' | |
class Repo < Struct.new(:owner, :name); end | |
class Result < Struct.new(:created_after, :count) | |
def to_s | |
"PRs Created After #{created_after}: #{count}" | |
end | |
end | |
class PRCounter | |
def initialize(gh_client) | |
@client = gh_client | |
@results = {} | |
end | |
def add_interval(created_after) | |
@results[created_after] = 0 | |
end | |
def results | |
all_repos.each do |repo| | |
pull_requests(repo) do |pr| | |
@results.keys.each do |ts| | |
@results[ts] += 1 if pr_created_in_interval?(pr, ts) | |
end | |
end | |
end | |
results = [] | |
@results.each do |ts, count| | |
results << Result.new(ts, count) | |
end | |
return results | |
end | |
private | |
def all_repos | |
repos = [] | |
@client.repos.list(auto_pagination: true).map { |r| repos << Repo.new(r.owner.login, r.name) } | |
return repos | |
end | |
def pull_requests(repo) | |
prs = [] | |
@client.pull_requests.list(user: repo.owner, repo: repo.name, state: 'all', auto_pagination: true).each do |pr| | |
prs << pr | |
yield(pr) if block_given? | |
end | |
return prs | |
end | |
def pr_created_in_interval?(pr, start_date) | |
t = Time.parse(pr.created_at) | |
return t >= start_date.to_time | |
end | |
end | |
unless oauth = ARGV[0] | |
raise "You must provide a GitHub OAuth token as the only argument" | |
end | |
client = Github.new(basic_auth: oauth) | |
counter = PRCounter.new(client) | |
counter.add_interval(Date.new(2015, 6, 1)) | |
counter.add_interval(Date.new(2015, 9, 1)) | |
results = counter.results | |
results.each { |r| puts r } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment