Class: Pilot::BuildManager

Inherits:
Manager
  • Object
show all
Defined in:
pilot/lib/pilot/build_manager.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary

Attributes inherited from Manager

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Manager

#app, #fetch_app_id, #fetch_app_identifier, #fetch_app_platform, #login, #start

Class Method Details

.sanitize_changelog(changelog) ⇒ Object



241
242
243
244
# File 'pilot/lib/pilot/build_manager.rb', line 241

def self.sanitize_changelog(changelog)
  changelog = strip_emoji(changelog)
  truncate_changelog(changelog)
end

.strip_emoji(changelog) ⇒ Object



233
234
235
236
237
238
239
# File 'pilot/lib/pilot/build_manager.rb', line 233

def self.strip_emoji(changelog)
  if changelog && changelog =~ EmojiRegex::Regex
    changelog.gsub!(EmojiRegex::Regex, "")
    UI.important("Emoji symbols have been removed from the changelog, since they're not allowed by Apple.")
  end
  changelog
end

.truncate_changelog(changelog) ⇒ Object



222
223
224
225
226
227
228
229
230
231
# File 'pilot/lib/pilot/build_manager.rb', line 222

def self.truncate_changelog(changelog)
  max_changelog_length = 4000
  if changelog && changelog.length > max_changelog_length
    original_length = changelog.length
    bottom_message = "..."
    changelog = "#{changelog[0...max_changelog_length - bottom_message.length]}#{bottom_message}"
    UI.important("Changelog has been truncated since it exceeds Apple's #{max_changelog_length} character limit. It currently contains #{original_length} characters.")
  end
  changelog
end

Instance Method Details

#check_for_changelog_or_whats_new!(options) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'pilot/lib/pilot/build_manager.rb', line 73

def check_for_changelog_or_whats_new!(options)
  if !has_changelog_or_whats_new?(options) && options[:distribute_external] == true
    if UI.interactive?
      options[:changelog] = UI.input("No changelog provided for new build. You can provide a changelog using the `changelog` option. For now, please provide a changelog here:")
    else
      UI.user_error!("No changelog provided for new build. Please either disable `distribute_external` or provide a changelog using the `changelog` option")
    end
  end
end

#distribute(options, build: nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'pilot/lib/pilot/build_manager.rb', line 96

def distribute(options, build: nil)
  start(options)
  if config[:apple_id].to_s.length == 0 && config[:app_identifier].to_s.length == 0
    config[:app_identifier] = UI.input("App Identifier: ")
  end

  # Get latest uploaded build if no build specified
  if build.nil?
    UI.important("No build specified - fetching latest build")
    platform = Spaceship::ConnectAPI::Platform.map(fetch_app_platform)
    build ||= Spaceship::ConnectAPI::Build.all(app_id: app.id, sort: "-uploadedDate", platform: platform, limit: 1).first
  end

  # Verify the build has all the includes that we need
  # and fetch a new build if not
  if build && (!build.app || !build.build_beta_detail || !build.pre_release_version)
    UI.important("Build did include information for app, build beta detail and pre release version")
    UI.important("Fetching a new build with all the information needed")
    build = Spaceship::ConnectAPI::Build.get(build_id: build.id)
  end

  # Error out if no build
  if build.nil?
    UI.user_error!("No build to distribute!")
  end

  # Update beta app meta info
  # 1. Demo account required
  # 2. App info
  # 3. Localized app  info
  # 4. Localized build info
  # 5. Auto notify enabled with config[:notify_external_testers]
  update_beta_app_meta(options, build)

  return if config[:skip_submission]
  if options[:reject_build_waiting_for_review]
    waiting_for_review_build = build.app.get_builds(filter: { "betaAppReviewSubmission.betaReviewState" => "WAITING_FOR_REVIEW" }, includes: "betaAppReviewSubmission,preReleaseVersion").first
    unless waiting_for_review_build.nil?
      UI.important("Another build is already in review. Going to remove that build and submit the new one.")
      UI.important("Deleting beta app review submission for build: #{waiting_for_review_build.app_version} - #{waiting_for_review_build.version}")
      waiting_for_review_build.beta_app_review_submission.delete!
      UI.success("Deleted beta app review submission for previous build: #{waiting_for_review_build.app_version} - #{waiting_for_review_build.version}")
    end
  end
  distribute_build(build, options)
  type = options[:distribute_external] ? 'External' : 'Internal'
  UI.success("Successfully distributed build to #{type} testers 🚀")
end

#has_changelog_or_whats_new?(options) ⇒ Boolean

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'pilot/lib/pilot/build_manager.rb', line 56

def has_changelog_or_whats_new?(options)
  # Look for legacy :changelog option
  has_changelog = !options[:changelog].nil?

  # Look for :whats_new in :localized_build_info
  unless has_changelog
    infos_by_lang = options[:localized_build_info] || []
    infos_by_lang.each do |k, v|
      next if has_changelog
      v ||= {}
      has_changelog = v.key?(:whats_new) || v.key?('whats_new')
    end
  end

  return has_changelog
end

#list(options) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'pilot/lib/pilot/build_manager.rb', line 145

