Skip to content

Commit 3f28db7

Browse files
committed
Refactoring to split code in more separate methods
1 parent 9255053 commit 3f28db7

2 files changed

Lines changed: 57 additions & 25 deletions

File tree

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

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,17 @@ module Actions
33
class PrototypeBuildDetailsCommentAction < Action
44
def self.run(params)
55
app_display_name = params[:app_display_name]
6-
7-
app_center_org_name = params[:app_center_org_name]
8-
app_center_info = extract_app_center_info(app_center_org_name)
9-
app_center_app_name = params[:app_center_app_name] || app_center_info['app_name']
10-
app_center_app_display_name = app_center_info['app_display_name'] || app_center_app_name
11-
app_center_release_id = params[:app_center_release_id] || app_center_info['id']
12-
13-
# Assemble explicit metadata passed as params with implicit metadata derived from App Center params or lane_context
6+
app_center_info = AppCenterInfo.from_params(params)
147
metadata = consolidate_metadata(params, app_center_info)
158

16-
# Installation Link(s) -- either download_url param, or App Center Build link, or both
17-
qr_code_url, extra_metadata = build_install_links(params[:download_url], app_center_org_name, app_center_app_name, app_center_app_display_name, app_center_release_id)
9+
qr_code_url, extra_metadata = build_install_links(app_center_info, params[:download_url])
1810
metadata.merge!(extra_metadata)
1911

2012
# Build the comment parts
21-
icon_img_tag = img_tag(params[:app_icon] || app_center_info['app_icon_url'], alt: app_display_name)
13+
icon_img_tag = img_tag(params[:app_icon] || app_center_info.icon, alt: app_display_name)
2214
metadata_rows = metadata.compact.map { |key, value| "<tr><td><b>#{key}</b></td><td>#{value}</td></tr>" }
2315
intro = "#{icon_img_tag}📲 You can test the changes from this Pull Request in <b>#{app_display_name}</b> by scanning the QR code below to install the corresponding build."
24-
footnote = params[:footnote] || (app_center_org_name.nil? ? '' : DEFAULT_APP_CENTER_FOOTNOTE)
16+
footnote = params[:footnote] || (app_center_info.org_name.nil? ? '' : DEFAULT_APP_CENTER_FOOTNOTE)
2517
body = <<~COMMENT_BODY
2618
<table>
2719
<tr>
@@ -52,40 +44,80 @@ def self.run(params)
5244

5345
DEFAULT_APP_CENTER_FOOTNOTE = '<em>Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.</em>'.freeze
5446

55-
def self.extract_app_center_info(app_center_org_name)
56-
if app_center_org_name && defined?(SharedValues::APPCENTER_BUILD_INFORMATION)
57-
lane_context[SharedValues::APPCENTER_BUILD_INFORMATION] || {}
58-
else
59-
{}
47+
# A small model struct to consolidate and pack all the values related to App Center
48+
#
49+
AppCenterInfo = Struct.new(:org_name, :app_name, :display_name, :release_id, :icon, :version, :short_version, :os, :bundle_id) do
50+
# A method to construct an AppCenterInfo instance from the action params, and infer the rest from the `lane_context` if available
51+
def self.from_params(params)
52+
org_name = params[:app_center_org_name]
53+
ctx = if org_name && defined?(SharedValues::APPCENTER_BUILD_INFORMATION)
54+
Fastlane::Actions.lane_context[SharedValues::APPCENTER_BUILD_INFORMATION] || {}
55+
else
56+
{}
57+
end
58+
app_name = params[:app_center_app_name] || ctx['app_name']
59+
new(
60+
org_name,
61+
app_name,
62+
ctx['app_display_name'] || app_name,
63+
params[:app_center_release_id] || ctx['id'],
64+
ctx['app_icon_url'],
65+
ctx['version'],
66+
ctx['short_version'],
67+
ctx['app_os'],
68+
ctx['bundle_identifier']
69+
)
6070
end
6171
end
6272

