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 all commits
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
51 changes: 38 additions & 13 deletions supply/spec/client_spec.rb
Original file line number Diff line number Diff line change
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