Skip to content

Instantly share code, notes, and snippets.

@iamnader
Last active September 28, 2018 20:32
Show Gist options
  • Save iamnader/427a484f3eb333197d7b1e1c3f101025 to your computer and use it in GitHub Desktop.
Save iamnader/427a484f3eb333197d7b1e1c3f101025 to your computer and use it in GitHub Desktop.
Github Code Stats
require 'octokit'
require 'csv'
class GitStats
TOKEN = "<insert token>"
ORG = "<insert org>"
def process
process_repo_stats
process_contributors
process_punchcards
process_commits_over_time
end
def process_repo_stats
write_csv_file("repo_stats", repo_stats)
end
def process_contributors
write_csv_file("contributors", contributors)
end
def process_punchcards
write_csv_file("punchcards", punchcards)
end
def process_commits_over_time
write_csv_file("commits_over_time", commits_over_time)
end
private
def repos
@repos ||= client.org_repos(ORG, {:type => 'private'})
end
def process_repos(type)
repos.each do |r|
puts "*** Processing #{type}: #{r.name}"
begin
yield r
rescue StandardError => ex
puts "XXXX Error processing #{type} for #{r.name}: #{ex.message}"
end
end
end
def repo_stats
puts "**** STARTING PROCESSING REPO STATS ****"
contribs_output = [["Repo", "Contributors", "Commits"]]
process_repos("repo_stats") do |r|
contribs = client.contributors(r.id)
next unless contribs.is_a?(Array)
commit_count = contribs.inject(0) {|sum, c| sum += c.contributions}
contribs_output << [r.name, contribs.count, commit_count]
end
contribs_output
end
def contributors
puts "**** STARTING PROCESSING CONTRIBUTORS ****"
commits = [["Contributor", "Commits"]]
contrib_hash = {}
process_repos("contributors") do |r|
contribs = client.contributors(r.id)
next unless contribs.is_a?(Array)
contribs.each do |c|
contrib_hash[c.login] ||= 0
contrib_hash[c.login] = contrib_hash[c.login] + c.contributions
end
end
commits.concat contrib_hash.to_a
end
def commits_over_time
puts "**** STARTING PROCESSING COMMITS OVER TIME ****"
cot = [["Repo", "Commits over time"]]
commits = {}
process_repos("commits over time") do |r|
repo_stats = client.contributor_stats(r.id)
commits[r.name] ||= {}
repo_stats.each do |rs|
commits[r.name][rs.author.login] ||= {}
rs.weeks.each { |w| commits[r.name][rs.author.login][w.w] = w.c }
end
end
cot = [["Repo", "User", "Week", "Commits over time"]]
commits.each do |repo_name, repo_stats|
repo_stats.each do |user, weeks|
weeks.each do |week, commits|
cot << [repo_name, user, week, commits] if commits > 0
end
end
end
cot
end
def punchcards
puts "**** STARTING PROCESSING PUNCHCARDS ****"
repo_punches = []
process_repos("punchcards") do |r|
pc = client.punch_card(r.id)
repo_punches << pc
end
temp = {}
repo_punches.each do |punches|
punches.each do |punch|
punch_key = "#{punch[0]}-#{punch[1]}"
punch_value = punch[2]
temp[punch_key] ||= 0
temp[punch_key] = temp[punch_key] + punch_value
end
end
temp.map{ |k,v| k.split("-").map(&:to_i).concat([v])}
end
def write_csv_file(name, data)
CSV.open("#{name}.csv", "w") do |csv|
data.each { |d| csv << d }
end
end
def client
@client ||= begin
client = Octokit::Client.new(access_token: TOKEN)
client.auto_paginate = true
client
end
end
end
GitStats.new.process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment