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

GitHub's raw endpoint is proving hilariously unreliable for us here.
This commit is contained in:
Mike McQuaid 2023-02-16 12:05:38 +00:00
parent 6f195c1e6f
commit f7f04bae26
No known key found for this signature in database
GPG Key ID: 3338A31AFDB1D829
4 changed files with 30 additions and 13 deletions

View File

@ -94,17 +94,29 @@ module Homebrew
end end
end end
sig { params(filepath: String, repo: String, git_head: T.nilable(String)).returns(String) } sig { params(name: String, git_head: T.nilable(String)).returns(String) }
def self.fetch_file_source(filepath, repo:, git_head: nil) def self.fetch_homebrew_cask_source(name, git_head: nil)
git_head ||= "master" git_head = "master" if git_head.blank?
endpoint = "#{git_head}/#{filepath}" raw_endpoint = "#{git_head}/Casks/#{name}.rb"
return cache[endpoint] if cache.present? && cache.key?(endpoint) return cache[raw_endpoint] if cache.present? && cache.key?(raw_endpoint)
raw_url = "https://raw.githubusercontent.com/#{repo}/#{endpoint}" # This API sometimes returns random 404s so needs a fallback at formulae.brew.sh.
output = Utils::Curl.curl_output("--fail", raw_url) raw_source_url = "https://raw.githubusercontent.com/Homebrew/homebrew-cask/#{raw_endpoint}"
raise ArgumentError, "No file found at #{Tty.underline}#{raw_url}#{Tty.reset}" unless output.success? api_source_url = "#{HOMEBREW_API_DEFAULT_DOMAIN}/cask-source/#{name}.rb"
output = Utils::Curl.curl_output("--fail", raw_source_url)
cache[endpoint] = output.stdout 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[raw_endpoint] = output.stdout
end end
sig { params(json: Hash).returns(Hash) } sig { params(json: Hash).returns(Hash) }

View File

@ -17,7 +17,7 @@ module Homebrew
sig { params(token: String, git_head: T.nilable(String)).returns(String) } sig { params(token: String, git_head: T.nilable(String)).returns(String) }
def fetch_source(token, git_head: nil) 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 end
sig { returns(Hash) } sig { returns(Hash) }

View File

@ -8,6 +8,7 @@ describe Homebrew::API::Cask do
before do before do
stub_const("Homebrew::API::HOMEBREW_CACHE_API", cache_dir) stub_const("Homebrew::API::HOMEBREW_CACHE_API", cache_dir)
Homebrew::API.clear_cache
end end
def mock_curl_download(stdout:) def mock_curl_download(stdout:)

View File

@ -9,6 +9,10 @@ describe Homebrew::API do
let(:json_hash) { JSON.parse(json) } let(:json_hash) { JSON.parse(json) }
let(:json_invalid) { '{"foo":"bar"' } let(:json_invalid) { '{"foo":"bar"' }
before do
described_class.clear_cache
end
def mock_curl_output(stdout: "", success: true) def mock_curl_output(stdout: "", success: true)
curl_output = OpenStruct.new(stdout: stdout, success?: success) curl_output = OpenStruct.new(stdout: stdout, success?: success)
allow(Utils::Curl).to receive(:curl_output).and_return curl_output allow(Utils::Curl).to receive(:curl_output).and_return curl_output
@ -68,15 +72,15 @@ describe Homebrew::API do
describe "::fetch_file_source" do describe "::fetch_file_source" do
it "fetches a file" do it "fetches a file" do
mock_curl_output stdout: json 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 expect(fetched_json).to eq json
end end
it "raises an error if the file does not exist" do it "raises an error if the file does not exist" do
mock_curl_output success: false mock_curl_output success: false
expect { expect {
described_class.fetch_file_source("bar.txt", repo: "Homebrew/homebrew-core", git_head: "master") described_class.fetch_homebrew_cask_source("bar", git_head: "master")
}.to raise_error(ArgumentError, /No file found/) }.to raise_error(ArgumentError, /No valid file found/)
end end
end end
end end