2022-07-24 22:06:00 +01:00
|
|
|
# typed: true
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "cli/parser"
|
dev-cmd/contributions: CSV output of queried repos; shorter sentence
- This gives users of this command a `--csv` option to pass to... you guessed
it, generate a CSV that's `pbcopy`able elsewhere, for more granular
breakdowns of where a person contributed.
- Inspiration was taken from the mockup in
https://github.com/Homebrew/brew/issues/13642#issuecomment-1254535251
but without the extra dependency of the TerminalTable gem.
- Always print a condensed "total contributions" sentence.
Output:
```
$ brew contributions issyl0
The user issyl0 has made 1201 contributions in all time.
$ brew contributions issyl0 --csv
user,repo,commits,coauthorships,signoffs
issyl0,brew,331,13,0
issyl0,core,473,24,326
issyl0,cask,4,0,0
issyl0,aliases,0,0,0
issyl0,autoupdate,1,0,0
issyl0,bundle,14,2,0
issyl0,command-not-found,1,0,0
issyl0,test-bot,3,0,0
issyl0,services,9,0,0
issyl0,cask-drivers,0,0,0
issyl0,cask-fonts,0,0,0
issyl0,cask-versions,0,0,0
```
2023-02-15 12:25:04 +00:00
|
|
|
require "csv"
|
2022-07-24 22:06:00 +01:00
|
|
|
|
|
|
|
module Homebrew
|
|
|
|
module_function
|
|
|
|
|
2023-02-19 18:26:07 +00:00
|
|
|
PRIMARY_REPOS = %w[brew core cask].freeze
|
2022-07-29 21:20:16 +01:00
|
|
|
SUPPORTED_REPOS = [
|
2023-02-19 18:26:07 +00:00
|
|
|
PRIMARY_REPOS,
|
2022-07-29 21:20:16 +01:00
|
|
|
OFFICIAL_CMD_TAPS.keys.map { |t| t.delete_prefix("homebrew/") },
|
2022-08-03 16:48:40 +01:00
|
|
|
OFFICIAL_CASK_TAPS.reject { |t| t == "cask" },
|
2022-07-29 21:20:16 +01:00
|
|
|
].flatten.freeze
|
2023-08-30 15:08:50 +01:00
|
|
|
MAX_REPO_COMMITS = 1000
|
2022-07-24 22:06:00 +01:00
|
|
|
|
|
|
|
sig { returns(CLI::Parser) }
|
|
|
|
def contributions_args
|
|
|
|
Homebrew::CLI::Parser.new do
|
2023-02-20 00:14:53 +00:00
|
|
|
usage_banner "`contributions` [--user=<email|username>] [<--repositories>`=`] [<--csv>]"
|
2022-07-28 12:50:04 +01:00
|
|
|
description <<~EOS
|
2023-09-08 14:46:15 -04:00
|
|
|
Summarise contributions to Homebrew repositories.
|
2022-07-24 22:06:00 +01:00
|
|
|
EOS
|
|
|
|
|
2022-08-03 17:01:52 +01:00
|
|
|
comma_array "--repositories",
|
2023-08-30 15:08:50 +01:00
|
|
|
description: "Specify a comma-separated list of repositories to search. " \
|
2023-02-20 16:16:08 +00:00
|
|
|
"Supported repositories: #{SUPPORTED_REPOS.map { |t| "`#{t}`" }.to_sentence}. " \
|
2023-08-30 15:08:50 +01:00
|
|
|
"Omitting this flag, or specifying `--repositories=primary`, searches only the " \
|
|
|
|
"main repositories: brew,core,cask. " \
|
|
|
|
"Specifying `--repositories=all`, searches all repositories. "
|
2022-07-24 22:06:00 +01:00
|
|
|
flag "--from=",
|
2023-08-30 15:08:50 +01:00
|
|
|
description: "Date (ISO-8601 format) to start searching contributions. " \
|
|
|
|
"Omitting this flag searches the last year."
|
2022-07-24 22:06:00 +01:00
|
|
|
|
|
|
|
flag "--to=",
|
|
|
|
description: "Date (ISO-8601 format) to stop searching contributions."
|
|
|
|
|
2023-08-30 15:08:50 +01:00
|
|
|
comma_array "--user=",
|
|
|
|
description: "Specify a comma-separated list of GitHub usernames or email addresses to find " \
|
|
|
|
"contributions from. Omitting this flag searches maintainers."
|
2023-02-20 00:14:53 +00:00
|
|
|
|
dev-cmd/contributions: CSV output of queried repos; shorter sentence
- This gives users of this command a `--csv` option to pass to... you guessed
it, generate a CSV that's `pbcopy`able elsewhere, for more granular
breakdowns of where a person contributed.
- Inspiration was taken from the mockup in
https://github.com/Homebrew/brew/issues/13642#issuecomment-1254535251
but without the extra dependency of the TerminalTable gem.
- Always print a condensed "total contributions" sentence.
Output:
```
$ brew contributions issyl0
The user issyl0 has made 1201 contributions in all time.
$ brew contributions issyl0 --csv
user,repo,commits,coauthorships,signoffs
issyl0,brew,331,13,0
issyl0,core,473,24,326
issyl0,cask,4,0,0
issyl0,aliases,0,0,0
issyl0,autoupdate,1,0,0
issyl0,bundle,14,2,0
issyl0,command-not-found,1,0,0
issyl0,test-bot,3,0,0
issyl0,services,9,0,0
issyl0,cask-drivers,0,0,0
issyl0,cask-fonts,0,0,0
issyl0,cask-versions,0,0,0
```
2023-02-15 12:25:04 +00:00
|
|
|
switch "--csv",
|
2023-02-23 23:27:38 +00:00
|
|
|
description: "Print a CSV of contributions across repositories over the time period."
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-03 10:19:03 +01:00
|
|
|
sig { void }
|
2022-07-24 22:06:00 +01:00
|
|
|
def contributions
|
|
|
|
args = contributions_args.parse
|
|
|
|
|
dev-cmd/contributions: CSV output of queried repos; shorter sentence
- This gives users of this command a `--csv` option to pass to... you guessed
it, generate a CSV that's `pbcopy`able elsewhere, for more granular
breakdowns of where a person contributed.
- Inspiration was taken from the mockup in
https://github.com/Homebrew/brew/issues/13642#issuecomment-1254535251
but without the extra dependency of the TerminalTable gem.
- Always print a condensed "total contributions" sentence.
Output:
```
$ brew contributions issyl0
The user issyl0 has made 1201 contributions in all time.
$ brew contributions issyl0 --csv
user,repo,commits,coauthorships,signoffs
issyl0,brew,331,13,0
issyl0,core,473,24,326
issyl0,cask,4,0,0
issyl0,aliases,0,0,0
issyl0,autoupdate,1,0,0
issyl0,bundle,14,2,0
issyl0,command-not-found,1,0,0
issyl0,test-bot,3,0,0
issyl0,services,9,0,0
issyl0,cask-drivers,0,0,0
issyl0,cask-fonts,0,0,0
issyl0,cask-versions,0,0,0
```
2023-02-15 12:25:04 +00:00
|
|
|
results = {}
|
2023-02-23 23:27:38 +00:00
|
|
|
grand_totals = {}
|
2022-07-24 22:06:00 +01:00
|
|
|
|
2023-08-30 15:08:50 +01:00
|
|
|
repos = if args.repositories.blank? || args.repositories.include?("primary")
|
2023-02-19 18:26:07 +00:00
|
|
|
PRIMARY_REPOS
|
2023-08-30 15:08:50 +01:00
|
|
|
elsif args.repositories.include?("all")
|
|
|
|
SUPPORTED_REPOS
|
2023-02-19 18:26:07 +00:00
|
|
|
else
|
|
|
|
args.repositories
|
|
|
|
end
|
2022-07-31 21:25:20 +01:00
|
|
|
|
2023-08-30 15:08:50 +01:00
|
|
|
from = args.from.presence || Date.today.prev_year.iso8601
|
2023-02-23 23:27:38 +00:00
|
|
|
|
2023-08-30 15:08:50 +01:00
|
|
|
contribution_types = [:author, :committer, :coauthorship, :review]
|
2023-02-22 16:48:31 +00:00
|
|
|
|
2023-12-14 02:52:30 +00:00
|
|
|
users = args.user.presence || GitHub.members_by_team("Homebrew", "maintainers").keys
|
|
|
|
users.each do |username|
|
2023-02-20 00:14:53 +00:00
|
|
|
# TODO: Using the GitHub username to scan the `git log` undercounts some
|
|
|
|
# contributions as people might not always have configured their Git
|
|
|
|
# committer details to match the ones on GitHub.
|
|
|
|
# TODO: Switch to using the GitHub APIs instead of `git log` if
|
|
|
|
# they ever support trailers.
|
2023-08-30 15:08:50 +01:00
|
|
|
results[username] = scan_repositories(repos, username, args, from: from)
|
2023-02-23 23:27:38 +00:00
|
|
|
grand_totals[username] = total(results[username])
|
|
|
|
|
2024-02-22 23:29:55 +00:00
|
|
|
contributions = contribution_types.filter_map do |type|
|
2023-08-30 15:08:50 +01:00
|
|
|
type_count = grand_totals[username][type]
|
|
|
|
next if type_count.to_i.zero?
|
|
|
|
|
|
|
|
"#{Utils.pluralize("time", type_count, include_count: true)} (#{type})"
|
2024-02-22 23:29:55 +00:00
|
|
|
end
|
2023-08-30 15:08:50 +01:00
|
|
|
contributions << "#{Utils.pluralize("time", grand_totals[username].values.sum, include_count: true)} (total)"
|
|
|
|
|
|
|
|
puts [
|
|
|
|
"#{username} contributed",
|
|
|
|
*contributions.to_sentence,
|
|
|
|
"#{time_period(from: from, to: args.to)}.",
|
|
|
|
].join(" ")
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|
2023-02-23 23:27:38 +00:00
|
|
|
|
2023-08-30 15:08:50 +01:00
|
|
|
return unless args.csv?
|
|
|
|
|
|
|
|
puts
|
|
|
|
puts generate_csv(grand_totals)
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|
|
|
|
|
2022-07-24 23:41:00 +01:00
|
|
|
sig { params(repo: String).returns(Pathname) }
|
2022-07-24 22:06:00 +01:00
|
|
|
def find_repo_path_for_repo(repo)
|
2022-07-24 23:18:27 +01:00
|
|
|
return HOMEBREW_REPOSITORY if repo == "brew"
|
|
|
|
|
|
|
|
Tap.fetch("homebrew", repo).path
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|
|
|
|
|
2023-08-30 15:08:50 +01:00
|
|
|
sig { params(from: T.nilable(String), to: T.nilable(String)).returns(String) }
|
|
|
|
def time_period(from:, to:)
|
|
|
|
if from && to
|
|
|
|
"between #{from} and #{to}"
|
|
|
|
elsif from
|
|
|
|
"after #{from}"
|
|
|
|
elsif to
|
|
|
|
"before #{to}"
|
dev-cmd/contributions: CSV output of queried repos; shorter sentence
- This gives users of this command a `--csv` option to pass to... you guessed
it, generate a CSV that's `pbcopy`able elsewhere, for more granular
breakdowns of where a person contributed.
- Inspiration was taken from the mockup in
https://github.com/Homebrew/brew/issues/13642#issuecomment-1254535251
but without the extra dependency of the TerminalTable gem.
- Always print a condensed "total contributions" sentence.
Output:
```
$ brew contributions issyl0
The user issyl0 has made 1201 contributions in all time.
$ brew contributions issyl0 --csv
user,repo,commits,coauthorships,signoffs
issyl0,brew,331,13,0
issyl0,core,473,24,326
issyl0,cask,4,0,0
issyl0,aliases,0,0,0
issyl0,autoupdate,1,0,0
issyl0,bundle,14,2,0
issyl0,command-not-found,1,0,0
issyl0,test-bot,3,0,0
issyl0,services,9,0,0
issyl0,cask-drivers,0,0,0
issyl0,cask-fonts,0,0,0
issyl0,cask-versions,0,0,0
```
2023-02-15 12:25:04 +00:00
|
|
|
else
|
|
|
|
"in all time"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-23 23:27:38 +00:00
|
|
|
sig { params(totals: Hash).returns(String) }
|
2023-08-30 15:08:50 +01:00
|
|
|
def generate_csv(totals)
|
2023-02-23 23:27:38 +00:00
|
|
|
CSV.generate do |csv|
|
2023-08-30 15:08:50 +01:00
|
|
|
csv << %w[user repo author committer coauthorship review total]
|
2023-02-25 00:29:37 +00:00
|
|
|
|
2023-02-25 18:04:01 +00:00
|
|
|
totals.sort_by { |_, v| -v.values.sum }.each do |user, total|
|
2023-02-25 00:29:37 +00:00
|
|
|
csv << grand_total_row(user, total)
|
2023-02-23 23:27:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-25 00:29:37 +00:00
|
|
|
sig { params(user: String, grand_total: Hash).returns(Array) }
|
|
|
|
def grand_total_row(user, grand_total)
|
|
|
|
[
|
|
|
|
user,
|
|
|
|
"all",
|
2023-03-01 23:38:49 +00:00
|
|
|
grand_total[:author],
|
|
|
|
grand_total[:committer],
|
2023-08-30 15:08:50 +01:00
|
|
|
grand_total[:coauthorship],
|
|
|
|
grand_total[:review],
|
2023-02-25 00:29:37 +00:00
|
|
|
grand_total.values.sum,
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2023-08-30 15:08:50 +01:00
|
|
|
def scan_repositories(repos, person, args, from:)
|
2023-02-20 00:14:53 +00:00
|
|
|
data = {}
|
|
|
|
|
|
|
|
repos.each do |repo|
|
|
|
|
if SUPPORTED_REPOS.exclude?(repo)
|
|
|
|
return ofail "Unsupported repository: #{repo}. Try one of #{SUPPORTED_REPOS.join(", ")}."
|
|
|
|
end
|
|
|
|
|
|
|
|
repo_path = find_repo_path_for_repo(repo)
|
|
|
|
tap = Tap.fetch("homebrew", repo)
|
|
|
|
unless repo_path.exist?
|
|
|
|
opoo "Repository #{repo} not yet tapped! Tapping it now..."
|
|
|
|
tap.install
|
|
|
|
end
|
|
|
|
|
|
|
|
repo_full_name = if repo == "brew"
|
|
|
|
"homebrew/brew"
|
|
|
|
else
|
|
|
|
tap.full_name
|
|
|
|
end
|
|
|
|
|
2023-02-22 16:48:31 +00:00
|
|
|
puts "Determining contributions for #{person} on #{repo_full_name}..." if args.verbose?
|
|
|
|
|
2023-08-30 15:08:50 +01:00
|
|
|
author_commits, committer_commits = GitHub.count_repo_commits(repo_full_name, person, args,
|
|
|
|
max: MAX_REPO_COMMITS)
|
2023-02-20 00:14:53 +00:00
|
|
|
data[repo] = {
|
2023-08-30 15:08:50 +01:00
|
|
|
author: author_commits,
|
|
|
|
committer: committer_commits,
|
|
|
|
coauthorship: git_log_trailers_cmd(T.must(repo_path), person, "Co-authored-by", from: from, to: args.to),
|
|
|
|
review: count_reviews(repo_full_name, person, args),
|
2023-02-20 00:14:53 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2023-02-25 00:29:37 +00:00
|
|
|
sig { params(results: Hash).returns(Hash) }
|
dev-cmd/contributions: CSV output of queried repos; shorter sentence
- This gives users of this command a `--csv` option to pass to... you guessed
it, generate a CSV that's `pbcopy`able elsewhere, for more granular
breakdowns of where a person contributed.
- Inspiration was taken from the mockup in
https://github.com/Homebrew/brew/issues/13642#issuecomment-1254535251
but without the extra dependency of the TerminalTable gem.
- Always print a condensed "total contributions" sentence.
Output:
```
$ brew contributions issyl0
The user issyl0 has made 1201 contributions in all time.
$ brew contributions issyl0 --csv
user,repo,commits,coauthorships,signoffs
issyl0,brew,331,13,0
issyl0,core,473,24,326
issyl0,cask,4,0,0
issyl0,aliases,0,0,0
issyl0,autoupdate,1,0,0
issyl0,bundle,14,2,0
issyl0,command-not-found,1,0,0
issyl0,test-bot,3,0,0
issyl0,services,9,0,0
issyl0,cask-drivers,0,0,0
issyl0,cask-fonts,0,0,0
issyl0,cask-versions,0,0,0
```
2023-02-15 12:25:04 +00:00
|
|
|
def total(results)
|
2023-08-30 15:08:50 +01:00
|
|
|
totals = { author: 0, committer: 0, coauthorship: 0, review: 0 }
|
2023-02-25 00:29:37 +00:00
|
|
|
|
|
|
|
results.each_value do |counts|
|
|
|
|
counts.each do |kind, count|
|
|
|
|
totals[kind] += count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-01 23:45:08 +00:00
|
|
|
totals
|
dev-cmd/contributions: CSV output of queried repos; shorter sentence
- This gives users of this command a `--csv` option to pass to... you guessed
it, generate a CSV that's `pbcopy`able elsewhere, for more granular
breakdowns of where a person contributed.
- Inspiration was taken from the mockup in
https://github.com/Homebrew/brew/issues/13642#issuecomment-1254535251
but without the extra dependency of the TerminalTable gem.
- Always print a condensed "total contributions" sentence.
Output:
```
$ brew contributions issyl0
The user issyl0 has made 1201 contributions in all time.
$ brew contributions issyl0 --csv
user,repo,commits,coauthorships,signoffs
issyl0,brew,331,13,0
issyl0,core,473,24,326
issyl0,cask,4,0,0
issyl0,aliases,0,0,0
issyl0,autoupdate,1,0,0
issyl0,bundle,14,2,0
issyl0,command-not-found,1,0,0
issyl0,test-bot,3,0,0
issyl0,services,9,0,0
issyl0,cask-drivers,0,0,0
issyl0,cask-fonts,0,0,0
issyl0,cask-versions,0,0,0
```
2023-02-15 12:25:04 +00:00
|
|
|
end
|
|
|
|
|
2023-08-30 15:08:50 +01:00
|
|
|
sig {
|
|
|
|
params(repo_path: Pathname, person: String, trailer: String, from: T.nilable(String),
|
|
|
|
to: T.nilable(String)).returns(Integer)
|
|
|
|
}
|
|
|
|
def git_log_trailers_cmd(repo_path, person, trailer, from:, to:)
|
2022-07-28 11:20:04 +01:00
|
|
|
cmd = ["git", "-C", repo_path, "log", "--oneline"]
|
2023-02-11 11:39:31 +00:00
|
|
|
cmd << "--format='%(trailers:key=#{trailer}:)'"
|
2023-08-30 15:08:50 +01:00
|
|
|
cmd << "--before=#{to}" if to
|
|
|
|
cmd << "--after=#{from}" if from
|
2022-07-24 22:06:00 +01:00
|
|
|
|
2023-02-20 00:14:53 +00:00
|
|
|
Utils.safe_popen_read(*cmd).lines.count { |l| l.include?(person) }
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|
2023-03-14 21:17:34 +00:00
|
|
|
|
|
|
|
sig { params(repo_full_name: String, person: String, args: Homebrew::CLI::Args).returns(Integer) }
|
|
|
|
def count_reviews(repo_full_name, person, args)
|
|
|
|
GitHub.count_issues("", is: "pr", repo: repo_full_name, reviewed_by: person, review: "approved", args: args)
|
2023-03-15 12:59:21 +00:00
|
|
|
rescue GitHub::API::ValidationFailedError
|
2023-03-15 21:31:41 +00:00
|
|
|
if args.verbose?
|
|
|
|
onoe "Couldn't search GitHub for PRs by #{person}. Their profile might be private. Defaulting to 0."
|
|
|
|
end
|
2023-03-15 12:59:21 +00:00
|
|
|
0 # Users who have made their contributions private are not searchable to determine counts.
|
2023-03-14 21:17:34 +00:00
|
|
|
end
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|