contributions, github: reorder requires
Per feedback to https://github.com/Homebrew/brew/pull/17806, this moves some `require` statements in `dev-cmd/contributions.rb` and `Utils::GitHub` into the methods that need them.
This commit is contained in:
		
							parent
							
								
									0f0e2c7bd9
								
							
						
					
					
						commit
						a6e61fd664
					
				@ -2,8 +2,6 @@
 | 
				
			|||||||
# frozen_string_literal: true
 | 
					# frozen_string_literal: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require "abstract_command"
 | 
					require "abstract_command"
 | 
				
			||||||
require "tap"
 | 
					 | 
				
			||||||
require "utils/github"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
module Homebrew
 | 
					module Homebrew
 | 
				
			||||||
  module DevCmd
 | 
					  module DevCmd
 | 
				
			||||||
@ -68,6 +66,7 @@ module Homebrew
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        contribution_types = [:author, :committer, :coauthor, :review]
 | 
					        contribution_types = [:author, :committer, :coauthor, :review]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        require "utils/github"
 | 
				
			||||||
        users = args.user.presence || GitHub.members_by_team("Homebrew", "maintainers").keys
 | 
					        users = args.user.presence || GitHub.members_by_team("Homebrew", "maintainers").keys
 | 
				
			||||||
        users.each do |username|
 | 
					        users.each do |username|
 | 
				
			||||||
          # TODO: Using the GitHub username to scan the `git log` undercounts some
 | 
					          # TODO: Using the GitHub username to scan the `git log` undercounts some
 | 
				
			||||||
@ -106,6 +105,7 @@ module Homebrew
 | 
				
			|||||||
      def find_repo_path_for_repo(repo)
 | 
					      def find_repo_path_for_repo(repo)
 | 
				
			||||||
        return HOMEBREW_REPOSITORY if repo == "brew"
 | 
					        return HOMEBREW_REPOSITORY if repo == "brew"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        require "tap"
 | 
				
			||||||
        Tap.fetch("homebrew", repo).path
 | 
					        Tap.fetch("homebrew", repo).path
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -169,6 +169,8 @@ module Homebrew
 | 
				
			|||||||
        data = {}
 | 
					        data = {}
 | 
				
			||||||
        return data if repos.blank?
 | 
					        return data if repos.blank?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        require "tap"
 | 
				
			||||||
 | 
					        require "utils/github"
 | 
				
			||||||
        repos.each do |repo|
 | 
					        repos.each do |repo|
 | 
				
			||||||
          repo_path = find_repo_path_for_repo(repo)
 | 
					          repo_path = find_repo_path_for_repo(repo)
 | 
				
			||||||
          tap = Tap.fetch("homebrew", repo)
 | 
					          tap = Tap.fetch("homebrew", repo)
 | 
				
			||||||
@ -229,6 +231,7 @@ module Homebrew
 | 
				
			|||||||
               to: T.nilable(String)).returns(Integer)
 | 
					               to: T.nilable(String)).returns(Integer)
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      def count_reviews(repo_full_name, person, from:, to:)
 | 
					      def count_reviews(repo_full_name, person, from:, to:)
 | 
				
			||||||
 | 
					        require "utils/github"
 | 
				
			||||||
        GitHub.count_issues("", is: "pr", repo: repo_full_name, reviewed_by: person, review: "approved", from:, to:)
 | 
					        GitHub.count_issues("", is: "pr", repo: repo_full_name, reviewed_by: person, review: "approved", from:, to:)
 | 
				
			||||||
      rescue GitHub::API::ValidationFailedError
 | 
					      rescue GitHub::API::ValidationFailedError
 | 
				
			||||||
        if args.verbose?
 | 
					        if args.verbose?
 | 
				
			||||||
 | 
				
			|||||||
@ -2,8 +2,6 @@
 | 
				
			|||||||
# frozen_string_literal: true
 | 
					# frozen_string_literal: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require "uri"
 | 
					require "uri"
 | 
				
			||||||
require "utils/curl"
 | 
					 | 
				
			||||||
require "utils/popen"
 | 
					 | 
				
			||||||
require "utils/github/actions"
 | 
					require "utils/github/actions"
 | 
				
			||||||
require "utils/github/api"
 | 
					require "utils/github/api"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -705,6 +703,7 @@ module GitHub
 | 
				
			|||||||
    pr_message = info[:pr_message]
 | 
					    pr_message = info[:pr_message]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sourcefile_path.parent.cd do
 | 
					    sourcefile_path.parent.cd do
 | 
				
			||||||
 | 
					      require "utils/popen"
 | 
				
			||||||
      git_dir = Utils.popen_read("git", "rev-parse", "--git-dir").chomp
 | 
					      git_dir = Utils.popen_read("git", "rev-parse", "--git-dir").chomp
 | 
				
			||||||
      shallow = !git_dir.empty? && File.exist?("#{git_dir}/shallow")
 | 
					      shallow = !git_dir.empty? && File.exist?("#{git_dir}/shallow")
 | 
				
			||||||
      changed_files = [sourcefile_path]
 | 
					      changed_files = [sourcefile_path]
 | 
				
			||||||
