Skip to content

Commit

Permalink
support alternate download endpoint urls (#48)
Browse files Browse the repository at this point in the history
* support alternate download endpoint urls

* adds ability to control the regex for replacing s3 url with download_endpoint

* Synced with upstream and fixed merge conflicts

Co-authored-by: Case Taintor <[email protected]>
Co-authored-by: Siddarth <[email protected]>
  • Loading branch information
3 people authored Mar 25, 2021
1 parent 003ab10 commit 4cdd70d
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions lib/fastlane/plugin/aws_s3/actions/aws_s3_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def self.run(config)
params[:aws_profile] = config[:aws_profile]
params[:bucket] = config[:bucket]
params[:endpoint] = config[:endpoint]
params[:download_endpoint] = config[:download_endpoint]
params[:download_endpoint_replacement_regex] = config[:download_endpoint_replacement_regex]
params[:region] = config[:region]
params[:app_directory] = config[:app_directory]
params[:acl] = config[:acl]
Expand Down Expand Up @@ -128,14 +130,15 @@ def self.upload_ipa(s3_client, params, s3_region, s3_access_key, s3_secret_acces
version_template_params = params[:version_template_params] || {}
version_file_name = params[:version_file_name]
override_file_name = params[:override_file_name]

download_endpoint = params[:download_endpoint]
download_endpoint_replacement_regex = params[:download_endpoint_replacement_regex]
url_part = self.expand_path_with_substitutions_from_ipa_plist(ipa_file, s3_path)

ipa_file_basename = File.basename(ipa_file)
ipa_file_name = "#{url_part}#{override_file_name ? override_file_name : ipa_file_basename}"
ipa_file_data = File.open(ipa_file, 'rb')

ipa_url = self.upload_file(s3_client, s3_bucket, app_directory, ipa_file_name, ipa_file_data, acl, server_side_encryption)
ipa_url = self.upload_file(s3_client, s3_bucket, app_directory, ipa_file_name, ipa_file_data, acl, server_side_encryption, download_endpoint, download_endpoint_replacement_regex)

# Setting action and environment variables
Actions.lane_context[SharedValues::S3_IPA_OUTPUT_PATH] = ipa_url
Expand All @@ -146,7 +149,7 @@ def self.upload_ipa(s3_client, params, s3_region, s3_access_key, s3_secret_acces
dsym_file_name = "#{url_part}#{dsym_file_basename}"
dsym_file_data = File.open(dsym_file, 'rb')

dsym_url = self.upload_file(s3_client, s3_bucket, app_directory, dsym_file_name, dsym_file_data, acl, server_side_encryption)
dsym_url = self.upload_file(s3_client, s3_bucket, app_directory, dsym_file_name, dsym_file_data, acl, server_side_encryption, download_endpoint, download_endpoint_replacement_regex)

# Setting action and environment variables
Actions.lane_context[SharedValues::S3_DSYM_OUTPUT_PATH] = dsym_url
Expand Down Expand Up @@ -207,7 +210,7 @@ def self.upload_ipa(s3_client, params, s3_region, s3_access_key, s3_secret_acces
# plist uploading
#
#####################################
plist_url = self.upload_file(s3_client, s3_bucket, app_directory, plist_file_name, plist_render, acl, server_side_encryption)
plist_url = self.upload_file(s3_client, s3_bucket, app_directory, plist_file_name, plist_render, acl, server_side_encryption, download_endpoint, download_endpoint_replacement_regex)

# Creates html from template
if html_template_path && File.exist?(html_template_path)
Expand Down Expand Up @@ -250,8 +253,8 @@ def self.upload_ipa(s3_client, params, s3_region, s3_access_key, s3_secret_acces

skip_html = params[:skip_html_upload]
html_file_name = "#{url_part}#{html_file_name}" if generate_html_in_folder
html_url = self.upload_file(s3_client, s3_bucket, app_directory, html_file_name, html_render, acl, server_side_encryption) unless skip_html
version_url = self.upload_file(s3_client, s3_bucket, app_directory, version_file_name, version_render, acl, server_side_encryption)
html_url = self.upload_file(s3_client, s3_bucket, app_directory, html_file_name, html_render, acl, server_side_encryption, download_endpoint, download_endpoint_replacement_regex) unless skip_html
version_url = self.upload_file(s3_client, s3_bucket, app_directory, version_file_name, version_render, acl, server_side_encryption, download_endpoint, download_endpoint_replacement_regex)

# Setting action and environment variables
Actions.lane_context[SharedValues::S3_PLIST_OUTPUT_PATH] = plist_url
Expand Down Expand Up @@ -283,8 +286,10 @@ def self.upload_xcarchive(s3_client, params, s3_region, s3_access_key, s3_secret
sh("zip -r '#{archive_zip}' '#{archive}'")
full_archive_zip_name = "#{url_part}#{archive_zip_name}"
archive_zip_data = File.open(archive_zip, 'rb')
download_endpoint = params[:download_endpoint]
download_endpoint_replacement_regex = params[:download_endpoint_replacement_regex]

archive_url = self.upload_file(s3_client, s3_bucket, app_directory, full_archive_zip_name, archive_zip_data, acl, server_side_encryption)
archive_url = self.upload_file(s3_client, s3_bucket, app_directory, full_archive_zip_name, archive_zip_data, acl, server_side_encryption, download_endpoint, download_endpoint_replacement_regex)

Actions.lane_context[SharedValues::S3_XCARCHIVE_OUTPUT_PATH] = archive_url
ENV[SharedValues::S3_XCARCHIVE_OUTPUT_PATH.to_s] = archive_url
Expand All @@ -311,14 +316,16 @@ def self.upload_apk(s3_client, params, s3_region, s3_access_key, s3_secret_acces
version_template_params = params[:version_template_params] || {}
version_file_name = params[:version_file_name]
override_file_name = params[:override_file_name]
download_endpoint = params[:download_endpoint]
download_endpoint_replacement_regex = params[:download_endpoint_replacement_regex]

url_part = s3_path

apk_file_basename = File.basename(apk_file)
apk_file_name = "#{url_part}#{override_file_name ? override_file_name : apk_file_basename}"
apk_file_data = File.open(apk_file, 'rb')

apk_url = self.upload_file(s3_client, s3_bucket, app_directory, apk_file_name, apk_file_data, acl, server_side_encryption)
apk_url = self.upload_file(s3_client, s3_bucket, app_directory, apk_file_name, apk_file_data, acl, server_side_encryption, download_endpoint, download_endpoint_replacement_regex)

# Setting action and environment variables
Actions.lane_context[SharedValues::S3_APK_OUTPUT_PATH] = apk_url
Expand Down Expand Up @@ -377,8 +384,8 @@ def self.upload_apk(s3_client, params, s3_region, s3_access_key, s3_secret_acces

skip_html = params[:skip_html_upload]
html_file_name = "#{url_part}#{html_file_name}" if generate_html_in_folder
html_url = self.upload_file(s3_client, s3_bucket, app_directory, html_file_name, html_render, acl, server_side_encryption) unless skip_html
version_url = self.upload_file(s3_client, s3_bucket, app_directory, version_file_name, version_render, acl, server_side_encryption)
html_url = self.upload_file(s3_client, s3_bucket, app_directory, html_file_name, html_render, acl, server_side_encryption, download_endpoint, download_endpoint_replacement_regex) unless skip_html
version_url = self.upload_file(s3_client, s3_bucket, app_directory, version_file_name, version_render, acl, server_side_encryption, download_endpoint, download_endpoint_replacement_regex)

Actions.lane_context[SharedValues::S3_HTML_OUTPUT_PATH] = html_url unless skip_html
ENV[SharedValues::S3_HTML_OUTPUT_PATH.to_s] = html_url unless skip_html
Expand Down Expand Up @@ -408,7 +415,9 @@ def self.upload_source(s3_client, params, s3_bucket, source_directory, s3_path,
zip_file_name = "#{url_part}source.zip"

output_path_data = File.open("#{output_file_path}", 'rb')
source_url = self.upload_file(s3_client, s3_bucket, app_directory, zip_file_name, output_path_data, acl, server_side_encryption)
download_endpoint = params[:download_endpoint]
download_endpoint_replacement_regex = params[:download_endpoint_replacement_regex]
source_url = self.upload_file(s3_client, s3_bucket, app_directory, zip_file_name, output_path_data, acl, server_side_encryption, download_endpoint, download_endpoint_replacement_regex)

Actions.lane_context[SharedValues::S3_SOURCE_OUTPUT_PATH] = source_url
ENV[SharedValues::S3_SOURCE_OUTPUT_PATH.to_s] = source_url
Expand Down Expand Up @@ -498,9 +507,7 @@ def self.upload_folder(s3_client, params, s3_region, s3_access_key, s3_secret_ac
end
end



def self.upload_file(s3_client, bucket_name, app_directory, file_name, file_data, acl, server_side_encryption)
def self.upload_file(s3_client, bucket_name, app_directory, file_name, file_data, acl, server_side_encryption, download_endpoint, download_endpoint_replacement_regex)

if app_directory
file_name = "#{app_directory}/#{file_name}"
Expand All @@ -525,7 +532,14 @@ def self.upload_file(s3_client, bucket_name, app_directory, file_name, file_data
end

# Return public url
obj.public_url.to_s
url = obj.public_url.to_s

# if a download endpoint is provided, then swap it in before returning
if download_endpoint
url = url.gsub(Regexp.new(download_endpoint_replacement_regex), download_endpoint)
end

return url
end

#
Expand Down Expand Up @@ -685,6 +699,16 @@ def self.available_options
description: "The base endpoint for your S3 bucket",
optional: true,
default_value: nil),
FastlaneCore::ConfigItem.new(key: :download_endpoint,
env_name: "S3_DOWNLOAD_ENDPOINT",
description: "The endpoint for downloads from your S3 bucket",
optional: true,
default_value: nil),
FastlaneCore::ConfigItem.new(key: :download_endpoint_replacement_regex,
env_name: "S3_DOWNLOAD_ENDPOINT_REPLACEMENT_REGEX",
description: "A regex used to determine which part of the S3 URL to replace with S3_DOWNLOAD_ENDPOINT",
optional: true,
default_value: '^https?://[^/]*'),
FastlaneCore::ConfigItem.new(key: :override_file_name,
env_name: "",
description: "Optional override ipa/apk uploaded file name",
Expand Down

0 comments on commit 4cdd70d

Please sign in to comment.