Class: Bintray Private

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Context, Utils::Curl
Defined in:
bintray.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Bintray API client.

Defined Under Namespace

Classes: Error

Constant Summary collapse

API_URL =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"https://api.bintray.com"
URL_REGEX =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

%r{^https://[\w-]+\.bintray\.com/}.freeze

Instance Method Summary collapse

Methods included from Utils::Curl

curl, curl_args, curl_check_http_content, curl_download, curl_executable, curl_http_content_headers_and_checksum, curl_output, curl_with_workarounds, http_status_ok?, url_protected_by_cloudflare?, url_protected_by_incapsula?

Methods included from Context

current, current=, #debug?, #quiet?, #verbose?, #with_context

Constructor Details

#initialize(org: "homebrew") ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • org (String, nil) (defaults to: "homebrew")

Raises:



28
29
30
31
32
33
34
# File 'bintray.rb', line 28

def initialize(org: "homebrew")
  @bintray_org = org

  raise UsageError, "Must set a Bintray organisation!" unless @bintray_org

  ENV["HOMEBREW_FORCE_HOMEBREW_ON_LINUX"] = "1" if @bintray_org == "homebrew" && !OS.mac?
end

Instance Method Details

#create_package(repo:, package:) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



154
155
156
157
158
159
# File 'bintray.rb', line 154

def create_package(repo:, package:)
  url = "#{API_URL}/packages/#{@bintray_org}/#{repo}"
  data = { name: package, public_download_numbers: true }
  data[:public_stats] = official_org?
  open_api(url, "--header", "Content-Type: application/json", "--request", "POST", "--data", data.to_json)
end

#file_delete_instructions(bintray_repo, bintray_package, filename) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



196
197
198
199
200
201
202
203
204
# File 'bintray.rb', line 196

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

#inspectString

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



23
24
25
# File 'bintray.rb', line 23

def inspect
  "#<Bintray: org=#{@bintray_org}>"
end

#mirror_formula(formula, repo: "mirror", publish_package: false, warn_on_error: false) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • formula (Formula)
  • repo (String) (defaults to: "mirror")
  • publish_package (Boolean) (defaults to: false)
  • warn_on_error (Boolean) (defaults to: false)

Returns:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'bintray.rb', line 123

def mirror_formula(formula, repo: "mirror", publish_package: false, warn_on_error: false)
  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,
    warn_on_error: warn_on_error,
  )
  return destination_url unless publish_package

  odebug "Publishing #{@bintray_org}/#{repo}/#{package}/#{version}"
  publish(repo: repo, package: package, version: version, file_count: 1, warn_on_error: warn_on_error)

  destination_url
end

#official_org?(org: @bintray_org) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • org (String, nil) (defaults to: @bintray_org)

Returns:

  • (Boolean)


106
107
108
# File 'bintray.rb', line 106

def official_org?(org: @bintray_org)
  %w[homebrew linuxbrew].include? org
end

#open_api(url, *args, auth: true) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



36
37
38
39
40
41
42
43
44
45
# File 'bintray.rb', line 36

def open_api(url, *args, auth: true)
  if auth
    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)

    args += ["--user", "#{user}:#{key}"]
  end

  curl(*args, url, print_stdout: false, secrets: key)
end

#package_exists?(repo:, package:) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'bintray.rb', line 162

def package_exists?(repo:, package:)
  url = "#{API_URL}/packages/#{@bintray_org}/#{repo}/#{package}"
  begin
    open_api(url, "--fail", "--silent", "--output", "/dev/null", auth: false)
  rescue ErrorDuringExecution => e
    stderr = e.output
              .select { |type,| type == :stderr }
              .map { |_, line| line }
              .join
    raise if e.status.exitstatus != 22 && stderr.exclude?("404 Not Found")

    false
  else
    true
  end
end

#publish(repo:, package:, version:, file_count:, warn_on_error: false) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • repo (String)
  • package (String)
  • version (String)
  • file_count (Integer, nil)
  • warn_on_error (Boolean, nil) (defaults to: false)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'bintray.rb', line 89

