Skip to content

Commit bce34b1

Browse files
committed
Allow upload_to_s3 not to crash if file exists
1 parent 15c5b99 commit bce34b1

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/fastlane/plugin/wpmreleasetoolkit/actions/common/upload_to_s3.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ def self.run(params)
2020
key = [file_name_hash, key].join('/')
2121
end
2222

23-
UI.user_error!("File already exists in S3 bucket #{bucket} at #{key}") if file_is_already_uploaded?(bucket, key)
23+
if file_is_already_uploaded?(bucket, key)
24+
message = "File already exists in S3 bucket #{bucket} at #{key}"
25+
if params[:skip_if_exists]
26+
UI.important("#{message}. Skipping upload.")
27+
else
28+
UI.user_error!(message)
29+
end
30+
return
31+
end
2432

2533
UI.message("Uploading #{file_path} to: #{key}")
2634

@@ -101,6 +109,13 @@ def self.available_options
101109
default_value: true,
102110
type: Boolean
103111
),
112+
FastlaneCore::ConfigItem.new(
113+
key: :skip_if_exists,
114+
description: 'If the file already exists in the S3 bucket, just log the error instead of failing with `user_error!`',
115+
optional: true,
116+
default_value: false,
117+
type: Boolean
118+
),
104119
]
105120
end
106121

spec/upload_to_s3_spec.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def stub_s3_response_for_file(key, exists: true)
144144
end.to raise_error(FastlaneCore::Interface::FastlaneError, 'Path `this-file-does-not-exist.txt` does not exist.')
145145
end
146146

147-
it 'fails if the file already exists on S3' do
147+
it 'fails if the file already exists on S3 and skip_if_exists:false' do
148148
expected_key = 'a62f2225bf70bfaccbc7f1ef2a397836717377de/key'
149149
stub_s3_response_for_file(expected_key)
150150

@@ -158,5 +158,23 @@ def stub_s3_response_for_file(key, exists: true)
158158
end.to raise_error(FastlaneCore::Interface::FastlaneError, "File already exists in S3 bucket #{test_bucket} at #{expected_key}")
159159
end
160160
end
161+
162+
it 'just logs if the file already exists on S3 and skip_if_exists:true' do
163+
expected_key = 'a62f2225bf70bfaccbc7f1ef2a397836717377de/key'
164+
stub_s3_response_for_file(expected_key)
165+
166+
warnings = []
167+
allow(FastlaneCore::UI).to receive(:important) { |message| warnings << message }
168+
169+
with_tmp_file(named: 'key') do |file_path|
170+
run_described_fastlane_action(
171+
bucket: test_bucket,
172+
key: 'key',
173+
file: file_path,
174+
skip_if_exists: true
175+
)
176+
expect(warnings).to eq ["File already exists in S3 bucket #{test_bucket} at #{expected_key}. Skipping upload."]
177+
end
178+
end
161179
end
162180
end

0 commit comments

Comments
 (0)