From a9f7da36e0cb51c5d4b9af102c48e9b65bbe2ae3 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Thu, 18 Jul 2024 10:42:30 -0400 Subject: [PATCH 1/3] contributions, github: add missing requires This resolves `unitialized constant` errors in `brew contributions` (`Tap`, `GitHub`) and `Utils::GitHub` (`Utils::Curl`). This also preemptively adds some requires to `Utils::GitHub` and `GitHub::API`, to avoid similar errors. --- Library/Homebrew/dev-cmd/contributions.rb | 2 ++ Library/Homebrew/utils/github.rb | 2 ++ Library/Homebrew/utils/github/api.rb | 1 + 3 files changed, 5 insertions(+) diff --git a/Library/Homebrew/dev-cmd/contributions.rb b/Library/Homebrew/dev-cmd/contributions.rb index 96159227fa..b9085405d3 100644 --- a/Library/Homebrew/dev-cmd/contributions.rb +++ b/Library/Homebrew/dev-cmd/contributions.rb @@ -6,6 +6,8 @@ require "warnings" Warnings.ignore :default_gems do require "csv" end +require "tap" +require "utils/github" module Homebrew module DevCmd diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 2b0c11a989..fc35871ea4 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -2,6 +2,8 @@ # frozen_string_literal: true require "uri" +require "utils/curl" +require "utils/popen" require "utils/github/actions" require "utils/github/api" diff --git a/Library/Homebrew/utils/github/api.rb b/Library/Homebrew/utils/github/api.rb index d695cf52c1..2917b048de 100644 --- a/Library/Homebrew/utils/github/api.rb +++ b/Library/Homebrew/utils/github/api.rb @@ -3,6 +3,7 @@ require "system_command" require "tempfile" +require "utils/curl" require "utils/shell" require "utils/formatter" require "utils/uid" From b4b984e968be2a61178bdcdf6d7a01c98367e6c9 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:22:39 -0400 Subject: [PATCH 2/3] contributions: move CSV require into #generate_csv CSV generation is optional, so this moves the related `require` into the method where `CSV` is used (following a pattern we've used for other `require` calls throughout `brew`). --- Library/Homebrew/dev-cmd/contributions.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/dev-cmd/contributions.rb b/Library/Homebrew/dev-cmd/contributions.rb index b9085405d3..dbad695b9f 100644 --- a/Library/Homebrew/dev-cmd/contributions.rb +++ b/Library/Homebrew/dev-cmd/contributions.rb @@ -2,10 +2,6 @@ # frozen_string_literal: true require "abstract_command" -require "warnings" -Warnings.ignore :default_gems do - require "csv" -end require "tap" require "utils/github" @@ -120,6 +116,11 @@ module Homebrew sig { params(totals: T::Hash[String, T::Hash[Symbol, Integer]]).returns(String) } def generate_csv(totals) + require "warnings" + Warnings.ignore :default_gems do + require "csv" + end + CSV.generate do |csv| csv << %w[user repo author committer coauthor review total] From 403b0bf3f16eef8c4ffacd52c935e738f741446d Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:29:36 -0400 Subject: [PATCH 3/3] contributions: resolve type errors This updates the type signature for `#scan_repositories` to address a runtime type error and to reflect the actual return type. The logic in `#scan_repositories` to check for unsupported repositories leads to a type error, as `#ofail` has a void return type. To resolve this, I moved the repository verification code into `#run` (after `repos` is defined but before it's used) and used `#odie`, so the command will exit early with an error. While I was at it, I updated the type for the `repos` parameter to not be `nilable`, as it shouldn't be `nil` based on how we're handling `repos` in `#run`. --- Library/Homebrew/dev-cmd/contributions.rb | 35 ++++++++++++++--------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/Library/Homebrew/dev-cmd/contributions.rb b/Library/Homebrew/dev-cmd/contributions.rb index dbad695b9f..ef175a4fc4 100644 --- a/Library/Homebrew/dev-cmd/contributions.rb +++ b/Library/Homebrew/dev-cmd/contributions.rb @@ -48,12 +48,20 @@ module Homebrew results = {} grand_totals = {} - repos = if args.repositories.blank? || args.repositories&.include?("primary") - PRIMARY_REPOS - elsif args.repositories&.include?("all") - SUPPORTED_REPOS - else - args.repositories + repos = T.must( + if args.repositories.blank? || args.repositories&.include?("primary") + PRIMARY_REPOS + elsif args.repositories&.include?("all") + SUPPORTED_REPOS + else + args.repositories + end, + ) + + repos.each do |repo| + if SUPPORTED_REPOS.exclude?(repo) + odie "Unsupported repository: #{repo}. Try one of #{SUPPORTED_REPOS.join(", ")}." + end end from = args.from.presence || Date.today.prev_year.iso8601 @@ -150,17 +158,18 @@ module Homebrew ] end - sig { params(repos: T.nilable(T::Array[String]), person: String, from: String).void } + sig { + params( + repos: T::Array[String], + person: String, + from: String, + ).returns(T::Hash[Symbol, T.untyped]) + } def scan_repositories(repos, person, from:) - return if repos.blank? - data = {} + return data if repos.blank? 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?