class DPL::Provider::Bintray

Attributes

descriptor[R]
dry_run[R]
file[R]
key[R]
passphrase[R]
test_mode[RW]
url[R]
user[R]

Public Class Methods

new(*args) click to toggle source
Calls superclass method DPL::Provider::new
# File lib/dpl/provider/bintray.rb, line 25
def initialize(*args)
  super(*args)
  @test_mode = false
  @user = options[:user]
  @key = options[:key]
  @url = options[:url]
  @file = options[:file]
  @passphrase = options[:passphrase]
  @dry_run = options[:dry_run]

  if @user.nil?
    abort("The 'user' argument is required")
  end
  if @key.nil?
    abort("The 'key' argument is required")
  end
  if @file.nil?
    abort("The 'file' argument is required")
  end
  if @url.nil?
    @url = 'https://api.bintray.com'
  end
  if @dry_run.nil?
    @dry_run = false
  end
end

Public Instance Methods

add_if_matches(map, path, include_pattern, exclude_pattern, upload_pattern, matrix_params) click to toggle source
# File lib/dpl/provider/bintray.rb, line 401
def add_if_matches(map, path, include_pattern, exclude_pattern, upload_pattern, matrix_params)
  res = path.match(/#{include_pattern}/)

  # If the file matches the include pattern and it is not a directory.
  # In case test_mode is set, we do not check if the file exists.
  if !res.nil? && (test_mode || File.file?(path))
    # If the file does not match the exclude pattern.
    if exclude_pattern.nil? || exclude_pattern.empty? || !path.match(/#{exclude_pattern}/)
      # Using the capturing groups in the include pattern, replace the $1, $2, ...
      # in the upload pattern.
      groups = res.captures
      replaced_upload_pattern = upload_pattern
      for i in 0..groups.size-1
        replaced_upload_pattern = replaced_upload_pattern.gsub("$#{i+1}", groups[i])
      end
      map[path] = Artifact.new(path, replaced_upload_pattern, matrix_params)
    end
  end
end
add_package_attributes() click to toggle source
# File lib/dpl/provider/bintray.rb, line 225
def add_package_attributes
  package = descriptor["package"]
  repo = package["repo"]
  subject = package["subject"]
  package_name = package["name"]
  attributes = package["attributes"]
  path = nil
  if !attributes.nil?
    log "Adding attributes for package '#{package_name}'..."
    path = "/packages/#{subject}/#{repo}/#{package_name}/attributes"
    if !dry_run
      res = post_request(path, attributes)
      log_bintray_response(res)
    end
  end
  RequestDetails.new(path, attributes)
end
add_to_map(to_map, from_map, key) click to toggle source

Copies a key from one map to another, if the key exists there.

# File lib/dpl/provider/bintray.rb, line 452
def add_to_map(to_map, from_map, key)
  if !from_map[key].nil?
    to_map[key] = from_map[key]
  end
end
add_version_attributes() click to toggle source
# File lib/dpl/provider/bintray.rb, line 279
def add_version_attributes
  package = descriptor["package"]
  package_name = package["name"]
  subject = package["subject"]
  version = descriptor["version"]
  version_name = version["name"]
  repo = package["repo"]
  attributes = version["attributes"]
  path = nil
  if !attributes.nil?
    log "Adding attributes for version '#{version_name}'..."
    path = "/packages/#{subject}/#{repo}/#{package_name}/versions/#{version_name}/attributes"
    if !dry_run
      res = post_request(path, attributes)
      log_bintray_response(res)
    end
  end
  RequestDetails.new(path, attributes)
end
check_and_create_package() click to toggle source
# File lib/dpl/provider/bintray.rb, line 299
def check_and_create_package
  if !package_exists?
    create_package
  end
end
check_and_create_version() click to toggle source
# File lib/dpl/provider/bintray.rb, line 305
def check_and_create_version
  if !version_exists?
    create_version
  end
end
check_auth() click to toggle source
# File lib/dpl/provider/bintray.rb, line 9
def check_auth
end
create_package() click to toggle source
# File lib/dpl/provider/bintray.rb, line 189
def create_package
  package = descriptor["package"]
  repo = package["repo"]
  body = {}

  add_to_map(body, package, "name")
  add_to_map(body, package, "desc")
  add_to_map(body, package, "licenses")
  add_to_map(body, package, "labels")
  add_to_map(body, package, "vcs_url")
  add_to_map(body, package, "website_url")
  add_to_map(body, package, "issue_tracker_url")
  add_to_map(body, package, "public_download_numbers")
  add_to_map(body, package, "public_stats")

  subject = package["subject"]
  package_name = package["name"]
  log "Creating package '#{package_name}'..."

  path = "/packages/#{subject}/#{repo}"
  if !dry_run
    res = post_request(path, body)
    log_bintray_response(res)
    code = res.code.to_i
  else
    code = 200
  end

  if !test_mode
    if code == 201 || code == 200
      add_package_attributes
    end
  end
  RequestDetails.new(path, body)
end
create_version() click to toggle source
# File lib/dpl/provider/bintray.rb, line 243
def create_version
  package = descriptor["package"]
  version = descriptor["version"]
  repo = package["repo"]
  body = {}

  add_to_map(body, version, "name")
  add_to_map(body, version, "desc")
  add_to_map(body, version, "released")
  add_to_map(body, version, "vcs_tag")
  add_to_map(body, version, "github_release_notes_file")
  add_to_map(body, version, "github_use_tag_release_notes")
  add_to_map(body, version, "attributes")

  package_name = package["name"]
  subject = package["subject"]
  version_name = version["name"]
  log "Creating version '#{version_name}'..."

  path = "/packages/#{subject}/#{repo}/#{package_name}/versions"
  if !dry_run
    res = post_request(path, body)
    log_bintray_response(res)
    code = res.code.to_i
  else
    code = 200
  end

  if !test_mode
    if code == 201 || code == 200
      add_version_attributes
    end
  end
  RequestDetails.new(path, body)
end
deploy() click to toggle source
# File lib/dpl/provider/bintray.rb, line 442
def deploy
  read_descriptor
  check_and_create_package
  check_and_create_version
  upload_files
  gpg_sign_version
  publish_version
end
descriptor=(json) click to toggle source
# File lib/dpl/provider/bintray.rb, line 57
def descriptor=(json)
  @descriptor = JSON.parse(json)
end
files_to_upload() click to toggle source

Returns a map containing Artifact objects. The map contains the files to be uploaded to Bintray.

# File lib/dpl/provider/bintray.rb, line 423
def files_to_upload
  upload_files = Hash.new()
  files = descriptor["files"]
  if files.nil?
    return upload_files
  end

  files.each { |patterns|
    fill_files_map(
        upload_files,
        patterns["includePattern"],
        patterns["excludePattern"],
        patterns["uploadPattern"],
        patterns["matrixParams"])
  }

  return upload_files
end
fill_files_map(map, include_pattern, exclude_pattern, upload_pattern, matrix_params) click to toggle source

Fills a map with Artifact objects which match the include pattern and do not match the exclude pattern. The artifacts are files collected from the file system.

# File lib/dpl/provider/bintray.rb, line 388
def fill_files_map(map, include_pattern, exclude_pattern, upload_pattern, matrix_params)
  # Get the root path from which to start collecting the files.
  root_path = root_path(include_pattern)
  if root_path.nil?
    return
  end

  # Start scanning the root path recursively.
  Find.find(root_path) do |path|
    add_if_matches(map, path, include_pattern, exclude_pattern, upload_pattern, matrix_params)
  end
end
gpg_sign_version() click to toggle source
# File lib/dpl/provider/bintray.rb, line 339
def gpg_sign_version
  version = descriptor["version"]
  gpg_sign = version["gpgSign"]
  if gpg_sign
    package = descriptor["package"]
    repo = package["repo"]
    package_name = package["name"]
    subject = package["subject"]
    version_name = version["name"]

    body = nil
    if !passphrase.nil?
      log "Signing version with no passphrase..."
      body = {}
      body["passphrase"] = passphrase
    else
      log "Signing version with passphrase..."
    end

    path = "/gpg/#{subject}/#{repo}/#{package_name}/versions/#{version_name}"
    if !dry_run
      res = post_request(path, body)
      log_bintray_response(res)
    end
    RequestDetails.new(path, body)
  end
end
head_request(path) click to toggle source
# File lib/dpl/provider/bintray.rb, line 61
def head_request(path)
  url = URI.parse(self.url)
  req = Net::HTTP::Head.new(path)
  req.basic_auth user, key

  sock = Net::HTTP.new(url.host, url.port)
  sock.use_ssl = true
  res = sock.start {|http| http.request(req) }

  return res
end
log(msg) click to toggle source
# File lib/dpl/provider/bintray.rb, line 471
def log(msg)
  puts "[Bintray Upload] #{msg}"
end
log_bintray_response(res) click to toggle source
# File lib/dpl/provider/bintray.rb, line 458
def log_bintray_response(res)
  msg = ''
  if !res.body.nil?
    begin
      response = JSON.parse(res.body)
      msg = response["message"]
    rescue
    end
  end

  log "Bintray response: #{res.code.to_i} #{res.message}. #{msg}"
end
needs_key?() click to toggle source
# File lib/dpl/provider/bintray.rb, line 12
def needs_key?
  false
end
package_exists?() click to toggle source
# File lib/dpl/provider/bintray.rb, line 138
def package_exists?
  path = package_exists_path
  if !dry_run
    res = head_request(path)
    code = res.code.to_i
  else
    code = 404
  end

  if code == 404
    return false
  end
  if code == 201 || code == 200
    return true
  end
  name = descriptor["package"]["name"]
  abort("Unexpected HTTP response code #{code} returned from Bintray while checking if package '#{name}' exists. " +
            "Response message: #{res.message}")
end
package_exists_path() click to toggle source
# File lib/dpl/provider/bintray.rb, line 130
def package_exists_path
  package = descriptor["package"]
  subject = package["subject"]
  name = package["name"]
  repo = package["repo"]
  return "/packages/#{subject}/#{repo}/#{name}"
end
post_request(path, body) click to toggle source
# File lib/dpl/provider/bintray.rb, line 73
def post_request(path, body)
  req = Net::HTTP::Post.new(path)
  req.add_field('Content-Type', 'application/json')
  req.basic_auth user, key
  if !body.nil?
    req.body = body.to_json
  end

  url = URI.parse(self.url)
  sock = Net::HTTP.new(url.host, url.port)
  sock.use_ssl = true
  res = sock.start {|http| http.request(req) }
  return res
end
publish_version() click to toggle source
# File lib/dpl/provider/bintray.rb, line 319
def publish_version
  publish = descriptor["publish"]
  if publish
    package = descriptor["package"]
    version = descriptor["version"]
    repo = package["repo"]
    package_name = package["name"]
    subject = package["subject"]
    version_name = version["name"]

    log "Publishing version '#{version_name}' of package '#{package_name}'..."
    path = "/content/#{subject}/#{repo}/#{package_name}/#{version_name}/publish"
    if !dry_run
      res = post_request(path, nil)
      log_bintray_response(res)
    end
  end
  RequestDetails.new(path, nil)
end
put_file_request(local_file_path, upload_path, matrix_params) click to toggle source
# File lib/dpl/provider/bintray.rb, line 88
def put_file_request(local_file_path, upload_path, matrix_params)
  url = URI.parse(self.url)

  file = File.open(local_file_path, 'rb')
  data = file.read()
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true

  params = ''
  if !matrix_params.nil?
    matrix_params.each do |key, val|
      params << ";#{key}=#{val}"
    end
    upload_path << params
  end

  request = Net::HTTP::Put.new("#{upload_path}")
  request.basic_auth user, key
  request.body = data

  return http.request(request)
end
read_descriptor() click to toggle source
# File lib/dpl/provider/bintray.rb, line 52
def read_descriptor
  log "Reading descriptor file: #{file}"
  @descriptor = JSON.parse(File.read(file))
end
root_path(str) click to toggle source

Get the root path from which to start collecting files to be uploaded to Bintray.

# File lib/dpl/provider/bintray.rb, line 369
def root_path(str)
  index = str.index('(')
  path = nil
  if index.nil? || str.start_with?('(')
    path = str
  else
    path = str[0, index]
  end

  if !test_mode && !File.exist?(path)
    log "Warning: Path: #{path} does not exist."
    return nil
  end
  return path
end
upload_file(artifact) click to toggle source
# File lib/dpl/provider/bintray.rb, line 111
def upload_file(artifact)
  log "Uploading file '#{artifact.local_path}' to #{artifact.upload_path}"

  if dry_run
    return
  end

  package = descriptor["package"]
  version = descriptor["version"]
  package_name = package["name"]
  subject = package["subject"]
  repo = package["repo"]
  version_name = version["name"]

  path = "/content/#{subject}/#{repo}/#{package_name}/#{version_name}/#{artifact.upload_path}"
  res = put_file_request(artifact.local_path, path, artifact.matrix_params)
  log_bintray_response(res)
end
upload_files() click to toggle source
# File lib/dpl/provider/bintray.rb, line 311
def upload_files
  files = files_to_upload

  files.each do |key, artifact|
    upload_file(artifact)
  end
end
version_exists?() click to toggle source
# File lib/dpl/provider/bintray.rb, line 169
def version_exists?
  path = version_exists_path
  if !dry_run
    res = head_request(path)
    code = res.code.to_i
  else
    code = 404
  end

  if code == 404
    return false
  end
  if code == 201 || code == 200
    return true
  end
  version_name = descriptor["version"]["name"]
  abort("Unexpected HTTP response code #{code} returned from Bintray while checking if version '#{version_name}' exists. " +
            "Response message: #{res.message}")
end
version_exists_path() click to toggle source
# File lib/dpl/provider/bintray.rb, line 158
def version_exists_path
  package = descriptor["package"]
  version = descriptor["version"]
  package_name = package["name"]
  subject = package["subject"]
  repo = package["repo"]
  version_name = version["name"]

  return "/packages/#{subject}/#{repo}/#{package_name}/versions/#{version_name}"
end