class Dpl::Providers::Bintray

Constants

MAP
PATHS

Public Instance Methods

append_params(path, params) click to toggle source
# File lib/dpl/providers/bintray.rb, line 230
def append_params(path, params)
  [path, *Array(params).map { |pair| pair.join('=') }].join(';')
end
compact(hash) click to toggle source
# File lib/dpl/providers/bintray.rb, line 305
def compact(hash)
  hash.reject { |_, value| value.nil? }
end
create_package() click to toggle source
# File lib/dpl/providers/bintray.rb, line 86
def create_package
  info :create_package
  post(path(:packages), compact(only(package, *MAP[:package])))
  return unless package_attrs

  info :package_attrs
  post(path(:package_attrs), package_attrs)
end
create_version() click to toggle source
# File lib/dpl/providers/bintray.rb, line 99
def create_version
  info :create_version
  post(path(:versions), compact(only(version, *MAP[:version])))
  return unless version_attrs

  info :version_attrs
  post(path(:version_attrs), version_attrs)
end
deploy() click to toggle source
# File lib/dpl/providers/bintray.rb, line 74
def deploy
  create_package unless package_exists?
  create_version unless version_exists?
  upload_files
  sign_version if sign_version?
  publish_version && update_files if publish_version?
end
descriptor() click to toggle source
# File lib/dpl/providers/bintray.rb, line 244
def descriptor
  @descriptor ||= symbolize(JSON.parse(File.read(file)))
rescue StandardError
  error :invalid_file
