Merge pull request #14655 from MikeMcQuaid/api_cask_source
api: use formulae.brew.sh for cask-source API again.
This commit is contained in:
commit
49a1daebab
@ -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) }
|
||||||
|
|||||||
@ -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) }
|
||||||
|
|||||||
@ -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:)
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user