Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion supply/lib/supply/client.rb
Original file line number Diff line number Diff line change
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
50 changes: 37 additions & 13 deletions supply/spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,47 @@
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.")
expect(UI).to receive(:user_error!).with("Google Api Error: Invalid request - Ensure project settings are enabled.").once

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')
client.upload_image(image_path: fixture_file("playstore-icon.png"),
image_type: "icon",
language: "en-US")
client.begin_edit(package_name: 'test-app')
client.upload_image(image_path: fixture_file("playstore-icon.png"),
image_type: "icon",
language: "en-US")
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
expect(UI).to receive(:user_error!).with("Google Api Error: Invalid request - Ensure project settings are enabled.").once
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that I got bitten by in the past is that when you stub UI.user_error! with expect or allow, that means that the original method won't be called… which means that it won't raise.

As a result, the caller of call_google_api, in your case client.upload_image, won't be interrupted by the Fastlane::Error raised by user_error! in the context of running tests, while it will in the real world.

Depending on the implementation of client.upload_image(…), that could have side effects if that implementation counts on the fact that call_google_api can raise to stop early and not run the rest of the code of that method on error, as that would mean that the fact that we stubbed user_error! would make the method under test behave differently in test environment vs in reality.

I think the easiest way to solve this would be to replace your expect(UI).to receive(:user_error!) with wrapping your call to client.upload_image within expect { … }.to raise_error(Fastlane::Error, "Google Api Error: Invalid request – Ensure project settings are enabled.". That way you'd let user_error! raise and interrupt the code of upload_image mid-air even in the context of testing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AliSoftware That expect on UI.user_error! was already there 🤷 But... would be adding .and_call_original work? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well that would, except that I think rubocop might suggest you to only call .and_call_original on allow(…), not on expect(…)

So you could allow(UI).to receive(:user_error!).with(…).and_call_original… but if you do that and even if you wanted to still keep the expect(UI).to receive(:user_error!).once to assert on it only being called once and not more, you'll still need to wrap your client.upload_image within expect { … }.to raise_error(…) to avoid the test from crashing on the raised error and instead catch (and assert) it.

So at this point since you'll still need the expect { … } .to raise_error, I'm not sure that and_call_original adds much value, instead of not stubbing UI.user_error! at all?


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')
client.upload_image(image_path: fixture_file("playstore-icon.png"),
image_type: "icon",
language: "en-US")
end
end

describe "AndroidPublisher" do
Expand Down