Skip to content

Commit

Permalink
- Add option to set number of retries after fail get image from shiel…
Browse files Browse the repository at this point in the history
…ds.io

- Fix problem when shields.io respond status is 200 and body is HTML (Server error occures)
  • Loading branch information
rafalwojcik committed Aug 21, 2017
1 parent 385ad14 commit 5a3b379
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
6 changes: 5 additions & 1 deletion lib/badge/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def self.available_options
type: Integer,
optional: true),

FastlaneCore::ConfigItem.new(key: :shield_io_retry_count,
description: "The number of retries if shield.io error occures. Default: 10",
type: Integer,
optional: true),

FastlaneCore::ConfigItem.new(key: :shield_geometry,
description: "Position of shield on icon, relative to gravity e.g, +50+10%",
optional: true),
Expand All @@ -65,4 +70,3 @@ def self.available_options
end
end
end

25 changes: 15 additions & 10 deletions lib/badge/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module Badge
class Runner
@@retry_count = Badge.shield_io_retries
@@retry_attemps = 0
@@rsvg_enabled = true

def run(path, options)
Expand All @@ -26,28 +26,32 @@ def run(path, options)
UI.message "Start adding badges...".green

shield = nil
response_error = false
begin
timeout = Badge.shield_io_timeout
timeout = options[:shield_io_timeout] if options[:shield_io_timeout]
Timeout.timeout(timeout.to_i) do
shield = load_shield(options[:shield]) if options[:shield]
end
rescue Timeout::Error
UI.error "Error loading image from shield.io timeout reached. Skipping Shield. Use --verbose for more info".red
UI.error "Error loading image from shield.io timeout reached. Use --verbose for more info".red
rescue Curl::Err::CurlError => error
response = error.io
UI.error "Error loading image from shield.io response Error. Skipping Shield. Use --verbose for more info".red
UI.error "Error loading image from shield.io response Error. Use --verbose for more info".red
UI.verbose response.status if FastlaneCore::Globals.verbose?
response_error = true
rescue MiniMagick::Invalid
UI.error "Error validating image from shield.io. Use --verbose for more info".red
rescue Exception => error
UI.error "Other error occured. Use --verbose for more info".red
UI.verbose error if FastlaneCore::Globals.verbose?
end

if @@retry_count <= 0
retry_limit = options[:shield_io_retry_count] || Badge.shield_io_retries
if @@retry_attemps >= retry_limit
UI.error "Cannot load image from shield.io skipping it...".red
elsif response_error
UI.message "Waiting for #{timeout.to_i}s and retry to load image from shield.io tries remaining: #{@@retry_count}".red
else
UI.message "Waiting for #{timeout.to_i}s and retry to load image from shield.io tries remaining: #{retry_limit - @@retry_attemps}".red
sleep timeout.to_i
@@retry_count -= 1
@@retry_attemps += 1
return run(path, options)
end

Expand Down Expand Up @@ -121,7 +125,8 @@ def load_shield(shield_string)
UI.verbose "Trying to load image from shield.io. Timeout: #{Badge.shield_io_timeout}s".blue
UI.verbose "URL: #{url}".blue

Curl::Easy.download(url, file_name)
#Curl::Easy.download(url, file_name)
MiniMagick::Image.open(file_name)

File.open(file_name)
end
Expand Down

0 comments on commit 5a3b379

Please sign in to comment.