From 8c75eab88a9d3bdbe6db2723d1efd91cbc0a11aa Mon Sep 17 00:00:00 2001 From: Issy Long Date: Sat, 25 Feb 2023 19:10:17 +0000 Subject: [PATCH 1/4] dev-cmd/contributions: Count PR reviews since they're super important - The search APIs don't have that high a rate limit but we shouldn't need to worry about that too much because, to get counts, the JSON response comes with a `total_count` number. --- Library/Homebrew/dev-cmd/contributions.rb | 16 +++++++++++----- Library/Homebrew/utils/github.rb | 20 +++++++++++++++++--- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Library/Homebrew/dev-cmd/contributions.rb b/Library/Homebrew/dev-cmd/contributions.rb index 868e10f43b..0e080b6837 100755 --- a/Library/Homebrew/dev-cmd/contributions.rb +++ b/Library/Homebrew/dev-cmd/contributions.rb @@ -108,7 +108,7 @@ module Homebrew sig { params(totals: Hash).returns(String) } def generate_maintainers_csv(totals) CSV.generate do |csv| - csv << %w[user repo commits coauthorships signoffs total] + csv << %w[user repo commits coauthorships signoffs reviews total] totals.each do |user, total| csv << grand_total_row(user, total) @@ -119,7 +119,7 @@ module Homebrew 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] + csv << %w[user repo commits coauthorships signoffs reviews total] results.each do |repo, counts| csv << [ user, @@ -127,6 +127,7 @@ module Homebrew counts[:commits], counts[:coauthorships], counts[:signoffs], + counts[:reviews], counts.values.sum, ] end @@ -142,6 +143,7 @@ module Homebrew grand_total[:commits], grand_total[:coauthorships], grand_total[:signoffs], + grand_total[:reviews], grand_total.values.sum, ] end @@ -173,6 +175,7 @@ module Homebrew commits: GitHub.repo_commit_count_for_user(repo_full_name, person, args), coauthorships: git_log_trailers_cmd(T.must(repo_path), person, "Co-authored-by", args), signoffs: git_log_trailers_cmd(T.must(repo_path), person, "Signed-off-by", args), + reviews: GitHub.count_issues("", is: "pr", repo: repo_full_name, reviewed_by: person), } end @@ -181,16 +184,19 @@ module Homebrew sig { params(results: Hash).returns(Hash) } def total(results) - totals = { commits: 0, coauthorships: 0, signoffs: 0 } + totals = { commits: 0, coauthorships: 0, signoffs: 0, reviews: 0 } - # {"brew"=>{:commits=>9,:coauthorships=>6,:signoffs=>3},"core"=>{:commits=>15,:coauthorships=>10,:signoffs=>5}} + # { + # "brew"=>{:commits=>9,:coauthorships=>6,:signoffs=>3,:reviews=>1}, + # "core"=>{:commits=>15,:coauthorships=>10,:signoffs=>5,:reviews=>2} + # } results.each_value do |counts| counts.each do |kind, count| totals[kind] += count end end - totals # {:commits=>24,:coauthorships=>16,signoffs=>8} + totals # {:commits=>24,:coauthorships=>16,:signoffs=>8,:reviews=>3} end sig { params(repo_path: Pathname, person: String, trailer: String, args: Homebrew::CLI::Args).returns(Integer) } diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 1ca085109b..691bf4955e 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -37,7 +37,11 @@ module GitHub end def search_issues(query, **qualifiers) - search("issues", query, **qualifiers) + search_results_items("issues", query, **qualifiers) + end + + def count_issues(query, **qualifiers) + search_results_count("issues", query, **qualifiers) end def create_gist(files, description, private:) @@ -163,7 +167,7 @@ module GitHub params = main_params params += qualifiers.flat_map do |key, value| - Array(value).map { |v| "#{key}:#{v}" } + Array(value).map { |v| "#{key.to_s.tr("_", "-")}:#{v}" } end "q=#{URI.encode_www_form_component(params.join(" "))}&per_page=100" @@ -176,7 +180,17 @@ module GitHub def search(entity, *queries, **qualifiers) uri = url_to "search", entity uri.query = search_query_string(*queries, **qualifiers) - API.open_rest(uri) { |json| json.fetch("items", []) } + API.open_rest(uri) + end + + def search_results_items(entity, *queries, **qualifiers) + json = search(entity, *queries, **qualifiers) + json.fetch("items", []) + end + + def search_results_count(entity, *queries, **qualifiers) + json = search(entity, *queries, **qualifiers) + json.fetch("total_count", 0) end def approved_reviews(user, repo, pr, commit: nil) From 1edb59e08607998452d60f5d16fdaa30655bd107 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Sat, 25 Feb 2023 21:54:17 +0000 Subject: [PATCH 2/4] test/search: Fix stubbing of `GitHub::API.open_rest` call Test failure: ``` Failure/Error: expect(described_class.search_taps("some-formula")) .to match(formulae: ["homebrew/foo/some-formula"], casks: ["homebrew/bar/some-cask"]) GitHub::API asked to yield |[{"items"=>[{"path"=>"Formula/some-formula.rb", "repository"=>{"full_name"=>"Homebrew/homebrew-foo"}}, {"path"=>"Casks/some-cask.rb", "repository"=>{"full_name"=>"Homebrew/homebrew-bar"}}]}]| but no block was passed ``` --- Library/Homebrew/test/search_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/test/search_spec.rb b/Library/Homebrew/test/search_spec.rb index ef3f383919..6a37c77183 100644 --- a/Library/Homebrew/test/search_spec.rb +++ b/Library/Homebrew/test/search_spec.rb @@ -39,7 +39,7 @@ describe Homebrew::Search do ], } - allow(GitHub::API).to receive(:open_rest).and_yield(json_response) + allow(GitHub::API).to receive(:open_rest).and_return(json_response) expect(described_class.search_taps("some-formula")) .to match(formulae: ["homebrew/foo/some-formula"], casks: ["homebrew/bar/some-cask"]) From 99d2bb9a53745afd3b4c586dcc6afc4038e2dd4f Mon Sep 17 00:00:00 2001 From: Issy Long Date: Sat, 25 Feb 2023 21:57:50 +0000 Subject: [PATCH 3/4] utils/github: Use `search_results_items` method for code search --- Library/Homebrew/utils/github.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 691bf4955e..a14f1c8850 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -61,7 +61,7 @@ module GitHub end def search_code(repo: nil, user: "Homebrew", path: ["Formula", "Casks", "."], filename: nil, extension: "rb") - matches = search("code", user: user, path: path, filename: filename, extension: extension, repo: repo) + matches = search_results_items("code", user: user, path: path, filename: filename, extension: extension, repo: repo) return matches if matches.blank? matches.map do |match| From 591afa8ee00d7303d3e248e60b7e8ff2e219460b Mon Sep 17 00:00:00 2001 From: Issy Long Date: Sat, 25 Feb 2023 22:27:48 +0000 Subject: [PATCH 4/4] utils/github: Appease `brew style` line length --- Library/Homebrew/utils/github.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index a14f1c8850..3137393e6d 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -61,7 +61,14 @@ module GitHub end def search_code(repo: nil, user: "Homebrew", path: ["Formula", "Casks", "."], filename: nil, extension: "rb") - matches = search_results_items("code", user: user, path: path, filename: filename, extension: extension, repo: repo) + matches = search_results_items( + "code", + user: user, + path: path, + filename: filename, + extension: extension, + repo: repo, + ) return matches if matches.blank? matches.map do |match|