Skip to content

Commit

Permalink
[supply] add new SUPPLY_UPLOAD_MAX_RETRIES env var to attempt to solv…
Browse files Browse the repository at this point in the history
…e failed Google API calls (#21518)

* [supply] add new SUPPLY_UPLOAD_MAX_RETRIES env var to attempt to solve failed Google API calls

* Rewrite expectation to ensure SUT is interrupted by raised error

Otherwise if we just `expect(UI).to receive(:user_error!).with(…)` that will mock the `UI.user_error` call in the method being tested... and thus not make it `raise` anymore, which the rest of the implementation of that method will continue executing when run in the context of mocked tests while it would not in the real world, which means we would be testing a different behavior of the method

---------

Co-authored-by: Olivier Halligon <olivier@halligon.net>
  • Loading branch information
joshdholtz and AliSoftware committed Sep 17, 2023
1 parent 271f2d5 commit 3bbbebd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
9 changes: 8 additions & 1 deletion supply/lib/supply/client.rb
Expand Up @@ -78,6 +78,7 @@ def initialize(service_account_json: nil, params: nil)
private

def call_google_api
tries_left ||= (ENV["SUPPLY_UPLOAD_MAX_RETRIES"] || 0).to_i
yield if block_given?
rescue Google::Apis::Error => e
error = begin
Expand All @@ -92,7 +93,13 @@ def call_google_api
message = e.body
end

UI.user_error!("Google Api Error: #{e.message} - #{message}")
if tries_left.positive?
UI.error("Google Api Error: #{e.message} - #{message} - Retrying...")
tries_left -= 1
retry
else
UI.user_error!("Google Api Error: #{e.message} - #{message}")
end
end
end

Expand Down
51 changes: 38 additions & 13 deletions supply/spec/client_spec.rb
Expand Up @@ -8,23 +8,48 @@
to_return(status: 200, body: '{}', headers: { 'Content-Type' => 'application/json' })
end

it "displays error messages from the API" do
stub_request(:post, "https://androidpublisher.googleapis.com/upload/androidpublisher/v3/applications/test-app/edits/1/listings/en-US/icon").
to_return(status: 403, body: '{"error":{"message":"Ensure project settings are enabled."}}', headers: { 'Content-Type' => 'application/json' })
describe "displays error messages from the API" do
it "with no retries" do
stub_request(:post, "https://androidpublisher.googleapis.com/upload/androidpublisher/v3/applications/test-app/edits/1/listings/en-US/icon").
to_return(status: 403, body: '{"error":{"message":"Ensure project settings are enabled."}}', headers: { 'Content-Type' => 'application/json' })

expect(UI).to receive(:user_error!).with("Google Api Error: Invalid request - Ensure project settings are enabled.")
current_edit = double
allow(current_edit).to receive(:id).and_return(1)

current_edit = double
allow(current_edit).to receive(:id).and_return(1)
client = Supply::Client.new(service_account_json: StringIO.new(service_account_file), params: { timeout: 1 })
allow(client).to receive(:ensure_active_edit!)
allow(client).to receive(:current_edit).and_return(current_edit)

client = Supply::Client.new(service_account_json: StringIO.new(service_account_file), params: { timeout: 1 })
allow(client).to receive(:ensure_active_edit!)
allow(client).to receive(:current_edit).and_return(current_edit)
client.begin_edit(package_name: 'test-app')
expect {
client.upload_image(image_path: fixture_file("playstore-icon.png"),
image_type: "icon",
language: "en-US")
}.to raise_error(FastlaneCore::Interface::FastlaneError, "Google Api Error: Invalid request - Ensure project settings are enabled.")
end

it "with 5 retries" do
stub_const("ENV", { 'SUPPLY_UPLOAD_MAX_RETRIES' => 5 })

stub_request(:post, "https://androidpublisher.googleapis.com/upload/androidpublisher/v3/applications/test-app/edits/1/listings/en-US/icon").
to_return(status: 403, body: '{"error":{"message":"Ensure project settings are enabled."}}', headers: { 'Content-Type' => 'application/json' })

expect(UI).to receive(:error).with("Google Api Error: Invalid request - Ensure project settings are enabled. - Retrying...").exactly(5).times

client.begin_edit(package_name: 'test-app')
client.upload_image(image_path: fixture_file("playstore-icon.png"),
image_type: "icon",
language: "en-US")
current_edit = double
allow(current_edit).to receive(:id).and_return(1)

client = Supply::Client.new(service_account_json: StringIO.new(service_account_file), params: { timeout: 1 })
allow(client).to receive(:ensure_active_edit!)
allow(client).to receive(:current_edit).and_return(current_edit)

client.begin_edit(package_name: 'test-app')
expect {
client.upload_image(image_path: fixture_file("playstore-icon.png"),
image_type: "icon",
language: "en-US")
}.to raise_error(FastlaneCore::Interface::FastlaneError, "Google Api Error: Invalid request - Ensure project settings are enabled.")
end
end

describe "AndroidPublisher" do
Expand Down

0 comments on commit 3bbbebd

Please sign in to comment.