Merge pull request #14737 from issyl0/api-commits-for-person

dev-cmd/contributions: Use GitHub APIs for commit author info
This commit is contained in:
Issy Long 2023-02-22 14:12:16 +00:00 committed by GitHub
commit 93ce211ebd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 15 deletions

View File

@ -19,11 +19,11 @@ module Homebrew
sig { returns(CLI::Parser) } sig { returns(CLI::Parser) }
def contributions_args def contributions_args
Homebrew::CLI::Parser.new do Homebrew::CLI::Parser.new do
usage_banner "`contributions` <email|name> [<--repositories>`=`] [<--csv>]" usage_banner "`contributions` <email|username> [<--repositories>`=`] [<--csv>]"
description <<~EOS description <<~EOS
Contributions to Homebrew repos for a user. Contributions to Homebrew repos for a user.
The first argument is a name (e.g. "BrewTestBot") or an email address (e.g. "brewtestbot@brew.sh"). The first argument is a GitHub username (e.g. "BrewTestBot") or an email address (e.g. "brewtestbot@brew.sh").
EOS EOS
comma_array "--repositories", comma_array "--repositories",
@ -65,13 +65,20 @@ module Homebrew
end end
repo_path = find_repo_path_for_repo(repo) repo_path = find_repo_path_for_repo(repo)
tap = Tap.fetch("homebrew", repo)
unless repo_path.exist? unless repo_path.exist?
opoo "Repository #{repo} not yet tapped! Tapping it now..." opoo "Repository #{repo} not yet tapped! Tapping it now..."
Tap.fetch("homebrew", repo).install tap.install
end
repo_full_name = if repo == "brew"
"homebrew/brew"
else
tap.full_name
end end
results[repo] = { results[repo] = {
commits: git_log_author_cmd(T.must(repo_path), args), commits: GitHub.repo_commit_count_for_user(repo_full_name, args.named.first),
coauthorships: git_log_trailers_cmd(T.must(repo_path), "Co-authored-by", 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), signoffs: git_log_trailers_cmd(T.must(repo_path), "Signed-off-by", args),
} }
@ -127,15 +134,6 @@ module Homebrew
.sum(&:sum) # 956 .sum(&:sum) # 956
end end
sig { params(repo_path: Pathname, args: Homebrew::CLI::Args).returns(Integer) }
def git_log_author_cmd(repo_path, args)
cmd = ["git", "-C", repo_path, "log", "--oneline", "--author=#{args.named.first}"]
cmd << "--before=#{args.to}" if args.to
cmd << "--after=#{args.from}" if args.from
Utils.safe_popen_read(*cmd).lines.count
end
sig { params(repo_path: Pathname, trailer: String, args: Homebrew::CLI::Args).returns(Integer) } sig { params(repo_path: Pathname, trailer: String, args: Homebrew::CLI::Args).returns(Integer) }
def git_log_trailers_cmd(repo_path, trailer, args) def git_log_trailers_cmd(repo_path, trailer, args)
cmd = ["git", "-C", repo_path, "log", "--oneline"] cmd = ["git", "-C", repo_path, "log", "--oneline"]

View File

@ -699,4 +699,14 @@ module GitHub
output[/^Status: (200)/, 1] != "200" output[/^Status: (200)/, 1] != "200"
end end
def repo_commit_count_for_user(nwo, user)
return if Homebrew::EnvConfig.no_github_api?
commits = 0
API.paginate_rest("#{API_URL}/repos/#{nwo}/commits", additional_query_params: "author=#{user}") do |result|
commits += result.length
end
commits
end
end end

View File

@ -253,9 +253,9 @@ module GitHub
end end
end end
def paginate_rest(url, per_page: 100) def paginate_rest(url, additional_query_params: nil, per_page: 100)
(1..API_MAX_PAGES).each do |page| (1..API_MAX_PAGES).each do |page|
result = API.open_rest("#{url}?per_page=#{per_page}&page=#{page}") result = API.open_rest("#{url}?per_page=#{per_page}&page=#{page}&#{additional_query_params}")
yield(result, page) yield(result, page)
end end
end end