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
 | 
			
		||||
 | 
			
		||||
    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)
 | 
			
		||||
    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)
 | 
			
		||||
 | 
			
		||||
      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?
 | 
			
		||||
      # 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)
 | 
			
		||||
 | 
			
		||||
      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
 | 
			
		||||
 | 
			
		||||
    sig { params(json: Hash).returns(Hash) }
 | 
			
		||||
 | 
			
		||||
@ -17,7 +17,7 @@ module Homebrew
 | 
			
		||||
 | 
			
		||||
        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) }
 | 
			
		||||
 | 
			
		||||
@ -8,6 +8,7 @@ describe Homebrew::API::Cask do
 | 
			
		||||
 | 
			
		||||
  before do
 | 
			
		||||
    stub_const("Homebrew::API::HOMEBREW_CACHE_API", cache_dir)
 | 
			
		||||
    Homebrew::API.clear_cache
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def mock_curl_download(stdout:)
 | 
			
		||||
 | 
			
		||||
@ -9,6 +9,10 @@ describe Homebrew::API do
 | 
			
		||||
  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
 | 
			
		||||
@ -68,15 +72,15 @@ describe Homebrew::API do
 | 
			
		||||
  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
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user