Class: Fastlane::Actions::AppStoreBuildNumberAction

Inherits:
Fastlane::Action show all
Defined in:
fastlane/lib/fastlane/actions/app_store_build_number.rb

Constant Summary

Constants inherited from Fastlane::Action

Fastlane::Action::AVAILABLE_CATEGORIES, Fastlane::Action::RETURN_TYPES

Documentation collapse

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, author, deprecated_notes, lane_context, method_missing, other_action, return_type, return_value, sample_return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

.authorsObject



237
238
239
# File 'fastlane/lib/fastlane/actions/app_store_build_number.rb', line 237

def self.authors
  ["hjanuschka"]
end

.available_optionsObject



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
144
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'fastlane/lib/fastlane/actions/app_store_build_number.rb', line 114

def self.available_options
  user = CredentialsManager::AppfileConfig.try_fetch_value(:itunes_connect_id)
  user ||= CredentialsManager::AppfileConfig.try_fetch_value(:apple_id)
  [
    FastlaneCore::ConfigItem.new(key: :api_key_path,
                                 env_names: ["APPSTORE_BUILD_NUMBER_API_KEY_PATH", "APP_STORE_CONNECT_API_KEY_PATH"],
                                 description: "Path to your App Store Connect API Key JSON file (https://docs.fastlane.tools/app-store-connect-api/#using-fastlane-api-key-json-file)",
                                 optional: true,
                                 conflicting_options: [:api_key],
                                 verify_block: proc do |value|
                                   UI.user_error!("Couldn't find API key JSON file at path '#{value}'") unless File.exist?(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :api_key,
                                 env_names: ["APPSTORE_BUILD_NUMBER_API_KEY", "APP_STORE_CONNECT_API_KEY"],
                                 description: "Your App Store Connect API Key information (https://docs.fastlane.tools/app-store-connect-api/#use-return-value-and-pass-in-as-an-option)",
                                 type: Hash,
                                 default_value: Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::APP_STORE_CONNECT_API_KEY],
                                 default_value_dynamic: true,
                                 optional: true,
                                 sensitive: true,
                                 conflicting_options: [:api_key_path]),
    FastlaneCore::ConfigItem.new(key: :initial_build_number,
                                 env_name: "INITIAL_BUILD_NUMBER",
                                 description: "sets the build number to given value if no build is in current train",
                                 is_string: false),
    FastlaneCore::ConfigItem.new(key: :app_identifier,
                                 short_option: "-a",
                                 env_name: "FASTLANE_APP_IDENTIFIER",
                                 description: "The bundle identifier of your app",
                                 code_gen_sensitive: true,
                                 default_value: CredentialsManager::AppfileConfig.try_fetch_value(:app_identifier),
                                 default_value_dynamic: true),
    FastlaneCore::ConfigItem.new(key: :username,
                                 short_option: "-u",
                                 env_name: "ITUNESCONNECT_USER",
                                 description: "Your Apple ID Username",
                                 optional: true,
                                 default_value: user,
                                 default_value_dynamic: true),
    FastlaneCore::ConfigItem.new(key: :team_id,
                                 short_option: "-k",
                                 env_name: "APPSTORE_BUILD_NUMBER_LIVE_TEAM_ID",
                                 description: "The ID of your App Store Connect team if you're in multiple teams",
                                 optional: true,
                                 is_string: false, # as we also allow integers, which we convert to strings anyway
                                 code_gen_sensitive: true,
                                 default_value: CredentialsManager::AppfileConfig.try_fetch_value(:itc_team_id),
                                 default_value_dynamic: true,
                                 verify_block: proc do |value|
                                   ENV["FASTLANE_ITC_TEAM_ID"] = value.to_s
                                 end),
    FastlaneCore::ConfigItem.new(key: :live,
                                 short_option: "-l",
                                 env_name: "APPSTORE_BUILD_NUMBER_LIVE",
                                 description: "Query the live version (ready-for-sale)",
                                 optional: true,
                                 is_string: false,
                                 default_value: true),
    FastlaneCore::ConfigItem.new(key: :version,
                                 env_name: "LATEST_VERSION",
                                 description: "The version number whose latest build number we want",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :platform,
                                 short_option: "-j",
                                 env_name: "APPSTORE_PLATFORM",
                                 description: "The platform to use (optional)",
                                 optional: true,
                                 is_string: true,
                                 default_value: "ios",
                                 verify_block: proc do |value|
                                   UI.user_error!("The platform can only be ios, appletvos, or osx") unless %('ios', 'appletvos', 'osx').include?(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :team_name,
                                 short_option: "-e",
                                 env_name: "LATEST_TESTFLIGHT_BUILD_NUMBER_TEAM_NAME",
                                 description: "The name of your App Store Connect team if you're in multiple teams",
                                 optional: true,
                                 code_gen_sensitive: true,
                                 default_value: CredentialsManager::AppfileConfig.try_fetch_value(:itc_team_name),
                                 default_value_dynamic: true,
                                 verify_block: proc do |value|
                                   ENV["FASTLANE_ITC_TEAM_NAME"] = value.to_s
                                 end)
  ]
end

.categoryObject



241
242
243
# File 'fastlane/lib/fastlane/actions/app_store_build_number.rb', line 241

def self.category
  :app_store_connect
end

.descriptionObject



110
111
112
# File 'fastlane/lib/fastlane/actions/app_store_build_number.rb', line 110

def self.description
  "Returns the current build_number of either live or edit version"
end

.detailsObject



207
208
209
210
211
212
# File 'fastlane/lib/fastlane/actions/app_store_build_number.rb', line 207

def self.details
  [
    "Returns the current build number of either the live or testflight version - it is useful for getting the build_number of the current or ready-for-sale app version, and it also works on non-live testflight version.",
    "If you need to handle more build-trains please see `latest_testflight_build_number`."
  ].join("\n")
end

.example_codeObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'fastlane/lib/fastlane/actions/app_store_build_number.rb', line 214

def self.example_code
  [
    'app_store_build_number',
    'app_store_build_number(
      app_identifier: "app.identifier",
      username: "[email protected]"
    )',
    'app_store_build_number(
      live: false,
      app_identifier: "app.identifier",
      version: "1.2.9"
    )',
    'api_key = app_store_connect_api_key(
      key_id: "MyKeyID12345",
      issuer_id: "00000000-0000-0000-0000-000000000000",
      key_filepath: "./AuthKey.p8"
    )
    build_num = app_store_build_number(
      api_key: api_key
    )'
  ]
end

.get_build_number(params) ⇒ Object



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
63
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
# File 'fastlane/lib/fastlane/actions/app_store_build_number.rb', line 28

def self.get_build_number(params)
  # Prompts select team if multiple teams and none specified
  if (api_token = Spaceship::ConnectAPI::Token.from(hash: params[:api_key], filepath: params[:api_key_path]))
    UI.message("Creating authorization token for App Store Connect API")
    Spaceship::ConnectAPI.token = api_token
  elsif !Spaceship::ConnectAPI.token.nil?
    UI.message("Using existing authorization token for App Store Connect API")
  else
    # Username is now optional since addition of App Store Connect API Key
    # Force asking for username to prompt user if not already set
    params.fetch(:username, force_ask: true)

    UI.message("Login to App Store Connect (#{params[:username]})")
    Spaceship::ConnectAPI.(params[:username], use_portal: false, use_tunes: true, tunes_team_id: params[:team_id], team_name: params[:team_name])
    UI.message("Login successful")
  end

  platform = Spaceship::ConnectAPI::Platform.map(params[:platform])

  app = Spaceship::ConnectAPI::App.find(params[:app_identifier])
  UI.user_error!("Could not find an app on App Store Connect with app_identifier: #{params[:app_identifier]}") unless app
  if params[:live]
    UI.message("Fetching the latest build number for live-version")
    live_version = app.get_live_app_store_version(platform: platform)

    UI.user_error!("Could not find a live-version of #{params[:app_identifier]} on App Store Connect") unless live_version
    build_nr = live_version.build.version

    UI.message("Latest upload for live-version #{live_version.version_string} is build: #{build_nr}")

    return OpenStruct.new({ build_nr: build_nr, build_v: live_version.version_string })
  else
    version_number = params[:version]
    platform = params[:platform]

    # Create filter for get_builds with optional version number
    filter = { app: app.id }
    if version_number
      filter["preReleaseVersion.version"] = version_number
      version_number_message = "version #{version_number}"
    else
      version_number_message = "any version"
    end

    if platform
      filter["preReleaseVersion.platform"] = Spaceship::ConnectAPI::Platform.map(platform)
      platform_message = "#{platform} platform"
    else
      platform_message = "any platform"
    end

    UI.message("Fetching the latest build number for #{version_number_message}")

    # Get latest build for optional version number and return build number if found
    build = Spaceship::ConnectAPI.get_builds(filter: filter, sort: "-uploadedDate", includes: "preReleaseVersion", limit: 1).first
    if build
      build_nr = build.version
      UI.message("Latest upload for version #{build.app_version} on #{platform_message} is build: #{build_nr}")
      return OpenStruct.new({ build_nr: build_nr, build_v: build.app_version })
    end

    # Let user know that build couldn't be found
    UI.important("Could not find a build for #{version_number_message} on #{platform_message} on App Store Connect")

    if params[:initial_build_number].nil?
      UI.user_error!("Could not find a build on App Store Connect - and 'initial_build_number' option is not set")
    else
      build_nr = params[:initial_build_number]
      UI.message("Using initial build number of #{build_nr}")
      return OpenStruct.new({ build_nr: build_nr, build_v: version_number })
    end
  end
end

.is_supported?(platform) ⇒ Boolean

Returns:



245
246
247
# File 'fastlane/lib/fastlane/actions/app_store_build_number.rb', line 245

def self.is_supported?(platform)
  [:ios, :mac].include?(platform)
end

.order_versions(versions) ⇒ Object



102
103
104
# File 'fastlane/lib/fastlane/actions/app_store_build_number.rb', line 102

def self.order_versions(versions)
  versions.map(&:to_s).sort_by { |v| Gem::Version.new(v) }
end

.outputObject



200
201
202
203
204
205
# File 'fastlane/lib/fastlane/actions/app_store_build_number.rb', line 200

def self.output
  [
    ['LATEST_BUILD_NUMBER', 'The latest build number of either live or testflight version'],
    ['LATEST_VERSION', 'The version of the latest build number']
  ]
end

.run(params) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'fastlane/lib/fastlane/actions/app_store_build_number.rb', line 11

def self.run(params)
  require 'spaceship'

  result = get_build_number(params)
  build_nr = result.build_nr

  # Convert build_nr to int (for legacy use) if no "." in string
  if build_nr.kind_of?(String) && !build_nr.include?(".")
    build_nr = build_nr.to_i
  end

  Actions.lane_context[SharedValues::LATEST_BUILD_NUMBER] = build_nr
  Actions.lane_context[SharedValues::LATEST_VERSION] = result.build_v

  return build_nr
end