63-
def self.build_install_links(download_url, app_center_org_name, app_center_app_name, app_center_app_display_name, app_center_release_id)
73+
# Builds the installation link, QR code URL and extra metadata for download links from the available info
74+
#
75+
# @param [AppCenterInfo] app_center_info The struct containing all the values related to App Center info
76+
# @param [String] download_url The `download_url` parameter passed to the action, if one exists
77+
# @return [(String, Hash<String,String>)] A tuple containing:
78+
# - The URL for the QR Code
79+
# - A Hash of the extra metadata key/value pairs to add to the existing metadata, to enrich them with download/install links
80+
#
81+
def self.build_install_links(app_center_info, download_url)
6482
install_url = nil
6583
extra_metadata = {}
6684
if download_url
6785
install_url = download_url
6886
extra_metadata['Direct Download'] = "<a href='#{install_url}'><code>#{File.basename(install_url)}</code></a>"
6987
end
70-
if app_center_org_name && app_center_app_name
71-
install_url = "https://install.appcenter.ms/orgs/#{app_center_org_name}/apps/#{app_center_app_name}/releases/#{app_center_release_id}"
72-
extra_metadata['App Center Build'] = "<a href='#{install_url}'>#{app_center_app_display_name} \##{app_center_release_id}</a>"
88+
if app_center_info.org_name && app_center_info.app_name
89+
install_url = "https://install.appcenter.ms/orgs/#{app_center_info.org_name}/apps/#{app_center_info.app_name}/releases/#{app_center_info.release_id}"
90+
extra_metadata['App Center Build'] = "<a href='#{install_url}'>#{app_center_info.display_name} \##{app_center_info.release_id}</a>"
7391
end
7492
UI.user_error!(NO_INSTALL_URL_ERROR_MESSAGE) if install_url.nil?
7593
qr_code_url = "https://chart.googleapis.com/chart?chs=500x500&cht=qr&chl=#{CGI.escape(install_url)}&choe=UTF-8"
7694
[qr_code_url, extra_metadata]
7795
end
7896

97+
# A method to build the Hash of metadata, based on the explicit ones passed by the user as parameter + the implicit ones from `AppCenterInfo`
98+
#
99+
# @param [Hash<Symbol, Any>] params The action's parameters, as received by `self.run`
100+
# @param [AppCenterInfo] app_center_info The model object containing all the values related to App Center information
101+
# @return [Hash<String, String>] A hash of all the metadata, gathered from both the explicit and the implicit ones
102+
#
79103
def self.consolidate_metadata(params, app_center_info)
80104
metadata = params[:metadata]&.transform_keys(&:to_s) || {}
81-
metadata['Build Number'] ||= app_center_info['version']
82-
metadata['Version'] ||= app_center_info['short_version']
83-
metadata[app_center_info['app_os'] == 'Android' ? 'Application ID' : 'Bundle ID'] ||= app_center_info['bundle_identifier']
105+
metadata['Build Number'] ||= app_center_info.version
106+
metadata['Version'] ||= app_center_info.short_version
107+
metadata[app_center_info.os == 'Android' ? 'Application ID' : 'Bundle ID'] ||= app_center_info.bundle_id
84108
# (Feel free to add more CI-specific env vars in the line below to support other CI providers if you need)
85109
metadata['Commit'] ||= ENV.fetch('BUILDKITE_COMMIT', nil) || other_action.last_git_commit[:abbreviated_commit_hash]
86110
metadata
87111
end
88112

113+
# Creates an HTML `<img>` tag for an icon URL or the image URL to represent a given Buildkite emoji
114+
#
115+
# @param [String] url_or_emoji A `String` which can be:
116+
# - Either a valid URI to an image
117+
# - Or a string formatted like `:emojiname:`, using a valid Buildite emoji name as defined in https://github.com/buildkite/emojis
118+
# @param [String] alt The alt text to use for the `<img>` tag
119+
# @return [String] The `<img …>` tag with the proper image and alt tag
120+
#
89121
def self.img_tag(url_or_emoji, alt: '')
90122
return nil if url_or_emoji.nil?
91123

spec/prototype_build_details_comment_action_spec.rb

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

277277
before do
278278
stub_const('Fastlane::Actions::SharedValues::APPCENTER_BUILD_INFORMATION', :fake_app_center_build_info)
279-
allow(described_class).to receive(:lane_context).and_return({ fake_app_center_build_info: fake_lane_context })
279+
allow(Fastlane::Actions).to receive(:lane_context).and_return({ fake_app_center_build_info: fake_lane_context })
280280
end
281281

282282
describe 'checking specific content is present' do

0 commit comments

Comments
 (0)