| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "utils/curl" | 
					
						
							|  |  |  | require "json" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 02:02:31 +02:00
										 |  |  | # Bintray API client. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # @api private | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  | class Bintray | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   extend T::Sig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |   include Context | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |   API_URL = "https://api.bintray.com" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   class Error < RuntimeError | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |   def inspect | 
					
						
							| 
									
										
										
										
											2020-07-23 01:20:31 +02:00
										 |  |  |     "#<Bintray: org=#{@bintray_org}>" | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 14:32:31 +02:00
										 |  |  |   def initialize(org: "homebrew") | 
					
						
							| 
									
										
										
										
											2020-03-30 22:29:31 +11:00
										 |  |  |     @bintray_org = org | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-31 17:57:19 +11:00
										 |  |  |     raise UsageError, "Must set a Bintray organisation!" unless @bintray_org | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     ENV["HOMEBREW_FORCE_HOMEBREW_ON_LINUX"] = "1" if @bintray_org == "homebrew" && !OS.mac? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def open_api(url, *extra_curl_args, auth: true) | 
					
						
							|  |  |  |     args = extra_curl_args | 
					
						
							| 
									
										
										
										
											2020-07-23 01:20:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if auth | 
					
						
							| 
									
										
										
										
											2020-07-25 22:05:38 +01:00
										 |  |  |       raise UsageError, "HOMEBREW_BINTRAY_USER is unset." unless (user = Homebrew::EnvConfig.bintray_user) | 
					
						
							|  |  |  |       raise UsageError, "HOMEBREW_BINTRAY_KEY is unset." unless (key = Homebrew::EnvConfig.bintray_key) | 
					
						
							| 
									
										
										
										
											2020-07-23 01:20:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       args += ["--user", "#{user}:#{key}"] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     curl(*args, url, | 
					
						
							| 
									
										
										
										
											2020-09-08 22:30:22 +02:00
										 |  |  |          print_stdout: false, | 
					
						
							|  |  |  |          secrets:      key) | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-22 20:58:28 +11:00
										 |  |  |   def upload(local_file, repo:, package:, version:, remote_file:, sha256: nil, warn_on_error: false) | 
					
						
							|  |  |  |     unless File.exist? local_file | 
					
						
							|  |  |  |       msg = "#{local_file} for upload doesn't exist!" | 
					
						
							|  |  |  |       raise Error, msg unless warn_on_error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # Warn and return early here since we know this upload is going to fail. | 
					
						
							|  |  |  |       opoo msg | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     url = "#{API_URL}/content/#{@bintray_org}/#{repo}/#{package}/#{version}/#{remote_file}" | 
					
						
							| 
									
										
										
										
											2020-10-22 20:58:28 +11:00
										 |  |  |     args = ["--upload-file", local_file] | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     args += ["--header", "X-Checksum-Sha2: #{sha256}"] unless sha256.blank? | 
					
						
							| 
									
										
										
										
											2020-10-22 20:58:28 +11:00
										 |  |  |     args << "--fail" unless warn_on_error | 
					
						
							|  |  |  |     result = open_api(url, *args) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 17:50:10 +10:00
										 |  |  |     json = JSON.parse(result.stdout) | 
					
						
							| 
									
										
										
										
											2020-10-22 20:58:28 +11:00
										 |  |  |     if json["message"] != "success" | 
					
						
							|  |  |  |       msg = "Bottle upload failed: #{json["message"]}" | 
					
						
							|  |  |  |       raise msg unless warn_on_error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       opoo msg | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2020-06-10 17:50:10 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     result | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 00:11:55 +10:00
										 |  |  |   def publish(repo:, package:, version:, file_count:, warn_on_error: false) | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     url = "#{API_URL}/content/#{@bintray_org}/#{repo}/#{package}/#{version}/publish" | 
					
						
							| 
									
										
										
										
											2020-10-22 20:58:28 +11:00
										 |  |  |     upload_args = %w[--request POST] | 
					
						
							|  |  |  |     upload_args << "--fail" unless warn_on_error | 
					
						
							|  |  |  |     result = open_api(url, *upload_args) | 
					
						
							| 
									
										
										
										
											2020-06-10 17:50:10 +10:00
										 |  |  |     json = JSON.parse(result.stdout) | 
					
						
							| 
									
										
										
										
											2020-06-10 18:06:56 +10:00
										 |  |  |     if file_count.present? && json["files"] != file_count | 
					
						
							| 
									
										
										
										
											2020-07-02 00:11:55 +10:00
										 |  |  |       message = "Bottle publish failed: expected #{file_count} bottles, but published #{json["files"]} instead." | 
					
						
							|  |  |  |       raise message unless warn_on_error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       opoo message | 
					
						
							| 
									
										
										
										
											2020-06-10 17:50:10 +10:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     odebug "Published #{json["files"]} bottles" | 
					
						
							|  |  |  |     result | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def official_org?(org: @bintray_org) | 
					
						
							|  |  |  |     %w[homebrew linuxbrew].include? org | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 17:48:28 +02:00
										 |  |  |   def stable_mirrored?(url) | 
					
						
							|  |  |  |     headers, = curl_output("--connect-timeout", "15", "--location", "--head", url) | 
					
						
							|  |  |  |     status_code = headers.scan(%r{^HTTP/.* (\d+)}).last.first | 
					
						
							|  |  |  |     status_code.start_with?("2") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-22 20:58:28 +11:00
										 |  |  |   def mirror_formula(formula, repo: "mirror", publish_package: false, warn_on_error: false) | 
					
						
							| 
									
										
										
										
											2020-06-08 17:48:28 +02:00
										 |  |  |     package = Utils::Bottles::Bintray.package formula.name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     create_package(repo: repo, package: package) unless package_exists?(repo: repo, package: package) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     formula.downloader.fetch | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     version = ERB::Util.url_encode(formula.pkg_version) | 
					
						
							|  |  |  |     filename = ERB::Util.url_encode(formula.downloader.basename) | 
					
						
							|  |  |  |     destination_url = "https://dl.bintray.com/#{@bintray_org}/#{repo}/#{filename}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     odebug "Uploading to #{destination_url}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     upload( | 
					
						
							|  |  |  |       formula.downloader.cached_location, | 
					
						
							| 
									
										
										
										
											2020-10-22 20:58:28 +11:00
										 |  |  |       repo:          repo, | 
					
						
							|  |  |  |       package:       package, | 
					
						
							|  |  |  |       version:       version, | 
					
						
							|  |  |  |       sha256:        formula.stable.checksum, | 
					
						
							|  |  |  |       remote_file:   filename, | 
					
						
							|  |  |  |       warn_on_error: warn_on_error, | 
					
						
							| 
									
										
										
										
											2020-06-08 17:48:28 +02:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-06-20 21:55:49 +10:00
										 |  |  |     return destination_url unless publish_package | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     odebug "Publishing #{@bintray_org}/#{repo}/#{package}/#{version}" | 
					
						
							| 
									
										
										
										
											2020-10-22 20:58:28 +11:00
										 |  |  |     publish(repo: repo, package: package, version: version, file_count: 1, warn_on_error: warn_on_error) | 
					
						
							| 
									
										
										
										
											2020-06-08 17:48:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     destination_url | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |   def create_package(repo:, package:, **extra_data_args) | 
					
						
							| 
									
										
										
										
											2020-04-21 14:21:34 +01:00
										 |  |  |     url = "#{API_URL}/packages/#{@bintray_org}/#{repo}" | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     data = { name: package, public_download_numbers: true } | 
					
						
							|  |  |  |     data[:public_stats] = official_org? | 
					
						
							|  |  |  |     data.merge! extra_data_args | 
					
						
							| 
									
										
										
										
											2020-04-21 14:21:34 +01:00
										 |  |  |     open_api url, "--header", "Content-Type: application/json", "--request", "POST", "--data", data.to_json | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def package_exists?(repo:, package:) | 
					
						
							|  |  |  |     url = "#{API_URL}/packages/#{@bintray_org}/#{repo}/#{package}" | 
					
						
							| 
									
										
										
										
											2020-04-21 14:21:34 +01:00
										 |  |  |     begin | 
					
						
							| 
									
										
										
										
											2020-06-10 17:50:10 +10:00
										 |  |  |       open_api url, "--fail", "--silent", "--output", "/dev/null", auth: false | 
					
						
							| 
									
										
										
										
											2020-04-21 14:21:34 +01:00
										 |  |  |     rescue ErrorDuringExecution => e | 
					
						
							| 
									
										
										
										
											2020-04-22 19:23:41 +01:00
										 |  |  |       stderr = e.output | 
					
						
							|  |  |  |                 .select { |type,| type == :stderr } | 
					
						
							| 
									
										
										
										
											2020-04-21 14:21:34 +01:00
										 |  |  |                 .map { |_, line| line } | 
					
						
							|  |  |  |                 .join | 
					
						
							|  |  |  |       raise if e.status.exitstatus != 22 && !stderr.include?("404 Not Found") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       false | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 20:56:14 +11:00
										 |  |  |   # Gets the SHA-256 checksum of the specified remote file. | 
					
						
							| 
									
										
										
										
											2020-11-05 15:19:56 -05:00
										 |  |  |   # | 
					
						
							|  |  |  |   # @return the empty string if the file exists but doesn't have a checksum. | 
					
						
							|  |  |  |   # @return [nil] if the file doesn't exist. | 
					
						
							| 
									
										
										
										
											2020-10-12 20:56:14 +11:00
										 |  |  |   def remote_checksum(repo:, remote_file:) | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     url = "https://dl.bintray.com/#{@bintray_org}/#{repo}/#{remote_file}" | 
					
						
							| 
									
										
										
										
											2020-10-12 20:56:14 +11:00
										 |  |  |     result = curl_output "--fail", "--silent", "--head", url | 
					
						
							|  |  |  |     if result.success? | 
					
						
							|  |  |  |       result.stdout.match(/^X-Checksum-Sha2:\s+(\h{64})\b/i)&.values_at(1)&.first || "" | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2020-10-12 20:56:14 +11:00
										 |  |  |       raise Error if result.status.exitstatus != 22 && !result.stderr.include?("404 Not Found") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       nil | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 20:56:14 +11:00
										 |  |  |   def file_delete_instructions(bintray_repo, bintray_package, filename) | 
					
						
							|  |  |  |     <<~EOS | 
					
						
							|  |  |  |       Remove this file manually in your web browser: | 
					
						
							|  |  |  |         https://bintray.com/#{@bintray_org}/#{bintray_repo}/#{bintray_package}/view#files | 
					
						
							|  |  |  |       Or run: | 
					
						
							|  |  |  |         curl -X DELETE -u $HOMEBREW_BINTRAY_USER:$HOMEBREW_BINTRAY_KEY \\ | 
					
						
							|  |  |  |         https://api.bintray.com/content/#{@bintray_org}/#{bintray_repo}/#{filename} | 
					
						
							|  |  |  |     EOS | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 14:21:55 +02:00
										 |  |  |   def upload_bottles(bottles_hash, publish_package: false, warn_on_error: false) | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     formula_packaged = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bottles_hash.each do |formula_name, bottle_hash| | 
					
						
							| 
									
										
										
										
											2020-05-10 00:30:32 +01:00
										 |  |  |       version = ERB::Util.url_encode(bottle_hash["formula"]["pkg_version"]) | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |       bintray_package = bottle_hash["bintray"]["package"] | 
					
						
							|  |  |  |       bintray_repo = bottle_hash["bintray"]["repository"] | 
					
						
							| 
									
										
										
										
											2020-10-12 20:56:14 +11:00
										 |  |  |       bottle_count = bottle_hash["bottle"]["tags"].length | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |       bottle_hash["bottle"]["tags"].each do |_tag, tag_hash| | 
					
						
							| 
									
										
										
										
											2020-05-10 00:30:32 +01:00
										 |  |  |         filename = tag_hash["filename"] # URL encoded in Bottle::Filename#bintray | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |         sha256 = tag_hash["sha256"] | 
					
						
							| 
									
										
										
										
											2020-10-12 20:56:14 +11:00
										 |  |  |         delete_instructions = file_delete_instructions(bintray_repo, bintray_package, filename) | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-31 17:57:19 +11:00
										 |  |  |         odebug "Checking remote file #{@bintray_org}/#{bintray_repo}/#{filename}" | 
					
						
							| 
									
										
										
										
											2020-10-12 20:56:14 +11:00
										 |  |  |         result = remote_checksum(repo: bintray_repo, remote_file: filename) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         case result | 
					
						
							|  |  |  |         when nil | 
					
						
							|  |  |  |           # File doesn't exist. | 
					
						
							|  |  |  |           if !formula_packaged[formula_name] && !package_exists?(repo: bintray_repo, package: bintray_package) | 
					
						
							|  |  |  |             odebug "Creating package #{@bintray_org}/#{bintray_repo}/#{bintray_package}" | 
					
						
							|  |  |  |             create_package repo: bintray_repo, package: bintray_package | 
					
						
							|  |  |  |             formula_packaged[formula_name] = true | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           odebug "Uploading #{@bintray_org}/#{bintray_repo}/#{bintray_package}/#{version}/#{filename}" | 
					
						
							|  |  |  |           upload(tag_hash["local_filename"], | 
					
						
							| 
									
										
										
										
											2020-10-22 20:58:28 +11:00
										 |  |  |                  repo:          bintray_repo, | 
					
						
							|  |  |  |                  package:       bintray_package, | 
					
						
							|  |  |  |                  version:       version, | 
					
						
							|  |  |  |                  remote_file:   filename, | 
					
						
							|  |  |  |                  sha256:        sha256, | 
					
						
							|  |  |  |                  warn_on_error: warn_on_error) | 
					
						
							| 
									
										
										
										
											2020-10-12 20:56:14 +11:00
										 |  |  |         when sha256 | 
					
						
							|  |  |  |           # File exists, checksum matches. | 
					
						
							|  |  |  |           odebug "#{filename} is already published with matching hash." | 
					
						
							|  |  |  |           bottle_count -= 1
 | 
					
						
							|  |  |  |         when "" | 
					
						
							|  |  |  |           # File exists, but can't find checksum | 
					
						
							|  |  |  |           failed_message = "#{filename} is already published!" | 
					
						
							|  |  |  |           raise Error, "#{failed_message}\n#{delete_instructions}" unless warn_on_error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           opoo failed_message | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           # File exists, but checksum either doesn't exist or is mismatched. | 
					
						
							| 
									
										
										
										
											2020-07-02 00:11:55 +10:00
										 |  |  |           failed_message = <<~EOS | 
					
						
							| 
									
										
										
										
											2020-10-12 20:56:14 +11:00
										 |  |  |             #{filename} is already published with a mismatched hash! | 
					
						
							|  |  |  |               Expected: #{sha256} | 
					
						
							|  |  |  |               Actual:   #{result} | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |           EOS | 
					
						
							| 
									
										
										
										
											2020-10-12 20:56:14 +11:00
										 |  |  |           raise Error, "#{failed_message}#{delete_instructions}" unless warn_on_error | 
					
						
							| 
									
										
										
										
											2020-07-02 00:11:55 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 20:56:14 +11:00
										 |  |  |           opoo failed_message | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-06-10 17:50:10 +10:00
										 |  |  |       next unless publish_package | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       odebug "Publishing #{@bintray_org}/#{bintray_repo}/#{bintray_package}/#{version}" | 
					
						
							| 
									
										
										
										
											2020-07-02 00:11:55 +10:00
										 |  |  |       publish(repo:          bintray_repo, | 
					
						
							|  |  |  |               package:       bintray_package, | 
					
						
							|  |  |  |               version:       version, | 
					
						
							|  |  |  |               file_count:    bottle_count, | 
					
						
							|  |  |  |               warn_on_error: warn_on_error) | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |