Class: Pilot::BuildManager

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

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


151
152
153
154
# File 'pilot/lib/pilot/build_manager.rb', line 151

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

.strip_emoji(changelog) ⇒ Object


143
144
145
146
147
148
149
# File 'pilot/lib/pilot/build_manager.rb', line 143

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


132
133
134
135
136
137
138
139
140
141
# File 'pilot/lib/pilot/build_manager.rb', line 132

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

#distribute(options, build: nil) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'pilot/lib/pilot/build_manager.rb', line 64

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

  build ||= Spaceship::TestFlight::Build.latest(app_id: app.apple_id, platform: fetch_app_platform)
  if build.nil?
    UI.user_error!("No build to distribute!")
  end

  if should_update_app_test_information?(options)
    app_test_info = Spaceship::TestFlight::AppTestInfo.find(app_id: build.app_id)
    app_test_info.test_info.feedback_email = options[:beta_app_feedback_email] if options[:beta_app_feedback_email]
    app_test_info.test_info.description = options[:beta_app_description] if options[:beta_app_description]
    begin
      app_test_info.save_for_app!(app_id: build.app_id)
      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_build_information?(options)
    begin
      build.update_build_information!(whats_new: options[:changelog])
      UI.success("Successfully set the changelog for build")
    rescue => ex
      UI.user_error!("Could not set changelog: #{ex}")
    end
  end

  build.auto_notify_enabled = config[:notify_external_testers]

  return if config[:skip_submission]
  if options[:reject_build_waiting_for_review]
    waiting_for_review_build = Spaceship::TestFlight::Build.all_waiting_for_review(app_id: build.app_id, platform: fetch_app_platform).first
    unless waiting_for_review_build.nil?
      UI.important("Another build is already in review. Going to expire that build and submit the new one.")
      UI.important("Expiring build: #{waiting_for_review_build.train_version} - #{waiting_for_review_build.build_version}")
      waiting_for_review_build.expire!
      UI.success("Expired previous build: #{waiting_for_review_build.train_version} - #{waiting_for_review_build.build_version}")
    end
  end
  distribute_build(build, options)
  type = options[:distribute_external] ? 'External' : 'Internal'
  UI.success("Successfully distributed build to #{type} testers 🚀")
end

#list(options) ⇒ Object


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'pilot/lib/pilot/build_manager.rb', line 113

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

  platform = fetch_app_platform(required: false)
  builds = app.all_processing_builds(platform: platform) + app.builds(platform: platform)
  # sort by upload_date
  builds.sort! { |a, b| a.upload_date <=> b.upload_date }
  rows = builds.collect { |build| describe_build(build) }

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

#upload(options) ⇒ Object


12
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
55
56
57
58
59
60
61
62
# File 'pilot/lib/pilot/build_manager.rb', line 12

def upload(options)
  start(options)

  options[:changelog] = self.class.sanitize_changelog(options[:changelog]) if options[:changelog]

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

  if options[:changelog].nil? && 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

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

  dir = Dir.mktmpdir

  platform = fetch_app_platform
  package_path = FastlaneCore::IpaUploadPackageBuilder.new.generate(app_id: app.apple_id,
                                                                  ipa_path: config[:ipa],
                                                              package_path: dir,
                                                                  platform: platform)

  transporter = FastlaneCore::ItunesTransporter.new(options[:username], nil, false, options[:itc_provider])
  result = transporter.upload(app.apple_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

  UI.message("If you want to skip waiting for the processing to be finished, use the `skip_waiting_for_build_processing` option")
  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.apple_id, platform: platform, train_version: app_version, build_version: app_build, poll_interval: config[:wait_processing_interval], strict_build_watch: config[:wait_for_uploaded_build])

  unless latest_build.train_version == app_version && latest_build.build_version == app_build
    UI.important("Uploaded app #{app_version} - #{app_build}, but received build #{latest_build.train_version} - #{latest_build.build_version}. If you want to wait for uploaded build to be finished processing, use the `wait_for_uploaded_build` option")
  end

  distribute(options, build: latest_build)
end