Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate-cask-api: fall back to previous URL if network fails #18390

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Library/Homebrew/extend/api_hashable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,43 @@

# Used to substitute common paths with generic placeholders when generating JSON for the API.
module APIHashable
extend T::Helpers

requires_ancestor { Module }

module CaskURLFallbackExtension
extend T::Helpers

requires_ancestor { Kernel }
requires_ancestor { Cask::Cask }

def url(...)
url = super

# URLs with blocks are lazy-evaluated, so we let's force an evaluation to ensure the URL can be determined
begin
url.to_s
rescue
raise unless self.class.generating_hash?
raise unless (json_cask = Homebrew::API::Cask.all_casks[token])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise unless (json_cask = Homebrew::API::Cask.all_casks[token])
json_cask = Homebrew::API::Cask.all_casks.fetch(token).presence
raise unless json_cask


prev_url = json_cask["url"]
prev_specs = json_cask["url_specs"] || {}
url = Cask::URL.new(prev_url, **prev_specs)

Check warning on line 28 in Library/Homebrew/extend/api_hashable.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/extend/api_hashable.rb#L26-L28

Added lines #L26 - L28 were not covered by tests
Comment on lines +26 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
prev_url = json_cask["url"]
prev_specs = json_cask["url_specs"] || {}
url = Cask::URL.new(prev_url, **prev_specs)
previous_url = json_cask["url"]
previous_specs = json_cask["url_specs"] || {}
url = Cask::URL.new(previous_url, **previous_specs)


opoo "Unable to determine URL for #{token}. Falling back to previous value: #{url}"

Check warning on line 30 in Library/Homebrew/extend/api_hashable.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/extend/api_hashable.rb#L30

Added line #L30 was not covered by tests
end

url
end
end

def self.extended(base)
return if base != Cask::Cask

base.prepend CaskURLFallbackExtension
end

def generating_hash!
return if generating_hash?

Expand Down
Loading