class Fastlane::Actions::XcodeServerGetAssetsAction::XcodeServer

Public Class Methods

new(host, username, password) click to toggle source
# File fastlane/lib/fastlane/actions/xcode_server_get_assets.rb, line 109
def initialize(host, username, password)
  @host = host.start_with?('https://') ? host : "https://#{host}"
  @username = username
  @password = password
end

Public Instance Methods

fetch_all_bots() click to toggle source
# File fastlane/lib/fastlane/actions/xcode_server_get_assets.rb, line 115
def fetch_all_bots
  response = get_endpoint('/bots')
  UI.user_error!("You are unauthorized to access data on #{@host}, please check that you're passing in a correct username and password.") if response.status == 401
  UI.user_error!("Failed to fetch Bots from Xcode Server at #{@host}, response: #{response.status}: #{response.body}.") if response.status != 200
  JSON.parse(response.body)['results']
end
fetch_assets(integration_id, target_folder, action) click to toggle source
# File fastlane/lib/fastlane/actions/xcode_server_get_assets.rb, line 128
def fetch_assets(integration_id, target_folder, action)
  # create a temp folder and a file, stream the download into it
  Dir.mktmpdir do |dir|
    temp_file = File.join(dir, "tmp_download.#{rand(1_000_000)}")
    f = open(temp_file, 'w')
    streamer = lambda do |chunk, remaining_bytes, total_bytes|
      if remaining_bytes && total_bytes
        UI.important("Downloading: #{100 - (100 * remaining_bytes.to_f / total_bytes.to_f).to_i}%")
      else
        UI.error(chunk.to_s)
      end
      f.write(chunk)
    end

    response = self.get_endpoint("/integrations/#{integration_id}/assets", streamer)
    f.close

    UI.user_error!("Integration doesn't have any assets (it probably never ran).") if response.status == 500
    UI.user_error!("Failed to fetch Assets zip for Integration #{integration_id} from Xcode Server at #{@host}, response: #{response.status}: #{response.body}") if response.status != 200

    # unzip it, it's a .tar.gz file
    out_folder = File.join(dir, "out_#{rand(1_000_000)}")
    FileUtils.mkdir_p(out_folder)

    action.sh("cd \"#{out_folder}\"; cat \"#{temp_file}\" | gzip -d | tar -x")

    # then pull the real name from headers
    asset_filename = response.headers['Content-Disposition'].split(';')[1].split('=')[1].delete('"')
    asset_foldername = asset_filename.split('.')[0]

    # rename the folder in out_folder to asset_foldername
    found_folder = Dir.entries(out_folder).select { |item| item != '.' && item != '..' }[0]

    UI.user_error!("Internal error, couldn't find unzipped folder") if found_folder.nil?

    unzipped_folder_temp_name = File.join(out_folder, found_folder)
    unzipped_folder = File.join(out_folder, asset_foldername)

    # rename to destination name
    FileUtils.mv(unzipped_folder_temp_name, unzipped_folder)

    target_folder = File.absolute_path(target_folder)

    # create target folder if it doesn't exist
    FileUtils.mkdir_p(target_folder)

    # and move+rename it to the destination place
    FileUtils.cp_r(unzipped_folder, target_folder)
    out = File.join(target_folder, asset_foldername)
    return out
  end
  return nil
end
fetch_integrations(bot_id) click to toggle source
# File fastlane/lib/fastlane/actions/xcode_server_get_assets.rb, line 122
def fetch_integrations(bot_id)
  response = get_endpoint("/bots/#{bot_id}/integrations?last=10")
  UI.user_error!("Failed to fetch Integrations for Bot #{bot_id} from Xcode Server at #{@host}, response: #{response.status}: #{response.body}") if response.status != 200
  JSON.parse(response.body)['results']
end
get_endpoint(endpoint, response_block = nil) click to toggle source
# File fastlane/lib/fastlane/actions/xcode_server_get_assets.rb, line 197
def get_endpoint(endpoint, response_block = nil)
  url = url_for_endpoint(endpoint)
  headers = self.headers || {}

  if response_block
    response = Excon.get(url, response_block: response_block, headers: headers)
  else
    response = Excon.get(url, headers: headers)
  end

  return response
end
headers() click to toggle source
# File fastlane/lib/fastlane/actions/xcode_server_get_assets.rb, line 182
def headers
  require 'base64'
  headers = {
    'User-Agent' => 'fastlane-xcode_server_get_assets', # XCS wants user agent. for some API calls. not for others. sigh.
    'X-XCSAPIVersion' => 1 # XCS API version with this API, Xcode needs this otherwise it explodes in a 500 error fire. Currently Xcode 7 Beta 5 is on Version 5.
  }

  if @username && @password
    userpass = "#{@username}:#{@password}"
    headers['Authorization'] = "Basic #{Base64.strict_encode64(userpass)}"
  end

  return headers
end

Private Instance Methods

url_for_endpoint(endpoint) click to toggle source
# File fastlane/lib/fastlane/actions/xcode_server_get_assets.rb, line 212
def url_for_endpoint(endpoint)
  "#{@host}:20343/api#{endpoint}"
end