Skip to content

Commit 80ac4b6

Browse files
authored
Merge pull request #427 from wordpress-mobile/upload_to_s3-skip_if_exists
Allow `upload_to_s3` not to crash if file exists
2 parents 15c5b99 + dd723fa commit 80ac4b6

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
### New Features
1414

15-
_None_
15+
- Allow `upload_to_s3` action to just log instead of crash (using new `skip_if_exists` parameter) when the file already exists in the S3 bucket. [#427]
1616

1717
### Bug Fixes
1818

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+
return key
28+
else
29+
UI.user_error!(message)
30+
end
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, skip the upload (and report it in the logs), 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: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,39 @@ 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
148-
expected_key = 'a62f2225bf70bfaccbc7f1ef2a397836717377de/key'
149-
stub_s3_response_for_file(expected_key)
147+
context 'when the file already exists on S3' do
148+
it 'fails if skip_if_exists:false' do
149+
expected_key = 'a62f2225bf70bfaccbc7f1ef2a397836717377de/key'
150+
stub_s3_response_for_file(expected_key)
151+
152+
with_tmp_file(named: 'key') do |file_path|
153+
expect do
154+
run_described_fastlane_action(
155+
bucket: test_bucket,
156+
key: 'key',
157+
file: file_path
158+
)
159+
end.to raise_error(FastlaneCore::Interface::FastlaneError, "File already exists in S3 bucket #{test_bucket} at #{expected_key}")
160+
end
161+
end
150162

151-
with_tmp_file(named: 'key') do |file_path|
152-
expect do
153-
run_described_fastlane_action(
163+
it 'logs a message without failing if skip_if_exists:true' do
164+
expected_key = 'a62f2225bf70bfaccbc7f1ef2a397836717377de/key'
165+
stub_s3_response_for_file(expected_key)
166+
167+
warnings = []
168+
allow(FastlaneCore::UI).to receive(:important) { |message| warnings << message }
169+
170+
with_tmp_file(named: 'key') do |file_path|
171+
key = run_described_fastlane_action(
154172
bucket: test_bucket,
155173
key: 'key',
156-
file: file_path
174+
file: file_path,
175+
skip_if_exists: true
157176
)
158-
end.to raise_error(FastlaneCore::Interface::FastlaneError, "File already exists in S3 bucket #{test_bucket} at #{expected_key}")
177+
expect(warnings).to eq(["File already exists in S3 bucket #{test_bucket} at #{expected_key}. Skipping upload."])
178+
expect(key).to eq(expected_key)
179+
end
159180
end
160181
end
161182
end

0 commit comments

Comments
 (0)