diff --git a/Library/Homebrew/dev-cmd/contributions.rb b/Library/Homebrew/dev-cmd/contributions.rb index 035c16a2f7..868e10f43b 100755 --- a/Library/Homebrew/dev-cmd/contributions.rb +++ b/Library/Homebrew/dev-cmd/contributions.rb @@ -39,7 +39,7 @@ module Homebrew description: "A GitHub username or email address of a specific person to find contribution data for." switch "--csv", - description: "Print a CSV of a user's contributions across repositories over the time period." + description: "Print a CSV of contributions across repositories over the time period." end end @@ -48,6 +48,7 @@ module Homebrew args = contributions_args.parse results = {} + grand_totals = {} all_repos = args.repositories.nil? || args.repositories.include?("all") repos = if all_repos @@ -61,13 +62,13 @@ module Homebrew if args.user user = args.user results[user] = scan_repositories(repos, user, args) - puts "#{user} contributed #{total(results[user])} times #{time_period(args)}." - puts generate_csv(T.must(user), results[user]) if args.csv? + grand_totals[user] = total(results[user]) + + puts "#{user} contributed #{grand_totals[user].values.sum} times #{time_period(args)}." + puts generate_csv(T.must(user), results[user], grand_totals[user]) if args.csv? return end - odie "CSVs not yet supported for the full list of maintainers at once." if args.csv? - maintainers = GitHub.members_by_team("Homebrew", "maintainers") maintainers.each do |username, _| # TODO: Using the GitHub username to scan the `git log` undercounts some @@ -76,8 +77,12 @@ module Homebrew # TODO: Switch to using the GitHub APIs instead of `git log` if # they ever support trailers. results[username] = scan_repositories(repos, username, args) - puts "#{username} contributed #{total(results[username])} times #{time_period(args)}." + grand_totals[username] = total(results[username]) + + puts "#{username} contributed #{grand_totals[username].values.sum} times #{time_period(args)}." end + + puts generate_maintainers_csv(grand_totals) if args.csv? end sig { params(repo: String).returns(Pathname) } @@ -100,8 +105,19 @@ module Homebrew end end - sig { params(user: String, results: Hash).returns(String) } - def generate_csv(user, results) + sig { params(totals: Hash).returns(String) } + def generate_maintainers_csv(totals) + CSV.generate do |csv| + csv << %w[user repo commits coauthorships signoffs total] + + totals.each do |user, total| + csv << grand_total_row(user, total) + end + end + end + + sig { params(user: String, results: Hash, grand_total: Hash).returns(String) } + def generate_csv(user, results, grand_total) CSV.generate do |csv| csv << %w[user repo commits coauthorships signoffs total] results.each do |repo, counts| @@ -114,10 +130,22 @@ module Homebrew counts.values.sum, ] end - csv << [user, "*", "*", "*", "*", total(results)] + csv << grand_total_row(user, grand_total) end end + sig { params(user: String, grand_total: Hash).returns(Array) } + def grand_total_row(user, grand_total) + [ + user, + "all", + grand_total[:commits], + grand_total[:coauthorships], + grand_total[:signoffs], + grand_total.values.sum, + ] + end + def scan_repositories(repos, person, args) data = {} @@ -151,12 +179,18 @@ module Homebrew data end - sig { params(results: Hash).returns(Integer) } + sig { params(results: Hash).returns(Hash) } def total(results) - results - .values # [{:commits=>1, :coauthorships=>0, :signoffs=>3}, {:commits=>500, :coauthorships=>2, :signoffs=>450}] - .map(&:values) # [[1, 0, 3], [500, 2, 450]] - .sum(&:sum) # 956 + totals = { commits: 0, coauthorships: 0, signoffs: 0 } + + # {"brew"=>{:commits=>9,:coauthorships=>6,:signoffs=>3},"core"=>{:commits=>15,:coauthorships=>10,:signoffs=>5}} + results.each_value do |counts| + counts.each do |kind, count| + totals[kind] += count + end + end + + totals # {:commits=>24,:coauthorships=>16,signoffs=>8} end sig { params(repo_path: Pathname, person: String, trailer: String, args: Homebrew::CLI::Args).returns(Integer) }