| 
									
										
										
										
											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-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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def upload(local_file, repo:, package:, version:, remote_file:, sha256: nil) | 
					
						
							|  |  |  |     url = "#{API_URL}/content/#{@bintray_org}/#{repo}/#{package}/#{version}/#{remote_file}" | 
					
						
							| 
									
										
										
										
											2020-07-02 00:11:12 +10:00
										 |  |  |     args = ["--fail", "--upload-file", local_file] | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     args += ["--header", "X-Checksum-Sha2: #{sha256}"] unless sha256.blank? | 
					
						
							| 
									
										
										
										
											2020-06-10 17:50:10 +10:00
										 |  |  |     result = open_api url, *args | 
					
						
							|  |  |  |     json = JSON.parse(result.stdout) | 
					
						
							| 
									
										
										
										
											2020-06-10 18:06:56 +10:00
										 |  |  |     raise "Bottle upload failed: #{json["message"]}" if json["message"] != "success" | 
					
						
							| 
									
										
										
										
											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-07-02 00:11:12 +10:00
										 |  |  |     result = open_api url, "--request", "POST", "--fail" | 
					
						
							| 
									
										
										
										
											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-06-20 21:55:49 +10:00
										 |  |  |   def mirror_formula(formula, repo: "mirror", publish_package: 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, | 
					
						
							|  |  |  |       repo:        repo, | 
					
						
							|  |  |  |       package:     package, | 
					
						
							|  |  |  |       version:     version, | 
					
						
							|  |  |  |       sha256:      formula.stable.checksum, | 
					
						
							|  |  |  |       remote_file: filename, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-06-20 21:55:49 +10:00
										 |  |  |     return destination_url unless publish_package | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     odebug "Publishing #{@bintray_org}/#{repo}/#{package}/#{version}" | 
					
						
							|  |  |  |     publish(repo: repo, package: package, version: version, file_count: 1) | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def file_published?(repo:, remote_file:) | 
					
						
							|  |  |  |     url = "https://dl.bintray.com/#{@bintray_org}/#{repo}/#{remote_file}" | 
					
						
							|  |  |  |     begin | 
					
						
							| 
									
										
										
										
											2020-04-26 00:21:43 +01:00
										 |  |  |       curl "--fail", "--silent", "--head", "--output", "/dev/null", url | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     rescue ErrorDuringExecution => e | 
					
						
							| 
									
										
										
										
											2020-04-22 19:23:41 +01:00
										 |  |  |       stderr = e.output | 
					
						
							|  |  |  |                 .select { |type,| type == :stderr } | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |                 .map { |_, line| line } | 
					
						
							|  |  |  |                 .join | 
					
						
							|  |  |  |       raise if e.status.exitstatus != 22 && !stderr.include?("404 Not Found") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       false | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 00:11:55 +10:00
										 |  |  |   def upload_bottle_json(json_files, publish_package: false, warn_on_error: false) | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |     bottles_hash = json_files.reduce({}) do |hash, json_file| | 
					
						
							|  |  |  |       hash.deep_merge(JSON.parse(IO.read(json_file))) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       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-03-31 17:57:19 +11:00
										 |  |  |         odebug "Checking remote file #{@bintray_org}/#{bintray_repo}/#{filename}" | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |         if file_published? repo: bintray_repo, remote_file: filename | 
					
						
							| 
									
										
										
										
											2020-07-02 00:11:55 +10:00
										 |  |  |           already_published = "#{filename} is already published." | 
					
						
							|  |  |  |           failed_message = <<~EOS | 
					
						
							|  |  |  |             #{already_published} | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |             Please remove it manually from: | 
					
						
							|  |  |  |               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 | 
					
						
							| 
									
										
										
										
											2020-07-02 00:11:55 +10:00
										 |  |  |           raise Error, failed_message unless warn_on_error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           opoo already_published | 
					
						
							|  |  |  |           next | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if !formula_packaged[formula_name] && !package_exists?(repo: bintray_repo, package: bintray_package) | 
					
						
							| 
									
										
										
										
											2020-04-21 14:21:34 +01:00
										 |  |  |           odebug "Creating package #{@bintray_org}/#{bintray_repo}/#{bintray_package}" | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |           create_package repo: bintray_repo, package: bintray_package | 
					
						
							|  |  |  |           formula_packaged[formula_name] = true | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-10 00:30:32 +01:00
										 |  |  |         odebug "Uploading #{@bintray_org}/#{bintray_repo}/#{bintray_package}/#{version}/#{filename}" | 
					
						
							| 
									
										
										
										
											2020-03-30 19:35:54 +11:00
										 |  |  |         upload(tag_hash["local_filename"], | 
					
						
							|  |  |  |                repo:        bintray_repo, | 
					
						
							|  |  |  |                package:     bintray_package, | 
					
						
							|  |  |  |                version:     version, | 
					
						
							|  |  |  |                remote_file: filename, | 
					
						
							|  |  |  |                sha256:      sha256) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-06-10 17:50:10 +10:00
										 |  |  |       next unless publish_package | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       bottle_count = bottle_hash["bottle"]["tags"].length | 
					
						
							|  |  |  |       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 |