end
excluded?(path, pattern) click to toggle source
# File lib/dpl/providers/bintray.rb, line 175
def excluded?(path, pattern)
  !pattern.to_s.empty? && path.match(/#{pattern}/)
end
exists?(type) click to toggle source
# File lib/dpl/providers/bintray.rb, line 188
def exists?(type)
  case code = head(path(type), raise: false, silent: true)
  when 200, 201 then true
  when 404 then false
  else error :unexpected_code, code, type
  end
end
files() click to toggle source
# File lib/dpl/providers/bintray.rb, line 152
def files
  return {} unless files = descriptor[:files]
  return @files if @files

  keys = %i[path includePattern excludePattern uploadPattern matrixParams listInDownloads]
  files = files.map { |file| file if file[:path] = path_for(file[:includePattern]) }
  @files = files.compact.map { |file| find(*file.values_at(*keys)) }.flatten
end
find(path, includes, excludes, uploads, params, download) click to toggle source
# File lib/dpl/providers/bintray.rb, line 161
def find(path, includes, excludes, uploads, params, download)
  paths = Find.find(path).select { |path| File.file?(path) }
  paths = paths.reject { |path| excluded?(path, excludes) }
  paths = paths.map { |path| [path, path.match(/#{includes}/)] }
  paths = paths.select(&:last)
  paths.map { |path, match| Upload.new(path, fmt(uploads, match.captures), params, download) }
end
fmt(pattern, captures) click to toggle source
# File lib/dpl/providers/bintray.rb, line 169
def fmt(pattern, captures)
  captures.each.with_index.inject(pattern) do |pattern, (capture, ix)|
    pattern.gsub("$#{ix + 1}", capture)
  end
end
handle(req, res, opts = { raise: true }) click to toggle source
# File lib/dpl/providers/bintray.rb, line 234
def handle(req, res, opts = { raise: true })
  error :request_failed, req.method, req.uri, res.code if opts[:raise] && !success?(res.code)
  info :request_success, res.code, res.message, parse(res)['message'] unless opts[:silent]
  res.code.to_i
end
head(path, opts = {}) click to toggle source
# File lib/dpl/providers/bintray.rb, line 196
def head(path, opts = {})
  req = Net::HTTP::Head.new(path)
  req.basic_auth(user, key)
  request(req, opts)
end
http() click to toggle source
# File lib/dpl/providers/bintray.rb, line 224
def http
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http
end
install() click to toggle source
# File lib/dpl/providers/bintray.rb, line 65
def install
  require 'json'
end
only(hash, *keys) click to toggle source
# File lib/dpl/providers/bintray.rb, line 309
def only(hash, *keys)
  hash.select { |key, _| keys.include?(key) }
end
package() click to toggle source
# File lib/dpl/providers/bintray.rb, line 254
def package
  descriptor[:package]
end
package_attrs() click to toggle source
# File lib/dpl/providers/bintray.rb, line 262
def package_attrs
  package[:attributes]
end
package_exists?() click to toggle source
# File lib/dpl/providers/bintray.rb, line 82
def package_exists?
  exists?(:package)
end
package_name() click to toggle source
# File lib/dpl/providers/bintray.rb, line 258
def package_name
  package[:name]
end
parse(json) click to toggle source
# File lib/dpl/providers/bintray.rb, line 298
def parse(json)
  hash = JSON.parse(json)
  hash.is_a?(Hash) ? hash : {}
rescue StandardError
  {}
end
path(resource, args = {}) click to toggle source
# File lib/dpl/providers/bintray.rb, line 294
def path(resource, args = {})
  interpolate(PATHS[resource], args, secure: true)
end
path_for(str) click to toggle source
# File lib/dpl/providers/bintray.rb, line 179
def path_for(str)
  ix = str.index('(')
  path = ix.to_i.zero? ? str : str[0, ix]
  return path if File.exist?(path)

  warn(:missing_path, path: path)
  nil
end
post(path, body = nil) click to toggle source
# File lib/dpl/providers/bintray.rb, line 202
def post(path, body = nil)
  req = Net::HTTP::Post.new(path)
  req.add_field('Content-Type', 'application/json')
  req.basic_auth(user, key)
  req.body = JSON.dump(body) if body
  request(req)
end
publish_version() click to toggle source
# File lib/dpl/providers/bintray.rb, line 121
def publish_version
  info :publish_version
  post(path(:version_publish))
end
publish_version?() click to toggle source
# File lib/dpl/providers/bintray.rb, line 290
def publish_version?
  descriptor[:publish]
end
put(path, body, params = {}, headers = {}) click to toggle source
# File lib/dpl/providers/bintray.rb, line 210
def put(path, body, params = {}, headers = {})
  req = Net::HTTP::Put.new(append_params(path, params))
  headers.each { |key, value| req.add_field(key.to_s, value) }
  req.basic_auth(user, key)
  req.body = body
  request(req)
end
repo() click to toggle source
# File lib/dpl/providers/bintray.rb, line 270
def repo
  package[:repo]
end
request(req, opts = {}) click to toggle source
# File lib/dpl/providers/bintray.rb, line 218
def request(req, opts = {})
  res = http.request(req)
  handle(req, res, opts)
  res.code.to_i
end
retrying(opts) { || ... } click to toggle source
# File lib/dpl/providers/bintray.rb, line 141
def retrying(opts)
  1.upto(opts[:max]) do |count|
    code = yield
    return if code < 400

    info :retrying, opts.merge(count: count, code: code)
    sleep opts[:pause]
  end
  error :giveup_retries
end
sign_version() click to toggle source
# File lib/dpl/providers/bintray.rb, line 115
def sign_version
  body = compact(passphrase: passphrase)
  info :sign_version, (passphrase? ? 'with' : 'without')
  post(path(:version_sign), body)
end
sign_version?() click to toggle source
# File lib/dpl/providers/bintray.rb, line 286
def sign_version?
  version[:gpgSign]
end
subject() click to toggle source
# File lib/dpl/providers/bintray.rb, line 266
def subject
  package[:subject]
end
success?(code) click to toggle source
# File lib/dpl/providers/bintray.rb, line 240
def success?(code)
  code.to_s[0].to_i == 2
end
update_file(file) click to toggle source
# File lib/dpl/providers/bintray.rb, line 133
def update_file(file)
  retrying(max: 10, pause: 5) do
    body = { list_in_downloads: file.download }.to_json
    headers = { 'Content-Type': 'application/json' }
    put(path(:file_metadata, target: file.target), body, {}, headers)
  end
end
update_files() click to toggle source
# File lib/dpl/providers/bintray.rb, line 126
def update_files
  files.select(&:download).each do |file|
    info :list_download, path: file.target
    update_file(file)
  end
end
upload_files() click to toggle source
# File lib/dpl/providers/bintray.rb, line 108
def upload_files
  files.each do |file|
    info :upload_file, source: file.source, target: file.target
    put(path(:version_file, target: file.target), file.read, file.params)
  end
end
url() click to toggle source
Calls superclass method
# File lib/dpl/providers/bintray.rb, line 250
def url
  @url ||= URI.parse(super || URL)
end
validate() click to toggle source
# File lib/dpl/providers/bintray.rb, line 69
def validate
  error :missing_file unless File.exist?(file)
  # validate that the repo exists, and we have access
end
version() click to toggle source
# File lib/dpl/providers/bintray.rb, line 274
def version
  descriptor[:version]
end
version_attrs() click to toggle source
# File lib/dpl/providers/bintray.rb, line 282
def version_attrs
  version[:attributes]
end
version_exists?() click to toggle source
# File lib/dpl/providers/bintray.rb, line 95
def version_exists?
  exists?(:version)
end
version_name() click to toggle source
# File lib/dpl/providers/bintray.rb, line 278
def version_name
  version[:name]
end