def publish(repo:, package:, version:, file_count:, warn_on_error: false)
  url = "#{API_URL}/content/#{@bintray_org}/#{repo}/#{package}/#{version}/publish"
  upload_args = %w[--request POST]
  upload_args += ["--fail"] unless warn_on_error
  result = T.unsafe(self).open_api(url, *upload_args)
  json = JSON.parse(result.stdout)
  if file_count.present? && json["files"] != file_count
    message = "Bottle publish failed: expected #{file_count} bottles, but published #{json["files"]} instead."
    raise message unless warn_on_error

    opoo message
  end

  odebug "Published #{json["files"]} bottles"
end

#remote_checksum(repo:, remote_file:) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Gets the SHA-256 checksum of the specified remote file.

Parameters:

Returns:

  • (String, nil)

    the checksum, the empty string (if the file doesn’t have a checksum), nil (if the file doesn’t exist)



183
184
185
186
187
188
189
190
191
192
193
# File 'bintray.rb', line 183

def remote_checksum(repo:, remote_file:)
  url = "https://dl.bintray.com/#{@bintray_org}/#{repo}/#{remote_file}"
  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 || ""
  else
    raise Error if result.status.exitstatus != 22 && result.stderr.exclude?("404 Not Found")

    nil
  end
end

#stable_mirrored?(url) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


111
112
113
114
115
# File 'bintray.rb', line 111

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

#upload(local_file, repo:, package:, version:, remote_file:, sha256: nil, warn_on_error: false) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • local_file (String)
  • repo (String)
  • package (String)
  • version (String)
  • remote_file (String)
  • sha256 (String, nil) (defaults to: nil)
  • warn_on_error (Boolean, nil) (defaults to: false)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'bintray.rb', line 56

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

  url = "#{API_URL}/content/#{@bintray_org}/#{repo}/#{package}/#{version}/#{remote_file}"
  args = ["--upload-file", local_file]
  args += ["--header", "X-Checksum-Sha2: #{sha256}"] if sha256.present?
  args << "--fail" unless warn_on_error

  result = T.unsafe(self).open_api(url, *args)

  json = JSON.parse(result.stdout)
  return if json["message"] == "success"

  msg = "Bottle upload failed: #{json["message"]}"
  raise msg unless warn_on_error

  opoo msg
end

#upload_bottles(bottles_hash, publish_package: false, warn_on_error: false) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • bottles_hash (Hash{String => T.untyped})
  • publish_package (Boolean) (defaults to: false)
  • warn_on_error (Boolean, nil) (defaults to: false)


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'bintray.rb', line 211

def upload_bottles(bottles_hash, publish_package: false, warn_on_error: false)
  formula_packaged = {}

  bottles_hash.each do |formula_name, bottle_hash|
    version = ERB::Util.url_encode(bottle_hash["formula"]["pkg_version"])
    bintray_package = bottle_hash["bintray"]["package"]
    bintray_repo = bottle_hash["bintray"]["repository"]
    bottle_count = bottle_hash["bottle"]["tags"].length

    bottle_hash["bottle"]["tags"].each do |tag, tag_hash|
      filename = Bottle::Filename.new(bottle_hash["formula"]["name"], bottle_hash["formula"]["pkg_version"],
                                      tag, bottle_hash["bottle"]["rebuild"]).bintray
      sha256 = tag_hash["sha256"]
      delete_instructions = file_delete_instructions(bintray_repo, bintray_package, filename)

      odebug "Checking remote file #{@bintray_org}/#{bintray_repo}/#{filename}"
      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"],
               repo:          bintray_repo,
               package:       bintray_package,
               version:       version,
               remote_file:   filename,
               sha256:        sha256,
               warn_on_error: warn_on_error)
      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.
        failed_message = <<~EOS
          #{filename} is already published with a mismatched hash!
            Expected: #{sha256}
            Actual:   #{result}
        EOS
        raise Error, "#{failed_message}#{delete_instructions}" unless warn_on_error

        opoo failed_message
      end
    end
    next unless publish_package

    odebug "Publishing #{@bintray_org}/#{bintray_repo}/#{bintray_package}/#{version}"
    publish(repo:          bintray_repo,
            package:       bintray_package,
            version:       version,
            file_count:    bottle_count,
            warn_on_error: warn_on_error)
  end
end