2024-06-30 18:42:16 +01:00
|
|
|
# typed: strict
|
2022-07-24 22:06:00 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-03-21 08:08:53 -07:00
|
|
|
require "abstract_command"
|
2022-07-24 22:06:00 +01:00
|
|
|
|
|
|
|
module Homebrew
|
2024-03-19 11:57:54 -07:00
|
|
|
module DevCmd
|
|
|
|
class Contributions < AbstractCommand
|
2025-09-10 15:32:06 +01:00
|
|
|
PRIMARY_REPOS = T.let(%w[
|
|
|
|
Homebrew/brew
|
|
|
|
Homebrew/homebrew-core
|
|
|
|
Homebrew/homebrew-cask
|
|
|
|
].freeze, T::Array[String])
|
|
|
|
ALL_REPOS = T.let([
|
|
|
|
*PRIMARY_REPOS,
|
|
|
|
*OFFICIAL_CMD_TAPS.keys,
|
|
|
|
].freeze, T::Array[String])
|
|
|
|
CONTRIBUTION_TYPES = T.let({
|
|
|
|
merged_pr_author: "merged PR author",
|
|
|
|
approved_pr_review: "approved PR reviewer",
|
|
|
|
committer: "commit author or committer",
|
|
|
|
coauthor: "commit coauthor",
|
|
|
|
}.freeze, T::Hash[Symbol, String])
|
|
|
|
MAX_COMMITS = T.let(1000, Integer)
|
|
|
|
MAX_PR_SEARCH = T.let(100, Integer)
|
2024-03-19 11:57:54 -07:00
|
|
|
|
|
|
|
cmd_args do
|
2025-06-03 13:01:49 -04:00
|
|
|
usage_banner "`contributions` [`--user=`] [`--repositories=`] [`--from=`] [`--to=`] [`--csv`]"
|
2024-03-19 11:57:54 -07:00
|
|
|
description <<~EOS
|
|
|
|
Summarise contributions to Homebrew repositories.
|
|
|
|
EOS
|
2025-06-03 13:01:49 -04:00
|
|
|
comma_array "--user=",
|
|
|
|
description: "Specify a comma-separated list of GitHub usernames or email addresses to find " \
|
|
|
|
"contributions from. Omitting this flag searches Homebrew maintainers."
|
2024-03-19 11:57:54 -07:00
|
|
|
comma_array "--repositories",
|
|
|
|
description: "Specify a comma-separated list of repositories to search. " \
|
2025-09-10 15:32:06 +01:00
|
|
|
"All repositories must be under the same user or organisation. " \
|
2024-03-19 11:57:54 -07:00
|
|
|
"Omitting this flag, or specifying `--repositories=primary`, searches only the " \
|
2025-09-10 15:32:06 +01:00
|
|
|
"main repositories: `Homebrew/brew`, `Homebrew/homebrew-core`, " \
|
|
|
|
"`Homebrew/homebrew-cask`. Specifying `--repositories=all` searches all " \
|
|
|
|
"non-deprecated Homebrew repositories. "
|
|
|
|
flag "--organisation=", "--organization=", "--org=",
|
|
|
|
description: "Specify the organisation to populate sources repositories from. " \
|
|
|
|
"Omitting this flag searches the Homebrew primary repositories."
|
2025-09-11 11:36:48 +01:00
|
|
|
flag "--team=",
|
|
|
|
description: "Specify the team to populate users from. " \
|
|
|
|
"The first part of the team name will be used as the organisation."
|
2025-06-03 13:01:49 -04:00
|
|
|
flag "--from=",
|
|
|
|
description: "Date (ISO 8601 format) to start searching contributions. " \
|
|
|
|
"Omitting this flag searches the past year."
|
|
|
|
flag "--to=",
|
|
|
|
description: "Date (ISO 8601 format) to stop searching contributions."
|
2024-03-19 11:57:54 -07:00
|
|
|
switch "--csv",
|
|
|
|
description: "Print a CSV of contributions across repositories over the time period."
|
2025-09-10 15:32:06 +01:00
|
|
|
conflicts "--organisation", "--repositories"
|
2025-09-11 11:36:48 +01:00
|
|
|
conflicts "--organisation", "--team"
|
|
|
|
conflicts "--user", "--team"
|
2024-02-22 23:29:55 +00:00
|
|
|
end
|
2023-08-30 15:08:50 +01:00
|
|
|
|
2024-03-19 11:57:54 -07:00
|
|
|
sig { override.void }
|
|
|
|
def run
|
2025-09-10 15:32:06 +01:00
|
|
|
odie "Cannot get contributions as `$HOMEBREW_NO_GITHUB_API` is set!" if Homebrew::EnvConfig.no_github_api?
|
|
|
|
|
2025-04-22 19:23:10 +01:00
|
|
|
Homebrew.install_bundler_gems!(groups: ["contributions"]) if args.csv?
|
|
|
|
|
2025-09-11 11:36:48 +01:00
|
|
|
require "utils/github"
|
|
|
|
|
2024-03-19 11:57:54 -07:00
|
|
|
results = {}
|
|
|
|
grand_totals = {}
|
2025-09-10 15:32:06 +01:00
|
|
|
from = args.from.presence || Date.today.prev_year.iso8601
|
|
|
|
to = args.to.presence || (Date.today + 1).iso8601
|
|
|
|
organisation = nil
|
2025-09-11 11:36:48 +01:00
|
|
|
|
|
|
|
users = if (team = args.team.presence)
|
|
|
|
team_sections = team.split("/")
|
|
|
|
organisation = team_sections.first.presence
|
|
|
|
team_name = team_sections.last.presence
|
|
|
|
if team_sections.length != 2 || organisation.nil? || team_name.nil?
|
|
|
|
odie "Team must be in the format `organisation/team`!"
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Getting members for #{organisation}/#{team_name}..." if args.verbose?
|
|
|
|
GitHub.members_by_team(organisation, team_name).keys
|
|
|
|
elsif (users = args.user.presence)
|
|
|
|
users
|
|
|
|
else
|
|
|
|
puts "Getting members for Homebrew/maintainers..." if args.verbose?
|
|
|
|
GitHub.members_by_team("Homebrew", "maintainers").keys
|
|
|
|
end
|
|
|
|
|
|
|
|
repositories = if (org = organisation.presence) || (org = args.organisation.presence)
|
2025-09-10 15:32:06 +01:00
|
|
|
organisation = org
|
2025-09-11 11:36:48 +01:00
|
|
|
puts "Getting repositories for #{organisation}..." if args.verbose?
|
2025-09-10 15:32:06 +01:00
|
|
|
GitHub.organisation_repositories(organisation, from, to, args.verbose?)
|
|
|
|
elsif (repos = args.repositories.presence) && repos.length == 1 && (first_repository = repos.first)
|
|
|
|
case first_repository
|
|
|
|
when "primary"
|
2024-07-18 11:29:36 -04:00
|
|
|
PRIMARY_REPOS
|
2025-09-10 15:32:06 +01:00
|
|
|
when "all"
|
|
|
|
ALL_REPOS
|
2024-07-18 11:29:36 -04:00
|
|
|
else
|
2025-09-10 15:32:06 +01:00
|
|
|
Array(first_repository)
|
2024-07-18 11:29:36 -04:00
|
|
|
end
|
2025-09-10 15:32:06 +01:00
|
|
|
elsif (repos = args.repositories.presence)
|
|
|
|
organisations = repos.map { |repository| repository.split("/").first }.uniq
|
|
|
|
odie "All repositories must be under the same user or organisation!" if organisations.length > 1
|
2024-03-19 11:57:54 -07:00
|
|
|
|
2025-09-10 15:32:06 +01:00
|
|
|
repos
|
|
|
|
else
|
|
|
|
PRIMARY_REPOS
|
|
|
|
end
|
|
|
|
organisation ||= T.must(repositories.fetch(0).split("/").first)
|
2024-03-19 11:57:54 -07:00
|
|
|
|
|
|
|
users.each do |username|
|
|
|
|
# TODO: Using the GitHub username to scan the `git log` undercounts some
|
2025-08-05 12:47:04 -04:00
|
|
|
# contributions as people might not always have configured their Git
|
|
|
|
# committer details to match the ones on GitHub.
|
2024-03-19 11:57:54 -07:00
|
|
|
# TODO: Switch to using the GitHub APIs instead of `git log` if
|
2025-08-05 12:47:04 -04:00
|
|
|
# they ever support trailers.
|
2025-09-10 15:32:06 +01:00
|
|
|
results[username] = scan_repositories(organisation, repositories, username, from:, to:)
|
2024-03-19 11:57:54 -07:00
|
|
|
grand_totals[username] = total(results[username])
|
|
|
|
|
2025-09-10 15:32:06 +01:00
|
|
|
search_types = [:merged_pr_author, :approved_pr_review].freeze
|
|
|
|
greater_than_total = T.let(false, T::Boolean)
|
|
|
|
contributions = CONTRIBUTION_TYPES.keys.filter_map do |type|
|
2024-03-19 11:57:54 -07:00
|
|
|
type_count = grand_totals[username][type]
|
2025-09-10 15:32:06 +01:00
|
|
|
next if type_count.zero?
|
|
|
|
|
|
|
|
count_prefix = ""
|
|
|
|
if (search_types.include?(type) && type_count == MAX_PR_SEARCH) ||
|
|
|
|
(type == :committer && type_count == MAX_COMMITS)
|
|
|
|
greater_than_total ||= true
|
|
|
|
count_prefix = ">="
|
|
|
|
end
|
2024-03-19 11:57:54 -07:00
|
|
|
|
2025-09-10 15:32:06 +01:00
|
|
|
pretty_type = CONTRIBUTION_TYPES.fetch(type)
|
|
|
|
"#{count_prefix}#{Utils.pluralize("time", type_count, include_count: true)} (#{pretty_type})"
|
2024-03-19 11:57:54 -07:00
|
|
|
end
|
2025-09-10 15:32:06 +01:00
|
|
|
total = Utils.pluralize("time", grand_totals[username].values.sum, include_count: true)
|
|
|
|
total_prefix = ">=" if greater_than_total
|
|
|
|
contributions << "#{total_prefix}#{total} (total)"
|
2024-03-19 11:57:54 -07:00
|
|
|
|
2025-06-10 17:28:21 +01:00
|
|
|
contributions_string = [
|
2024-03-19 11:57:54 -07:00
|
|
|
"#{username} contributed",
|
|
|
|
*contributions.to_sentence,
|
|
|
|
"#{time_period(from:, to: args.to)}.",
|
|
|
|
].join(" ")
|
2025-06-10 17:28:21 +01:00
|
|
|
if args.csv?
|
|
|
|
$stderr.puts contributions_string
|
|
|
|
else
|
|
|
|
puts contributions_string
|
|
|
|
end
|
2024-03-19 11:57:54 -07:00
|
|
|
end
|
|
|
|
|
2024-07-28 19:28:19 +01:00
|
|
|
return unless args.csv?
|
|
|
|
|
2025-06-10 17:28:21 +01:00
|
|
|
$stderr.puts
|
2024-07-28 19:28:19 +01:00
|
|
|
puts generate_csv(grand_totals)
|
2024-03-19 11:57:54 -07:00
|
|
|
end
|
2022-07-24 22:06:00 +01:00
|
|
|
|
2024-03-19 11:57:54 -07:00
|
|
|
private
|
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
|
|
|
|
2025-09-10 15:32:06 +01:00
|
|
|
sig { params(repository: String).returns([T.nilable(Pathname), T.nilable(Tap)]) }
|
|
|
|
def repository_path_and_tap(repository)
|
|
|
|
return [HOMEBREW_REPOSITORY, nil] if repository == "Homebrew/brew"
|
|
|
|
return [nil, nil] if repository.exclude?("/homebrew-")
|
2023-02-25 00:29:37 +00:00
|
|
|
|
2024-07-25 10:02:18 -04:00
|
|
|
require "tap"
|
2025-09-10 15:32:06 +01:00
|
|
|
tap = Tap.fetch(repository)
|
|
|
|
return [nil, nil] if tap.user == "Homebrew" && DEPRECATED_OFFICIAL_TAPS.include?(tap.repository)
|
|
|
|
|
|
|
|
[tap.path, tap]
|
2023-02-23 23:27:38 +00:00
|
|
|
end
|
|
|
|
|
2024-03-19 11:57:54 -07: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}"
|
|
|
|
else
|
|
|
|
"in all time"
|
|
|
|
end
|
|
|
|
end
|
2023-02-25 00:29:37 +00:00
|
|
|
|
2024-07-01 23:46:25 +01:00
|
|
|
sig { params(totals: T::Hash[String, T::Hash[Symbol, Integer]]).returns(String) }
|
2024-03-19 11:57:54 -07:00
|
|
|
def generate_csv(totals)
|
2025-04-22 19:23:10 +01:00
|
|
|
require "csv"
|
2024-07-18 11:22:39 -04:00
|
|
|
|
2024-03-19 11:57:54 -07:00
|
|
|
CSV.generate do |csv|
|
2025-09-10 15:32:06 +01:00
|
|
|
csv << ["user", "repository", *CONTRIBUTION_TYPES.keys, "total"]
|
2023-02-20 00:14:53 +00:00
|
|
|
|
2024-03-19 11:57:54 -07:00
|
|
|
totals.sort_by { |_, v| -v.values.sum }.each do |user, total|
|
|
|
|
csv << grand_total_row(user, total)
|
|
|
|
end
|
|
|
|
end
|
2023-02-20 00:14:53 +00:00
|
|
|
end
|
|
|
|
|
2025-09-10 15:32:06 +01:00
|
|
|
sig { params(user: String, grand_total: T::Hash[Symbol, Integer]).returns(T::Array[T.any(String, T.nilable(Integer))]) }
|
2024-03-19 11:57:54 -07:00
|
|
|
def grand_total_row(user, grand_total)
|
2025-09-10 15:32:06 +01:00
|
|
|
grand_totals = grand_total.slice(*CONTRIBUTION_TYPES.keys).values
|
|
|
|
[user, "all", *grand_totals, grand_totals.sum]
|
2023-02-20 00:14:53 +00:00
|
|
|
end
|
|
|
|
|
2024-07-18 11:29:36 -04:00
|
|
|
sig {
|
|
|
|
params(
|
2025-09-10 15:32:06 +01:00
|
|
|
organisation: String,
|
|
|
|
repositories: T::Array[String],
|
|
|
|
person: String,
|
|
|
|
from: String,
|
|
|
|
to: String,
|
2024-07-18 11:29:36 -04:00
|
|
|
).returns(T::Hash[Symbol, T.untyped])
|
|
|
|
}
|
2025-09-10 15:32:06 +01:00
|
|
|
def scan_repositories(organisation, repositories, person, from:, to:)
|
2024-03-19 11:57:54 -07:00
|
|
|
data = {}
|
2025-09-10 15:32:06 +01:00
|
|
|
return data if repositories.blank?
|
2024-03-19 11:57:54 -07:00
|
|
|
|
2024-07-25 10:02:18 -04:00
|
|
|
require "utils/github"
|
2025-09-10 15:32:06 +01:00
|
|
|
|
|
|
|
max = MAX_COMMITS
|
|
|
|
verbose = args.verbose?
|
|
|
|
|
|
|
|
puts "Querying pull requests for #{person} in #{organisation}..." if args.verbose?
|
|
|
|
organisation_merged_prs = \
|
|
|
|
GitHub.search_merged_pull_requests_in_user_or_organisation(organisation, person, from:, to:)
|
|
|
|
organisation_approved_reviews = \
|
|
|
|
GitHub.search_approved_pull_requests_in_user_or_organisation(organisation, person, from:, to:)
|
|
|
|
|
|
|
|
require "utils/git"
|
|
|
|
|
|
|
|
repositories.each do |repository|
|
|
|
|
repository_path, tap = repository_path_and_tap(repository)
|
|
|
|
if repository_path && tap && !repository_path.exist?
|
|
|
|
opoo "Repository #{repository} not yet tapped! Tapping it now..."
|
2024-03-19 11:57:54 -07:00
|
|
|
tap.install
|
|
|
|
end
|
|
|
|
|
2025-09-10 15:32:06 +01:00
|
|
|
repository_full_name = tap&.full_name
|
|
|
|
repository_full_name ||= repository
|
2024-03-19 11:57:54 -07:00
|
|
|
|
2025-09-10 15:32:06 +01:00
|
|
|
repository_api_url = "#{GitHub::API_URL}/repos/#{repository_full_name}"
|
2024-03-19 11:57:54 -07:00
|
|
|
|
2025-09-10 15:32:06 +01:00
|
|
|
puts "Determining contributions for #{person} on #{repository_full_name}..." if args.verbose?
|
|
|
|
|
|
|
|
merged_pr_author = organisation_merged_prs.count do |pr|
|
|
|
|
pr.fetch("repository_url") == repository_api_url
|
|
|
|
end
|
|
|
|
approved_pr_review = organisation_approved_reviews.count do |pr|
|
|
|
|
pr.fetch("repository_url") == repository_api_url
|
|
|
|
end
|
|
|
|
committer = GitHub.count_repository_commits(repository_full_name, person, max:, verbose:, from:, to:)
|
|
|
|
coauthor = Utils::Git.count_coauthors(repository_path, person, from:, to:)
|
|
|
|
|
|
|
|
data[repository] = { merged_pr_author:, approved_pr_review:, committer:, coauthor: }
|
|
|
|
rescue GitHub::API::RateLimitExceededError => e
|
|
|
|
sleep_seconds = e.reset - Time.now.to_i
|
|
|
|
opoo "GitHub rate limit exceeded, sleeping for #{sleep_seconds} seconds..."
|
|
|
|
sleep sleep_seconds
|
|
|
|
retry
|
2024-03-19 11:57:54 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
data
|
2023-02-20 00:14:53 +00:00
|
|
|
end
|
|
|
|
|
2024-06-30 18:42:16 +01:00
|
|
|
sig { params(results: T::Hash[Symbol, T.untyped]).returns(T::Hash[Symbol, Integer]) }
|
2024-03-19 11:57:54 -07:00
|
|
|
def total(results)
|
2025-09-10 15:32:06 +01:00
|
|
|
totals = {}
|
2023-02-20 00:14:53 +00:00
|
|
|
|
2024-03-19 11:57:54 -07:00
|
|
|
results.each_value do |counts|
|
|
|
|
counts.each do |kind, count|
|
2025-09-10 15:32:06 +01:00
|
|
|
totals[kind] ||= 0
|
2024-03-19 11:57:54 -07:00
|
|
|
totals[kind] += count
|
|
|
|
end
|
|
|
|
end
|
2023-02-25 00:29:37 +00:00
|
|
|
|
2024-03-19 11:57:54 -07:00
|
|
|
totals
|
2023-02-25 00:29:37 +00:00
|
|
|
end
|
2023-03-15 21:31:41 +00:00
|
|
|
end
|
2023-03-14 21:17:34 +00:00
|
|
|
end
|
2022-07-24 22:06:00 +01:00
|
|
|
end
|