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
|
|
|
|
extend T::Sig
|
|
|
|
|
|
|
|
module_function
|
|
|
|
|
2022-07-29 21:20:16 +01:00
|
|
|
SUPPORTED_REPOS = [
|
|
|
|
%w[brew core cask],
|
|
|
|
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
|
2022-07-24 22:06:00 +01:00
|
|
|
|
|
|
|
sig { returns(CLI::Parser) }
|
|
|
|
def contributions_args
|
|
|
|
Homebrew::CLI::Parser.new do
|
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
|
|
|
usage_banner "`contributions` <email|name> [<--repositories>`=`] [<--csv>]"
|
2022-07-28 12:50:04 +01:00
|
|
|
description <<~EOS
|
2022-07-24 22:06:00 +01:00
|
|
|
Contributions to Homebrew repos for a user.
|
2022-07-31 21:25:20 +01:00
|
|
|
|
2022-08-01 17:38:56 +01:00
|
|
|
The first argument is a name (e.g. "BrewTestBot") or an email address (e.g. "brewtestbot@brew.sh").
|
2022-07-24 22:06:00 +01:00
|
|
|
EOS
|
|
|
|
|
2022-08-03 17:01:52 +01:00
|
|
|
comma_array "--repositories",
|
|
|
|
description: "Specify a comma-separated (no spaces) list of repositories to search. " \
|
2022-08-03 18:26:30 +01:00
|
|
|
"Supported repositories: #{SUPPORTED_REPOS.map { |t| "`#{t}`" }.to_sentence}." \
|
2022-08-03 17:01:52 +01:00
|
|
|
"Omitting this flag, or specifying `--repositories=all`, will search all repositories."
|
2022-07-24 22:06:00 +01:00
|
|
|
flag "--from=",
|
|
|
|
description: "Date (ISO-8601 format) to start searching contributions."
|
|
|
|
|
|
|
|
flag "--to=",
|
|
|
|
description: "Date (ISO-8601 format) to stop searching contributions."
|
|
|
|
|
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",
|
|
|
|
description: "Print a CSV of a user's contributions across repositories over the time period."
|
|
|
|
|
2022-08-03 17:43:24 +01:00
|
|
|
named_args number: 1
|
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 = {}
|
2022-07-24 22:06:00 +01:00
|
|
|
|
2022-08-03 17:01:52 +01:00
|
|
|
all_repos = args.repositories.nil? || args.repositories.include?("all")
|
|
|
|
repos = all_repos ? SUPPORTED_REPOS : args.repositories
|
2022-07-31 21:25:20 +01:00
|
|
|
|
2022-07-28 11:28:14 +01:00
|
|
|
repos.each do |repo|
|
2022-07-28 12:00:27 +01:00
|
|
|
if SUPPORTED_REPOS.exclude?(repo)
|
2022-07-29 21:19:42 +01:00
|
|
|
return ofail "Unsupported repository: #{repo}. Try one of #{SUPPORTED_REPOS.join(", ")}."
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|
|
|
|
|
2022-07-24 22:44:25 +01:00
|
|
|
repo_path = find_repo_path_for_repo(repo)
|
2022-07-28 12:10:05 +01:00
|
|
|
unless repo_path.exist?
|
2022-07-29 21:19:42 +01:00
|
|
|
opoo "Repository #{repo} not yet tapped! Tapping it now..."
|
2022-07-29 21:49:59 +01:00
|
|
|
Tap.fetch("homebrew", repo).install
|
2022-07-28 11:28:14 +01:00
|
|
|
end
|
2022-07-24 22:44:25 +01: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
|
|
|
results[repo] = {
|
|
|
|
commits: git_log_author_cmd(T.must(repo_path), args),
|
|
|
|
coauthorships: git_log_trailers_cmd(T.must(repo_path), "Co-authored-by", args),
|
|
|
|
signoffs: git_log_trailers_cmd(T.must(repo_path), "Signed-off-by", args),
|
|
|
|
}
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|
|
|
|
|
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
|
|
|
puts "The user #{args.named.first} has made #{total(results)} contributions #{time_period(args)}."
|
|
|
|
puts generate_csv(args.named.first, results) if args.csv?
|
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
|
|
|
|
|
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
|
|
|
sig { params(args: Homebrew::CLI::Args).returns(String) }
|
|
|
|
def time_period(args)
|
|
|
|
if args.from && args.to
|
|
|
|
"between #{args.from} and #{args.to}"
|
|
|
|
elsif args.from
|
|
|
|
"after #{args.from}"
|
|
|
|
elsif args.to
|
|
|
|
"before #{args.to}"
|
|
|
|
else
|
|
|
|
"in all time"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { params(user: String, results: Hash).returns(String) }
|
|
|
|
def generate_csv(user, results)
|
|
|
|
CSV.generate do |csv|
|
dev-cmd/contributions: Add a per-repo total column to the CSV
```
$ brew contributions issyl0 --csv
The user issyl0 has made 1202 contributions in all time.
user,repo,commits,coauthorships,signoffs,total
issyl0,brew,332,13,0,345
issyl0,core,473,24,326,823
issyl0,cask,4,0,0,4
issyl0,aliases,0,0,0,0
issyl0,autoupdate,1,0,0,1
issyl0,bundle,14,2,0,16
issyl0,command-not-found,1,0,0,1
issyl0,test-bot,3,0,0,3
issyl0,services,9,0,0,9
issyl0,cask-drivers,0,0,0,0
issyl0,cask-fonts,0,0,0,0
issyl0,cask-versions,0,0,0,0
```
2023-02-15 13:56:37 +00:00
|
|
|
csv << %w[user repo commits coauthorships signoffs total]
|
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.each do |repo, counts|
|
dev-cmd/contributions: Add a per-repo total column to the CSV
```
$ brew contributions issyl0 --csv
The user issyl0 has made 1202 contributions in all time.
user,repo,commits,coauthorships,signoffs,total
issyl0,brew,332,13,0,345
issyl0,core,473,24,326,823
issyl0,cask,4,0,0,4
issyl0,aliases,0,0,0,0
issyl0,autoupdate,1,0,0,1
issyl0,bundle,14,2,0,16
issyl0,command-not-found,1,0,0,1
issyl0,test-bot,3,0,0,3
issyl0,services,9,0,0,9
issyl0,cask-drivers,0,0,0,0
issyl0,cask-fonts,0,0,0,0
issyl0,cask-versions,0,0,0,0
```
2023-02-15 13:56:37 +00:00
|
|
|
csv << [
|
|
|
|
user,
|
|
|
|
repo,
|
|
|
|
counts[:commits],
|
|
|
|
counts[:coauthorships],
|
|
|
|
counts[:signoffs],
|
|
|
|
counts.values.sum
|
|
|
|
]
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { params(results: Hash).returns(Integer) }
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2022-07-24 23:41:00 +01:00
|
|
|
sig { params(repo_path: Pathname, args: Homebrew::CLI::Args).returns(Integer) }
|
2022-07-24 23:24:52 +01:00
|
|
|
def git_log_author_cmd(repo_path, args)
|
2022-07-28 11:20:04 +01:00
|
|
|
cmd = ["git", "-C", repo_path, "log", "--oneline", "--author=#{args.named.first}"]
|
2022-07-30 00:36:31 +01:00
|
|
|
cmd << "--before=#{args.to}" if args.to
|
|
|
|
cmd << "--after=#{args.from}" if args.from
|
2022-07-24 23:24:52 +01:00
|
|
|
|
2022-07-28 11:20:04 +01:00
|
|
|
Utils.safe_popen_read(*cmd).lines.count
|
2022-07-24 23:24:52 +01:00
|
|
|
end
|
|
|
|
|
2023-02-11 11:39:31 +00:00
|
|
|
sig { params(repo_path: Pathname, trailer: String, args: Homebrew::CLI::Args).returns(Integer) }
|
|
|
|
def git_log_trailers_cmd(repo_path, trailer, args)
|
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}:)'"
|
2022-07-30 00:36:31 +01:00
|
|
|
cmd << "--before=#{args.to}" if args.to
|
|
|
|
cmd << "--after=#{args.from}" if args.from
|
2022-07-24 22:06:00 +01:00
|
|
|
|
2022-07-28 12:10:05 +01:00
|
|
|
Utils.safe_popen_read(*cmd).lines.count { |l| l.include?(args.named.first) }
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|
|
|
|
end
|