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

api: use formulae.brew.sh for cask-source API again. #14655

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 22 additions & 10 deletions Library/Homebrew/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,29 @@ def self.fetch_json_api_file(endpoint, target:)
end
end

sig { params(filepath: String, repo: String, git_head: T.nilable(String)).returns(String) }
def self.fetch_file_source(filepath, repo:, git_head: nil)
git_head ||= "master"
endpoint = "#{git_head}/#{filepath}"
return cache[endpoint] if cache.present? && cache.key?(endpoint)

raw_url = "https://raw.githubusercontent.com/#{repo}/#{endpoint}"
output = Utils::Curl.curl_output("--fail", raw_url)
raise ArgumentError, "No file found at #{Tty.underline}#{raw_url}#{Tty.reset}" unless output.success?
sig { params(name: String, git_head: T.nilable(String)).returns(String) }
def self.fetch_homebrew_cask_source(name, git_head: nil)
git_head = "master" if git_head.blank?
raw_endpoint = "#{git_head}/Casks/#{name}.rb"
return cache[raw_endpoint] if cache.present? && cache.key?(raw_endpoint)

# This API sometimes returns random 404s so needs a fallback at formulae.brew.sh.
raw_source_url = "https://raw.githubusercontent.com/Homebrew/homebrew-cask/#{raw_endpoint}"
api_source_url = "#{HOMEBREW_API_DEFAULT_DOMAIN}/cask-source/#{name}.rb"
output = Utils::Curl.curl_output("--fail", raw_source_url)

if !output.success? || output.blank?
output = Utils::Curl.curl_output("--fail", api_source_url)
if !output.success? || output.blank?
raise ArgumentError, <<~EOS
No valid file found at either of:
#{Tty.underline}#{raw_source_url}#{Tty.reset}
#{Tty.underline}#{api_source_url}#{Tty.reset}
EOS
end
end

cache[endpoint] = output.stdout
cache[raw_endpoint] = output.stdout
end

sig { params(json: Hash).returns(Hash) }
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/api/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def fetch(token)

sig { params(token: String, git_head: T.nilable(String)).returns(String) }
def fetch_source(token, git_head: nil)
Homebrew::API.fetch_file_source "Casks/#{token}.rb", repo: "Homebrew/homebrew-cask", git_head: git_head
Homebrew::API.fetch_homebrew_cask_source token, git_head: git_head
end

sig { returns(Hash) }
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/test/api/cask_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

before do
stub_const("Homebrew::API::HOMEBREW_CACHE_API", cache_dir)
Homebrew::API.clear_cache
end

def mock_curl_download(stdout:)
Expand Down
10 changes: 7 additions & 3 deletions Library/Homebrew/test/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
let(:json_hash) { JSON.parse(json) }
let(:json_invalid) { '{"foo":"bar"' }

before do
described_class.clear_cache
end

def mock_curl_output(stdout: "", success: true)
curl_output = OpenStruct.new(stdout: stdout, success?: success)
allow(Utils::Curl).to receive(:curl_output).and_return curl_output
Expand Down Expand Up @@ -68,15 +72,15 @@ def mock_curl_download(stdout:)
describe "::fetch_file_source" do
it "fetches a file" do
mock_curl_output stdout: json
fetched_json = described_class.fetch_file_source("foo.json", repo: "Homebrew/homebrew-core", git_head: "master")
fetched_json = described_class.fetch_homebrew_cask_source("foo", git_head: "master")
expect(fetched_json).to eq json
end

it "raises an error if the file does not exist" do
mock_curl_output success: false
expect {
described_class.fetch_file_source("bar.txt", repo: "Homebrew/homebrew-core", git_head: "master")
}.to raise_error(ArgumentError, /No file found/)
described_class.fetch_homebrew_cask_source("bar", git_head: "master")
}.to raise_error(ArgumentError, /No valid file found/)
end
end
end