Skip to content

Commit

Permalink
rubocop: fix Lint/NoReturnInBeginEndBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jonchang committed Nov 10, 2020
1 parent 20b8fcd commit 500908e
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 53 deletions.
8 changes: 6 additions & 2 deletions Library/Homebrew/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ def stale_formula?(scrub)
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"
Expand All @@ -113,9 +115,11 @@ def stale_cask?(scrub)
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)
Expand Down
8 changes: 6 additions & 2 deletions Library/Homebrew/dev-cmd/pr-pull.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,19 @@ def determine_bump_subject(old_contents, new_contents, formula_path, reason: nil
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
Expand Down
4 changes: 3 additions & 1 deletion Library/Homebrew/extend/os/mac/unpack_strategy/zip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ def extract_to_dir(unpack_dir, basename:, verbose:)
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] }
Expand Down
4 changes: 3 additions & 1 deletion Library/Homebrew/extend/os/mac/utils/bottles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ def find_older_compatible_tag(tag)
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
Expand Down
4 changes: 3 additions & 1 deletion Library/Homebrew/rubocops/cask/homepage_matches_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ def url_match_homepage?(stanza)
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
Expand Down
4 changes: 3 additions & 1 deletion Library/Homebrew/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ def search_taps(query, silent: false)
)
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"])
Expand Down
60 changes: 27 additions & 33 deletions Library/Homebrew/utils/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,43 +141,37 @@ def api_credentials_type
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?

needed_human_scopes = "none" if needed_human_scopes.blank?
credentials_scopes = "none" if 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
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 = needed_scopes.join(", ")
credentials_scopes = response_headers["x-oauth-scopes"]
return if needed_human_scopes.blank? && credentials_scopes.blank?

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)
Expand Down
16 changes: 4 additions & 12 deletions Library/Homebrew/utils/shared_audits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,20 @@ def github_release(user, repo, tag, formula: nil, cask: nil)
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
Expand Down

0 comments on commit 500908e

Please sign in to comment.