def list(options)
  start(options)
  if config[:apple_id].to_s.length == 0 && config[:app_identifier].to_s.length == 0
    config[:app_identifier] = UI.input("App Identifier: ")
  end

  # Get processing builds
  build_deliveries = app.get_build_deliveries.map do |build_delivery|
    [
      build_delivery.cf_build_short_version_string,
      build_delivery.cf_build_version
    ]
  end

  # Get processed builds
  builds = app.get_builds(includes: "betaBuildMetrics,preReleaseVersion", sort: "-uploadedDate").map do |build|
    [
      build.app_version,
      build.version,
      (build.beta_build_metrics || []).map(&:install_count).reduce(:+)
    ]
  end

  # Only show table if there are any build deliveries
  unless build_deliveries.empty?
    puts(Terminal::Table.new(
           title: "#{app.name} Processing Builds".green,
           headings: ["Version #", "Build #"],
           rows: FastlaneCore::PrintTable.transform_output(build_deliveries)
    ))
  end

  puts(Terminal::Table.new(
         title: "#{app.name} Builds".green,
         headings: ["Version #", "Build #", "Installs"],
         rows: FastlaneCore::PrintTable.transform_output(builds)
  ))
end

#update_beta_app_meta(options, build) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'pilot/lib/pilot/build_manager.rb', line 184

def update_beta_app_meta(options, build)
  # Setting account required wth AppStore Connect API
  update_review_detail(build, { demo_account_required: options[:demo_account_required] })

  if should_update_beta_app_review_info(options)
    update_review_detail(build, options[:beta_app_review_info])
  end

  if should_update_localized_app_information?(options)
    update_localized_app_review(build, options[:localized_app_info])
  elsif should_update_app_test_information?(options)
    default_info = {}
    default_info[:feedback_email] = options[:beta_app_feedback_email] if options[:beta_app_feedback_email]
    default_info[:description] = options[:beta_app_description] if options[:beta_app_description]
    begin
      update_localized_app_review(build, {}, default_info: default_info)
      UI.success("Successfully set the beta_app_feedback_email and/or beta_app_description")
    rescue => ex
      UI.user_error!("Could not set beta_app_feedback_email and/or beta_app_description: #{ex}")
    end
  end

  if should_update_localized_build_information?(options)
    update_localized_build_review(build, options[:localized_build_info])
  elsif should_update_build_information?(options)
    begin
      update_localized_build_review(build, {}, default_info: { whats_new: options[:changelog] })
      UI.success("Successfully set the changelog for build")
    rescue => ex
      UI.user_error!("Could not set changelog: #{ex}")
    end
  end

  update_build_beta_details(build, {
    auto_notify_enabled: options[:notify_external_testers]
  })
end

#upload(options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'pilot/lib/pilot/build_manager.rb', line 13

def upload(options)
  # Only need to login before upload if no apple_id was given
  # 'login' will be deferred until before waiting for build processing
   = options[:apple_id].nil?
  start(options, should_login: )

  UI.user_error!("No ipa file given") unless config[:ipa]

  check_for_changelog_or_whats_new!(options)

  UI.success("Ready to upload new build to TestFlight (App: #{fetch_app_id})...")

  dir = Dir.mktmpdir

  platform = fetch_app_platform
  package_path = FastlaneCore::IpaUploadPackageBuilder.new.generate(app_id: fetch_app_id,
                                                                  ipa_path: options[:ipa],
                                                              package_path: dir,
                                                                  platform: platform)

  transporter = transporter_for_selected_team(options)
  result = transporter.upload(fetch_app_id, package_path)

  unless result
    UI.user_error!("Error uploading ipa file, for more information see above")
  end

  UI.success("Successfully uploaded the new binary to App Store Connect")

  if config[:skip_waiting_for_build_processing]
    UI.important("Skip waiting for build processing")
    UI.important("This means that no changelog will be set and no build will be distributed to testers")
    return
  end

  # Calling login again here is needed if login was not called during 'start'
   unless 

  UI.message("If you want to skip waiting for the processing to be finished, use the `skip_waiting_for_build_processing` option")
  latest_build = wait_for_build_processing_to_be_complete
  distribute(options, build: latest_build)
end

#wait_for_build_processing_to_be_completeObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'pilot/lib/pilot/build_manager.rb', line 83

def wait_for_build_processing_to_be_complete
  platform = fetch_app_platform
  app_version = FastlaneCore::IpaFileAnalyser.fetch_app_version(config[:ipa])
  app_build = FastlaneCore::IpaFileAnalyser.fetch_app_build(config[:ipa])
  latest_build = FastlaneCore::BuildWatcher.wait_for_build_processing_to_be_complete(app_id: app.id, platform: platform, app_version: app_version, build_version: app_build, poll_interval: config[:wait_processing_interval], return_spaceship_testflight_build: false)

  unless latest_build.app_version == app_version && latest_build.version == app_build
    UI.important("Uploaded app #{app_version} - #{app_build}, but received build #{latest_build.app_version} - #{latest_build.version}.")
  end

  return latest_build
end