Last active
December 1, 2022 16:50
-
-
Save doyle/7553598bd738bcf6200ea82a8db924ad to your computer and use it in GitHub Desktop.
Pulls a list of an author's contributions to a GitHub organization
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 'octokit' | |
def gh_stats(org, author) | |
repos = repos(org, 'private') | |
repos += repos(org, 'public') | |
total_additions = 0 | |
total_deletions = 0 | |
total_commits = 0 | |
repos.each do |repo| | |
id = repo[:id] | |
puts "Getting stats for #{repo[:name]} #{id}" | |
stats = client.contributor_stats(id, retry_timeout: 10, retry_wait: 1) | |
if stats.nil? | |
puts 'Failed to get stats' | |
end | |
next if stats.nil? | |
author_stats = stats.detect{|s| s[:author][:login] == author} | |
if !author_stats.nil? | |
additions = author_stats[:weeks].map(&:a).reduce(:+) | |
deletions = author_stats[:weeks].map(&:d).reduce(:+) | |
commits = author_stats[:weeks].map(&:c).reduce(:+) | |
total_additions += additions | |
total_deletions += deletions | |
total_commits += commits | |
puts "Additions: #{additions}" | |
puts "Deletions: #{deletions}" | |
puts "Commits: #{commits}" | |
puts "" | |
end | |
end | |
puts "Repo count: #{repos.size}" | |
puts "Additions: #{total_additions}" | |
puts "Deletions: #{total_deletions}" | |
puts "Commits: #{total_commits}" | |
end | |
def client | |
client = Octokit::Client.new(access_token: '<redacted>') | |
end | |
def repos(org, type) | |
page = 1 | |
per_page = 50 | |
all_repos = [] | |
repos = client.org_repos(org, {type: type, page: page, per_page: per_page}) | |
while repos.any? | |
all_repos += repos | |
page += 1 | |
repos = client.org_repos(org, {type: type, page: page, per_page: per_page}) | |
end | |
all_repos | |
end |
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 'csv' | |
require 'octokit' | |
def gh_stats_csv(org, author) | |
repos = repos(org, 'private') | |
repos += repos(org, 'public') | |
CSV.open("#{org}_#{author}_stats.csv", 'wb') do |csv| | |
csv << ['repo id', 'repo name', 'additions', 'deletions', 'commits'] | |
repos.each do |repo| | |
name = repo[:name] | |
id = repo[:id] | |
stats = client.contributor_stats(id, retry_timeout: 10, retry_wait: 1) | |
if stats.nil? | |
csv << [id, name, '-', '-', '-'] | |
end | |
next if stats.nil? | |
author_stats = stats.detect{|s| s[:author][:login] == author} | |
if author_stats.nil? | |
csv << [id, name, 0, 0, 0] | |
else | |
additions = author_stats[:weeks].map(&:a).reduce(:+) | |
deletions = author_stats[:weeks].map(&:d).reduce(:+) | |
commits = author_stats[:weeks].map(&:c).reduce(:+) | |
csv << [id, name, additions, deletions, commits] | |
end | |
end | |
end | |
end | |
def client | |
client = Octokit::Client.new(:access_token => '<redacted>') | |
end | |
def repos(org, type) | |
page = 1 | |
per_page = 50 | |
all_repos = [] | |
repos = client.org_repos(org, {type: type, page: page, per_page: per_page}) | |
while repos.any? | |
all_repos += repos | |
page += 1 | |
repos = client.org_repos(org, {type: type, page: page, per_page: per_page}) | |
end | |
all_repos | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment