Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions lib/fastlane/plugin/emerge/helper/emerge_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def initialize(sha:, base_sha:, previous_sha:, branch:, pr_number: nil, repo_nam
module Helper
class EmergeHelper
API_URL = 'https://api.emergetools.com/upload'.freeze
NETWORK_TIMEOUT = 450 # seconds, slightly under backend's 480s
OPEN_TIMEOUT = 30 # seconds for connection establishment
MAX_UPLOAD_RETRIES = 2
RETRY_BASE_DELAY = 5 # seconds

def self.perform_upload(api_token, params, file_path)
cleaned_params = clean_params(params)
Expand Down Expand Up @@ -70,6 +74,13 @@ def self.copy_config(config_path, tmp_dir)

private_class_method

def self.faraday_connection
Faraday.new do |f|
f.options.timeout = NETWORK_TIMEOUT
f.options.open_timeout = OPEN_TIMEOUT
end
end

def self.clean_params(params)
params.reject { |_, v| v.nil? }
end
Expand All @@ -83,7 +94,7 @@ def self.print_summary(params)
end

def self.create_upload(api_token, params)
response = Faraday.post(API_URL, params.to_json, headers(api_token, params, 'application/json'))
response = faraday_connection.post(API_URL, params.to_json, headers(api_token, params, 'application/json'))
parse_response(response)
end

Expand Down Expand Up @@ -124,13 +135,26 @@ def self.handle_upload_response(api_token, response, file_path)
end

def self.upload_file(api_token, upload_url, file_path)
response = Faraday.put(upload_url) do |req|
req.headers = headers(api_token, nil, 'application/zip')
req.headers['Content-Length'] = File.size(file_path).to_s
req.body = Faraday::UploadIO.new(file_path, 'application/zip')
retries = 0

begin
response = faraday_connection.put(upload_url) do |req|
req.headers = headers(api_token, nil, 'application/zip')
req.headers['Content-Length'] = File.size(file_path).to_s
req.body = Faraday::UploadIO.new(file_path, 'application/zip')
end

raise "Uploading zip file failed #{response.status}" unless response.status == 200
rescue Faraday::Error, Errno::ECONNRESET, Errno::ETIMEDOUT => e
retries += 1
if retries <= MAX_UPLOAD_RETRIES
delay = RETRY_BASE_DELAY * retries
UI.message("Upload failed (#{e.class}), retrying #{retries}/#{MAX_UPLOAD_RETRIES} in #{delay}s...")
sleep(delay)
retry
end
raise
end

raise "Uploading zip file failed #{response.status}" unless response.status == 200
end
end
end
Expand Down