@ -814,6 +813,7 @@ module GitHub
 | 
				
			|||||||
  def self.last_commit(user, repo, ref, version)
 | 
					  def self.last_commit(user, repo, ref, version)
 | 
				
			||||||
    return if Homebrew::EnvConfig.no_github_api?
 | 
					    return if Homebrew::EnvConfig.no_github_api?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    require "utils/curl"
 | 
				
			||||||
    output, _, status = Utils::Curl.curl_output(
 | 
					    output, _, status = Utils::Curl.curl_output(
 | 
				
			||||||
      "--silent", "--head", "--location",
 | 
					      "--silent", "--head", "--location",
 | 
				
			||||||
      "--header", "Accept: application/vnd.github.sha",
 | 
					      "--header", "Accept: application/vnd.github.sha",
 | 
				
			||||||
@ -832,6 +832,7 @@ module GitHub
 | 
				
			|||||||
  def self.multiple_short_commits_exist?(user, repo, commit)
 | 
					  def self.multiple_short_commits_exist?(user, repo, commit)
 | 
				
			||||||
    return false if Homebrew::EnvConfig.no_github_api?
 | 
					    return false if Homebrew::EnvConfig.no_github_api?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    require "utils/curl"
 | 
				
			||||||
    output, _, status = Utils::Curl.curl_output(
 | 
					    output, _, status = Utils::Curl.curl_output(
 | 
				
			||||||
      "--silent", "--head", "--location",
 | 
					      "--silent", "--head", "--location",
 | 
				
			||||||
      "--header", "Accept: application/vnd.github.sha",
 | 
					      "--header", "Accept: application/vnd.github.sha",
 | 
				
			||||||
 | 
				
			|||||||
@ -1,9 +1,6 @@
 | 
				
			|||||||
# typed: strict
 | 
					# typed: strict
 | 
				
			||||||
# frozen_string_literal: true
 | 
					# frozen_string_literal: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require "securerandom"
 | 
					 | 
				
			||||||
require "utils/tty"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
module GitHub
 | 
					module GitHub
 | 
				
			||||||
  # Helper functions for interacting with GitHub Actions.
 | 
					  # Helper functions for interacting with GitHub Actions.
 | 
				
			||||||
  #
 | 
					  #
 | 
				
			||||||
@ -22,6 +19,7 @@ module GitHub
 | 
				
			|||||||
      # Format multiline strings for environment files
 | 
					      # Format multiline strings for environment files
 | 
				
			||||||
      # See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
 | 
					      # See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      require "securerandom"
 | 
				
			||||||
      delimiter = "ghadelimiter_#{SecureRandom.uuid}"
 | 
					      delimiter = "ghadelimiter_#{SecureRandom.uuid}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if name.include?(delimiter) || value.include?(delimiter)
 | 
					      if name.include?(delimiter) || value.include?(delimiter)
 | 
				
			||||||
@ -84,6 +82,7 @@ module GitHub
 | 
				
			|||||||
      def initialize(type, message, file: nil, title: nil, line: nil, end_line: nil, column: nil, end_column: nil)
 | 
					      def initialize(type, message, file: nil, title: nil, line: nil, end_line: nil, column: nil, end_column: nil)
 | 
				
			||||||
        raise ArgumentError, "Unsupported type: #{type.inspect}" if ANNOTATION_TYPES.exclude?(type)
 | 
					        raise ArgumentError, "Unsupported type: #{type.inspect}" if ANNOTATION_TYPES.exclude?(type)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        require "utils/tty"
 | 
				
			||||||
        @type = type
 | 
					        @type = type
 | 
				
			||||||
        @message = T.let(Tty.strip_ansi(message), String)
 | 
					        @message = T.let(Tty.strip_ansi(message), String)
 | 
				
			||||||
        @file = T.let(self.class.path_relative_to_workspace(file), T.nilable(Pathname)) if file.present?
 | 
					        @file = T.let(self.class.path_relative_to_workspace(file), T.nilable(Pathname)) if file.present?
 | 
				
			||||||
 | 
				
			|||||||
@ -2,14 +2,11 @@
 | 
				
			|||||||
# frozen_string_literal: true
 | 
					# frozen_string_literal: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require "system_command"
 | 
					require "system_command"
 | 
				
			||||||
require "tempfile"
 | 
					 | 
				
			||||||
require "utils/curl"
 | 
					 | 
				
			||||||
require "utils/shell"
 | 
					 | 
				
			||||||
require "utils/formatter"
 | 
					 | 
				
			||||||
require "utils/uid"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
module GitHub
 | 
					module GitHub
 | 
				
			||||||
  def self.pat_blurb(scopes = ALL_SCOPES)
 | 
					  def self.pat_blurb(scopes = ALL_SCOPES)
 | 
				
			||||||
 | 
					    require "utils/formatter"
 | 
				
			||||||
 | 
					    require "utils/shell"
 | 
				
			||||||
    <<~EOS
 | 
					    <<~EOS
 | 
				
			||||||
      Create a GitHub personal access token:
 | 
					      Create a GitHub personal access token:
 | 
				
			||||||
        #{Formatter.url(
 | 
					        #{Formatter.url(
 | 
				
			||||||
@ -98,6 +95,7 @@ module GitHub
 | 
				
			|||||||
              printf "protocol=https\\nhost=github.com\\n" | git credential-osxkeychain erase
 | 
					              printf "protocol=https\\nhost=github.com\\n" | git credential-osxkeychain erase
 | 
				
			||||||
          EOS
 | 
					          EOS
 | 
				
			||||||
        when :env_token
 | 
					        when :env_token
 | 
				
			||||||
 | 
					          require "utils/formatter"
 | 
				
			||||||
          <<~EOS
 | 
					          <<~EOS
 | 
				
			||||||
            HOMEBREW_GITHUB_API_TOKEN may be invalid or expired; check:
 | 
					            HOMEBREW_GITHUB_API_TOKEN may be invalid or expired; check:
 | 
				
			||||||
              #{Formatter.url("https://github.com/settings/tokens")}
 | 
					              #{Formatter.url("https://github.com/settings/tokens")}
 | 
				
			||||||
@ -148,6 +146,7 @@ module GitHub
 | 
				
			|||||||
    # Gets the token from the GitHub CLI for github.com.
 | 
					    # Gets the token from the GitHub CLI for github.com.
 | 
				
			||||||
    sig { returns(T.nilable(String)) }
 | 
					    sig { returns(T.nilable(String)) }
 | 
				
			||||||
    def self.github_cli_token
 | 
					    def self.github_cli_token
 | 
				
			||||||
 | 
					      require "utils/uid"
 | 
				
			||||||
      Utils::UID.drop_euid do
 | 
					      Utils::UID.drop_euid do
 | 
				
			||||||
        # Avoid `Formula["gh"].opt_bin` so this method works even with `HOMEBREW_DISABLE_LOAD_FORMULA`.
 | 
					        # Avoid `Formula["gh"].opt_bin` so this method works even with `HOMEBREW_DISABLE_LOAD_FORMULA`.
 | 
				
			||||||
        env = {
 | 
					        env = {
 | 
				
			||||||
@ -168,6 +167,7 @@ module GitHub
 | 
				
			|||||||
    # but only if that password looks like a GitHub Personal Access Token.
 | 
					    # but only if that password looks like a GitHub Personal Access Token.
 | 
				
			||||||
    sig { returns(T.nilable(String)) }
 | 
					    sig { returns(T.nilable(String)) }
 | 
				
			||||||
    def self.keychain_username_password
 | 
					    def self.keychain_username_password
 | 
				
			||||||
 | 
					      require "utils/uid"
 | 
				
			||||||
      Utils::UID.drop_euid do
 | 
					      Utils::UID.drop_euid do
 | 
				
			||||||
        git_credential_out, _, result = system_command "git",
 | 
					        git_credential_out, _, result = system_command "git",
 | 
				
			||||||
                                                       args:         ["credential-osxkeychain", "get"],
 | 
					                                                       args:         ["credential-osxkeychain", "get"],
 | 
				
			||||||
@ -252,6 +252,7 @@ module GitHub
 | 
				
			|||||||
      args += ["--header", "Authorization: token #{token}"] if credentials_type != :none
 | 
					      args += ["--header", "Authorization: token #{token}"] if credentials_type != :none
 | 
				
			||||||
      args += ["--header", "X-GitHub-Api-Version:2022-11-28"]
 | 
					      args += ["--header", "X-GitHub-Api-Version:2022-11-28"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      require "tempfile"
 | 
				
			||||||
      data_tmpfile = nil
 | 
					      data_tmpfile = nil
 | 
				
			||||||
      if data
 | 
					      if data
 | 
				
			||||||
        begin
 | 
					        begin
 | 
				
			||||||
@ -279,6 +280,7 @@ module GitHub
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        args += ["--dump-header", T.must(headers_tmpfile.path)]
 | 
					        args += ["--dump-header", T.must(headers_tmpfile.path)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        require "utils/curl"
 | 
				
			||||||
        output, errors, status = Utils::Curl.curl_output("--location", url.to_s, *args, secrets: [token])
 | 
					        output, errors, status = Utils::Curl.curl_output("--location", url.to_s, *args, secrets: [token])
 | 
				
			||||||
        output, _, http_code = output.rpartition("\n")
 | 
					        output, _, http_code = output.rpartition("\n")
 | 
				
			||||||
        output, _, http_code = output.rpartition("\n") if http_code == "000"
 | 
					        output, _, http_code = output.rpartition("\n") if http_code == "000"
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user