From 500908ee6fe456197150494269d0862a7959efd5 Mon Sep 17 00:00:00 2001 From: Jonathan Chang Date: Tue, 10 Nov 2020 00:11:22 +1100 Subject: [PATCH] rubocop: fix Lint/NoReturnInBeginEndBlocks --- Library/Homebrew/cleanup.rb | 8 ++- Library/Homebrew/dev-cmd/pr-pull.rb | 8 ++- .../extend/os/mac/unpack_strategy/zip.rb | 4 +- .../Homebrew/extend/os/mac/utils/bottles.rb | 4 +- .../rubocops/cask/homepage_matches_url.rb | 4 +- Library/Homebrew/search.rb | 4 +- Library/Homebrew/utils/github.rb | 56 +++++++++---------- Library/Homebrew/utils/shared_audits.rb | 16 ++---- 8 files changed, 53 insertions(+), 51 deletions(-) diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index f23d8989bf..a421715c35 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -84,9 +84,11 @@ module Homebrew formula = begin Formulary.from_rack(HOMEBREW_CELLAR/formula_name) rescue FormulaUnavailableError, TapFormulaAmbiguityError, TapFormulaWithOldnameAmbiguityError - return false + nil end + return false if formula.blank? + resource_name = basename.to_s[/\A.*?--(.*?)--?(?:#{Regexp.escape(version)})/, 1] if resource_name == "patch" @@ -113,9 +115,11 @@ module Homebrew cask = begin Cask::CaskLoader.load(name) rescue Cask::CaskError - return false + nil end + return false if cask.blank? + return true unless basename.to_s.match?(/\A#{Regexp.escape(name)}--#{Regexp.escape(cask.version)}\b/) return true if scrub && !cask.versions.include?(cask.version) diff --git a/Library/Homebrew/dev-cmd/pr-pull.rb b/Library/Homebrew/dev-cmd/pr-pull.rb index 56fceff5e2..a11704972f 100644 --- a/Library/Homebrew/dev-cmd/pr-pull.rb +++ b/Library/Homebrew/dev-cmd/pr-pull.rb @@ -111,15 +111,19 @@ module Homebrew new_formula = begin Formulary.from_contents(formula_name, formula_path, new_contents, :stable) rescue FormulaUnavailableError - return "#{formula_name}: delete #{reason}".strip + nil end + return "#{formula_name}: delete #{reason}".strip if new_formula.blank? + old_formula = begin Formulary.from_contents(formula_name, formula_path, old_contents, :stable) rescue FormulaUnavailableError - return "#{formula_name} #{new_formula.stable.version} (new formula)" + nil end + return "#{formula_name} #{new_formula.stable.version} (new formula)" if old_formula.blank? + if old_formula.stable.version != new_formula.stable.version "#{formula_name} #{new_formula.stable.version}" elsif old_formula.revision != new_formula.revision diff --git a/Library/Homebrew/extend/os/mac/unpack_strategy/zip.rb b/Library/Homebrew/extend/os/mac/unpack_strategy/zip.rb index b804e49f35..7da213be6b 100644 --- a/Library/Homebrew/extend/os/mac/unpack_strategy/zip.rb +++ b/Library/Homebrew/extend/os/mac/unpack_strategy/zip.rb @@ -33,9 +33,11 @@ module UnpackStrategy system_command! "ditto", args: ["-x", "-k", path, unpack_dir], verbose: verbose - return + nil end + return if result.blank? + volumes = result.stderr.chomp .split("\n") .map { |l| l[/\A skipping: (.+) volume label\Z/, 1] } diff --git a/Library/Homebrew/extend/os/mac/utils/bottles.rb b/Library/Homebrew/extend/os/mac/utils/bottles.rb index 3986d0b940..6bd47aa1a0 100644 --- a/Library/Homebrew/extend/os/mac/utils/bottles.rb +++ b/Library/Homebrew/extend/os/mac/utils/bottles.rb @@ -36,9 +36,11 @@ module Utils tag_version = begin MacOS::Version.from_symbol(tag) rescue MacOSVersionError - return + nil end + return if tag_version.blank? + keys.find do |key| MacOS::Version.from_symbol(key) <= tag_version rescue MacOSVersionError diff --git a/Library/Homebrew/rubocops/cask/homepage_matches_url.rb b/Library/Homebrew/rubocops/cask/homepage_matches_url.rb index 1f3c5e6679..83f112b5e8 100644 --- a/Library/Homebrew/rubocops/cask/homepage_matches_url.rb +++ b/Library/Homebrew/rubocops/cask/homepage_matches_url.rb @@ -139,9 +139,11 @@ module RuboCop URI(remove_non_ascii(host)) rescue URI::InvalidURIError # Can't check if we can't parse. - return true + nil end + return true if host_uri.blank? + host = if host.match?(/:\d/) && host_uri.port != 80 "#{host_uri.host}:#{host_uri.port}" else diff --git a/Library/Homebrew/search.rb b/Library/Homebrew/search.rb index 942083a1c9..fe37e97254 100644 --- a/Library/Homebrew/search.rb +++ b/Library/Homebrew/search.rb @@ -50,9 +50,11 @@ module Homebrew ) rescue GitHub::Error => e opoo "Error searching on GitHub: #{e}\n" - return results + nil end + return results if matches.blank? + matches.each do |match| name = File.basename(match["path"], ".rb") tap = Tap.fetch(match["repository"]["full_name"]) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index b5a8eed644..124ba70255 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -141,43 +141,37 @@ module GitHub end end + # Given an API response from GitHub, warn the user if their credentials + # have insufficient permissions. def api_credentials_error_message(response_headers, needed_scopes) return if response_headers.empty? - @api_credentials_error_message ||= begin - unauthorized = (response_headers["http/1.1"] == "401 Unauthorized") - scopes = response_headers["x-accepted-oauth-scopes"].to_s.split(", ") - if unauthorized && scopes.blank? - needed_human_scopes = needed_scopes.join(", ") - credentials_scopes = response_headers["x-oauth-scopes"] - return if needed_human_scopes.blank? && credentials_scopes.blank? + unauthorized = (response_headers["http/1.1"] == "401 Unauthorized") + scopes = response_headers["x-accepted-oauth-scopes"].to_s.split(", ") + return unless unauthorized && scopes.blank? - needed_human_scopes = "none" if needed_human_scopes.blank? - credentials_scopes = "none" if credentials_scopes.blank? + needed_human_scopes = needed_scopes.join(", ") + credentials_scopes = response_headers["x-oauth-scopes"] + return if needed_human_scopes.blank? && credentials_scopes.blank? - case GitHub.api_credentials_type - when :keychain_username_password - onoe <<~EOS - Your macOS keychain GitHub credentials do not have sufficient scope! - Scopes they need: #{needed_human_scopes} - Scopes they have: #{credentials_scopes} - Create a personal access token: - #{ALL_SCOPES_URL} - #{Utils::Shell.set_variable_in_profile("HOMEBREW_GITHUB_API_TOKEN", "your_token_here")} - EOS - when :env_token - onoe <<~EOS - Your HOMEBREW_GITHUB_API_TOKEN does not have sufficient scope! - Scopes it needs: #{needed_human_scopes} - Scopes it has: #{credentials_scopes} - Create a new personal access token: - #{ALL_SCOPES_URL} - #{Utils::Shell.set_variable_in_profile("HOMEBREW_GITHUB_API_TOKEN", "your_token_here")} - EOS - end - end - true + needed_human_scopes = "none" if needed_human_scopes.blank? + credentials_scopes = "none" if credentials_scopes.blank? + + what = case GitHub.api_credentials_type + when :keychain_username_password + "macOS keychain GitHub" + when :env_token + "HOMEBREW_GITHUB_API_TOKEN" end + + @api_credentials_error_message ||= onoe <<~EOS + Your #{what} credentials do not have sufficient scope! + Scopes required: #{needed_human_scopes} + Scopes present: #{credentials_scopes} + Create a personal access token: + #{ALL_SCOPES_URL} + #{Utils::Shell.set_variable_in_profile("HOMEBREW_GITHUB_API_TOKEN", "your_token_here")} + EOS end def open_api(url, data: nil, data_binary_path: nil, request_method: nil, scopes: [].freeze, parse_json: true) diff --git a/Library/Homebrew/utils/shared_audits.rb b/Library/Homebrew/utils/shared_audits.rb index 1e4150553e..742f01f423 100644 --- a/Library/Homebrew/utils/shared_audits.rb +++ b/Library/Homebrew/utils/shared_audits.rb @@ -70,28 +70,20 @@ module SharedAudits def gitlab_repo_data(user, repo) @gitlab_repo_data ||= {} @gitlab_repo_data["#{user}/#{repo}"] ||= begin - out, _, status= curl_output("--request", "GET", "https://gitlab.com/api/v4/projects/#{user}%2F#{repo}") - return unless status.success? - - JSON.parse(out) + out, _, status = curl_output("https://gitlab.com/api/v4/projects/#{user}%2F#{repo}") + JSON.parse(out) if status.success? end - - @gitlab_repo_data["#{user}/#{repo}"] end def gitlab_release_data(user, repo, tag) id = "#{user}/#{repo}/#{tag}" @gitlab_release_data ||= {} @gitlab_release_data[id] ||= begin - out, _, status= curl_output( + out, _, status = curl_output( "https://gitlab.com/api/v4/projects/#{user}%2F#{repo}/releases/#{tag}", "--fail" ) - return unless status.success? - - JSON.parse(out) + JSON.parse(out) if status.success? end - - @gitlab_release_data[id] end GITLAB_PRERELEASE_ALLOWLIST = {}.freeze