Class: Fastlane::Actions::AppcenterUploadAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb

Class Method Summary collapse

Class Method Details

.add_to_group(api_token, release_url, group_name, release_notes = '') ⇒ Object

add release to distribution group



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 199

def self.add_to_group(api_token, release_url, group_name, release_notes = '')
  connection = self.connection

  response = connection.patch do |req|
    req.url("/#{release_url}")
    req.headers['X-API-Token'] = api_token
    req.headers['internal-request-source'] = "fastlane"
    req.body = {
      "distribution_group_name" => group_name,
      "release_notes" => release_notes
    }
  end

  case response.status
  when 200...300
    release = response.body
    download_url = release['download_url']

    UI.message("DEBUG: #{JSON.pretty_generate(release)}") if ENV['DEBUG']

    Actions.lane_context[SharedValues::APPCENTER_DOWNLOAD_LINK] = download_url
    Actions.lane_context[SharedValues::APPCENTER_BUILD_INFORMATION] = release

    UI.message("Public Download URL: #{download_url}") if download_url
    UI.success("Release #{release['short_version']} was successfully distributed")

    release
  when 404
    UI.error("Not found, invalid distribution group name")
    false
  else
    UI.error("Error adding to group #{response.status}: #{response.body}")
    false
  end
end

.authorsObject



410
411
412
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 410

def self.authors
  ["Microsoft"]
end

.available_optionsObject



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 420

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :api_token,
                            env_name: "APPCENTER_API_TOKEN",
                         description: "API Token for App Center",
                            optional: false,
                                type: String,
                        verify_block: proc do |value|
                          UI.user_error!("No API token for App Center given, pass using `api_token: 'token'`") unless value && !value.empty?
                        end),

    FastlaneCore::ConfigItem.new(key: :owner_name,
                            env_name: "APPCENTER_OWNER_NAME",
                         description: "Owner name",
                            optional: false,
                                type: String,
                        verify_block: proc do |value|
                          UI.user_error!("No Owner name for App Center given, pass using `owner_name: 'name'`") unless value && !value.empty?
                        end),

    FastlaneCore::ConfigItem.new(key: :app_name,
                            env_name: "APPCENTER_APP_NAME",
                         description: "App name. If there is no app with such name, you will be prompted to create one",
                            optional: false,
                                type: String,
                        verify_block: proc do |value|
                          UI.user_error!("No App name given, pass using `app_name: 'app name'`") unless value && !value.empty?
                        end),

    FastlaneCore::ConfigItem.new(key: :apk,
                            env_name: "APPCENTER_DISTRIBUTE_APK",
                         description: "Build release path for android build",
                       default_value: Actions.lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH],
                            optional: true,
                                type: String,
                 conflicting_options: [:ipa],
                      conflict_block: proc do |value|
                        UI.user_error!("You can't use 'apk' and '#{value.key}' options in one run")
                      end,
                        verify_block: proc do |value|
                          accepted_formats = [".apk"]
                          UI.user_error!("Only \".apk\" formats are allowed, you provided \"#{File.extname(value)}\"") unless accepted_formats.include? File.extname(value)
                        end),

    FastlaneCore::ConfigItem.new(key: :ipa,
                            env_name: "APPCENTER_DISTRIBUTE_IPA",
                         description: "Build release path for ios build",
                       default_value: Actions.lane_context[SharedValues::IPA_OUTPUT_PATH],
                            optional: true,
                                type: String,
                 conflicting_options: [:apk],
                      conflict_block: proc do |value|
                        UI.user_error!("You can't use 'ipa' and '#{value.key}' options in one run")
                      end,
                        verify_block: proc do |value|
                          accepted_formats = [".ipa"]
                          UI.user_error!("Only \".ipa\" formats are allowed, you provided \"#{File.extname(value)}\"") unless accepted_formats.include? File.extname(value)
                        end),

    FastlaneCore::ConfigItem.new(key: :dsym,
                            env_name: "APPCENTER_DISTRIBUTE_DSYM",
                         description: "Path to your symbols file. For iOS provide path to app.dSYM.zip",
                       default_value: Actions.lane_context[SharedValues::DSYM_OUTPUT_PATH],
                            optional: true,
                                type: String,
                        verify_block: proc do |value|
                          if value
                            UI.user_error!("Couldn't find dSYM file at path '#{value}'") unless File.exist?(value)
                          end
                        end),

    FastlaneCore::ConfigItem.new(key: :upload_dsym_only,
                            env_name: "APPCENTER_DISTRIBUTE_UPLOAD_DSYM_ONLY",
                         description: "Flag to upload only the dSYM file to App Center",
                            optional: true,
                           is_string: false,
                       default_value: false),

    FastlaneCore::ConfigItem.new(key: :group,
                            env_name: "APPCENTER_DISTRIBUTE_GROUP",
                         description: "Distribute group name",
                       default_value: "Collaborators",
                            optional: true,
                                type: String),

    FastlaneCore::ConfigItem.new(key: :release_notes,
                            env_name: "APPCENTER_DISTRIBUTE_RELEASE_NOTES",
                         description: "Release notes",
                       default_value: Actions.lane_context[SharedValues::FL_CHANGELOG] || "No changelog given",
                            optional: true,
                                type: String)
  ]
end

.connection(upload_url = false, dsym = false) ⇒ Object

create request



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 15

def self.connection(upload_url = false, dsym = false)
  require 'faraday'
  require 'faraday_middleware'

  options = {
    url: upload_url ? upload_url : "https://api.appcenter.ms"
  }

  Faraday.new(options) do |builder|
    if upload_url
      builder.request :multipart unless dsym
      builder.request :url_encoded unless dsym
    else
      builder.request :json
    end
    builder.response :json, content_type: /\bjson$/
    builder.use FaradayMiddleware::FollowRedirects
    builder.adapter :net_http
  end
end

.create_dsym_upload(api_token, owner_name, app_name) ⇒ Object

creates new dSYM upload in appcenter returns: symbol_upload_id upload_url expiration_date



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
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 71

def self.create_dsym_upload(api_token, owner_name, app_name)
  connection = self.connection

  response = connection.post do |req|
    req.url("/v0.1/apps/#{owner_name}/#{app_name}/symbol_uploads")
    req.headers['X-API-Token'] = api_token
    req.headers['internal-request-source'] = "fastlane"
    req.body = {
      symbol_type: 'Apple'
    }
  end

  case response.status
  when 200...300
    UI.message("DEBUG: #{JSON.pretty_generate(response.body)}\n") if ENV['DEBUG']
    response.body
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  when 404
    UI.error("Not found, invalid owner or application name")
    false
  else
    UI.error("Error #{response.status}: #{response.body}")
    false
  end
end

.create_release_upload(api_token, owner_name, app_name) ⇒ Object

creates new release upload returns: upload_id upload_url



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
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 40

def self.create_release_upload(api_token, owner_name, app_name)
  connection = self.connection

  response = connection.post do |req|
    req.url("/v0.1/apps/#{owner_name}/#{app_name}/release_uploads")
    req.headers['X-API-Token'] = api_token
    req.headers['internal-request-source'] = "fastlane"
    req.body = {}
  end

  case response.status
  when 200...300
    UI.message("DEBUG: #{JSON.pretty_generate(response.body)}\n") if ENV['DEBUG']
    response.body
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  when 404
    UI.error("Not found, invalid owner or application name")
    false
  else
    UI.error("Error #{response.status}: #{response.body}")
    false
  end
end

.descriptionObject



406
407
408
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 406

def self.description
  "Distribute new release to App Center"
end

.detailsObject



414
415
416
417
418
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 414

def self.details
  [
    "Symbols will also be uploaded automatically if a `app.dSYM.zip` file is found next to `app.ipa`. In case it is located in a different place you can specify the path explicitly in `:dsym` parameter."
  ]
end

.example_codeObject



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 525

def self.example_code
  [
    'appcenter_upload(
      api_token: "...",
      owner_name: "appcenter_owner",
      app_name: "testing_app",
      apk: "./app-release.apk",
      group: "Testers",
      release_notes: "release notes"
    )',
    'appcenter_upload(
      api_token: "...",
      owner_name: "appcenter_owner",
      app_name: "testing_app",
      apk: "./app-release.ipa",
      group: "Testers",
      dsym: "./app.dSYM.zip",
      release_notes: "release notes"
    )'
  ]
end

.get_app(api_token, owner_name, app_name) ⇒ Object

returns true if app exists, false in case of 404 and error otherwise



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 322

def self.get_app(api_token, owner_name, app_name)
  connection = self.connection

  response = connection.get do |req|
    req.url("/v0.1/apps/#{owner_name}/#{app_name}")
    req.headers['X-API-Token'] = api_token
    req.headers['internal-request-source'] = "fastlane"
  end

  case response.status
  when 200...300
    UI.message("DEBUG: #{JSON.pretty_generate(response.body)}\n") if ENV['DEBUG']
    true
  when 404
    UI.message("DEBUG: #{JSON.pretty_generate(response.body)}\n") if ENV['DEBUG']
    false
  else
    UI.error("Error #{response.status}: #{response.body}")
    false
  end
end

.get_or_create_app(params) ⇒ Object

checks app existance, if ther is no such - creates it



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 345

def self.get_or_create_app(params)
  api_token = params[:api_token]
  owner_name = params[:owner_name]
  app_name = params[:app_name]

  platforms = {
    "Android" => ['Java', 'React-Native', 'Xamarin'],
    "iOS" => ['Objective-C-Swift', 'React-Native', 'Xamarin']
  }

  if self.get_app(api_token, owner_name, app_name)
    true
  else
    if Helper.test? || UI.confirm("App with name #{app_name} not found, create one?")
      connection = self.connection

      os = Helper.test? ? "Android" : UI.select("Select OS", ["Android", "iOS"])
      platform = Helper.test? ? "Java" : UI.select("Select Platform", platforms[os])

      response = connection.post do |req|
        req.url("/v0.1/apps")
        req.headers['X-API-Token'] = api_token
        req.headers['internal-request-source'] = "fastlane"
        req.body = {
          "display_name" => app_name,
          "name" => app_name,
          "os" => os,
          "platform" => platform
        }
      end

      case response.status
      when 200...300
        created = response.body
        UI.message("DEBUG: #{JSON.pretty_generate(created)}") if ENV['DEBUG']
        UI.success("Created #{os}/#{platform} app with name \"#{created['name']}\"")
        true
      else
        UI.error("Error creating app #{response.status}: #{response.body}")
        false
      end
    else
      UI.error("Lane aborted")
      false
    end
  end
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


521
522
523
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 521

def self.is_supported?(platform)
  true
end

.outputObject



514
515
516
517
518
519
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 514

def self.output
  [
    ['APPCENTER_DOWNLOAD_LINK', 'The newly generated download link for this build'],
    ['APPCENTER_BUILD_INFORMATION', 'contains all keys/values from the App Center API']
  ]
end

.run(params) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 393

def self.run(params)
  values = params.values
  upload_dsym_only = params[:upload_dsym_only]

  # if app found or successfully created
  if self.get_or_create_app(params)
    self.run_release_upload(params) unless upload_dsym_only
    self.run_dsym_upload(params)
  end

  return values if Helper.test?
end

.run_dsym_upload(params) ⇒ Object

run whole upload process for dSYM files



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 236

def self.run_dsym_upload(params)
  values = params.values
  api_token = params[:api_token]
  owner_name = params[:owner_name]
  app_name = params[:app_name]
  file = params[:ipa]
  dsym = params[:dsym]

  dsym_path = nil
  if dsym
    # we can use dsym parameter only if build file is ipa
    dsym_path = dsym if !file || File.extname(file) == '.ipa'
  else
    # if dsym is note set, but build is ipa - check default path
    if file && File.exist?(file) && File.extname(file) == '.ipa'
      dsym_path = file.to_s.gsub('.ipa', '.dSYM.zip')
      UI.message("dSYM is found")
    end
  end

  # if we provided valid dsym path, or <ipa_path>.dSYM.zip was found, start dSYM upload
  if dsym_path && File.exist?(dsym_path)
    if File.directory?(dsym_path)
      UI.message("dSYM path is folder, zipping...")
      dsym_path = Actions::ZipAction.run(path: dsym, output_path: dsym + ".zip")
      UI.message("dSYM files zipped")
    end

    values[:dsym_path] = dsym_path

    UI.message("Starting dSYM upload...")
    dsym_upload_details = self.create_dsym_upload(api_token, owner_name, app_name)

    if dsym_upload_details
      symbol_upload_id = dsym_upload_details['symbol_upload_id']
      upload_url = dsym_upload_details['upload_url']

      UI.message("Uploading dSYM...")
      self.upload_dsym(api_token, owner_name, app_name, dsym_path, symbol_upload_id, upload_url)
    end
  end
end

.run_release_upload(params) ⇒ Object

run whole upload process for release



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 280

def self.run_release_upload(params)
  api_token = params[:api_token]
  owner_name = params[:owner_name]
  app_name = params[:app_name]
  group = params[:group]
  release_notes = params[:release_notes]

  if release_notes.length >= Constants::MAX_RELEASE_NOTES_LENGTH
    clip = UI.confirm("The release notes are limited to #{Constants::MAX_RELEASE_NOTES_LENGTH} characters, proceeding will clip them. Proceed anyway?")
    UI.abort_with_message!("Upload aborted, please edit your release notes") unless clip
    release_notes_link = UI.input("Provide a link for additional release notes, leave blank to skip")
    read_more = "..." + (release_notes_link.to_s.empty? ? "" : "\n\n[read more](#{release_notes_link})")
    release_notes = release_notes[0, Constants::MAX_RELEASE_NOTES_LENGTH - read_more.length] + read_more
    UI.message("Release notes clipped")
  end

  file = [
    params[:ipa],
    params[:apk]
  ].detect { |e| !e.to_s.empty? }

  UI.user_error!("Couldn't find build file at path '#{file}'") unless file && File.exist?(file)
  UI.user_error!("No Distribute Group given, pass using `group: 'group name'`") unless group && !group.empty?

  UI.message("Starting release upload...")
  upload_details = self.create_release_upload(api_token, owner_name, app_name)
  if upload_details
    upload_id = upload_details['upload_id']
    upload_url = upload_details['upload_url']

    UI.message("Uploading release binary...")
    uploaded = self.upload_build(api_token, owner_name, app_name, file, upload_id, upload_url)

    if uploaded
      release_url = uploaded['release_url']
      UI.message("Release committed")
      self.add_to_group(api_token, release_url, group, release_notes)
    end
  end
end

.update_dsym_upload(api_token, owner_name, app_name, symbol_upload_id, status) ⇒ Object

committs or aborts dsym upload



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 100

def self.update_dsym_upload(api_token, owner_name, app_name, symbol_upload_id, status)
  connection = self.connection

  response = connection.patch do |req|
    req.url("/v0.1/apps/#{owner_name}/#{app_name}/symbol_uploads/#{symbol_upload_id}")
    req.headers['X-API-Token'] = api_token
    req.headers['internal-request-source'] = "fastlane"
    req.body = {
      "status" => status
    }
  end

  case response.status
  when 200...300
    UI.message("DEBUG: #{JSON.pretty_generate(response.body)}\n") if ENV['DEBUG']
    response.body
  else
    UI.error("Error #{response.status}: #{response.body}")
    false
  end
end

.update_release_upload(api_token, owner_name, app_name, upload_id, status) ⇒ Object

Commits or aborts the upload process for a release



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 176

def self.update_release_upload(api_token, owner_name, app_name, upload_id, status)
  connection = self.connection

  response = connection.patch do |req|
    req.url("/v0.1/apps/#{owner_name}/#{app_name}/release_uploads/#{upload_id}")
    req.headers['X-API-Token'] = api_token
    req.headers['internal-request-source'] = "fastlane"
    req.body = {
      "status" => status
    }
  end

  case response.status
  when 200...300
    UI.message("DEBUG: #{JSON.pretty_generate(response.body)}\n") if ENV['DEBUG']
    response.body
  else
    UI.error("Error #{response.status}: #{response.body}")
    false
  end
end

.upload_build(api_token, owner_name, app_name, file, upload_id, upload_url) ⇒ Object

upload binary for specified upload_url if succeed, then commits the release otherwise aborts



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 150

def self.upload_build(api_token, owner_name, app_name, file, upload_id, upload_url)
  connection = self.connection(upload_url)

  options = {}
  options[:upload_id] = upload_id
  # ipa field is used both for .apk and .ipa files
  options[:ipa] = Faraday::UploadIO.new(file, 'application/octet-stream') if file && File.exist?(file)

  response = connection.post do |req|
    req.headers['internal-request-source'] = "fastlane"
    req.body = options
  end

  case response.status
  when 200...300
    UI.message("Binary uploaded")
    self.update_release_upload(api_token, owner_name, app_name, upload_id, 'committed')
  else
    UI.error("Error uploading binary #{response.status}: #{response.body}")
    self.update_release_upload(api_token, owner_name, app_name, upload_id, 'aborted')
    UI.error("Release aborted")
    false
  end
end

.upload_dsym(api_token, owner_name, app_name, dsym, symbol_upload_id, upload_url) ⇒ Object

upload dSYM files to specified upload url if succeed, then commits the upload otherwise aborts



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/fastlane/plugin/appcenter/actions/appcenter_upload_action.rb', line 125

def self.upload_dsym(api_token, owner_name, app_name, dsym, symbol_upload_id, upload_url)
  connection = self.connection(upload_url, true)

  response = connection.put do |req|
    req.headers['x-ms-blob-type'] = "BlockBlob"
    req.headers['Content-Length'] = File.size(dsym).to_s
    req.headers['internal-request-source'] = "fastlane"
    req.body = Faraday::UploadIO.new(dsym, 'application/octet-stream') if dsym && File.exist?(dsym)
  end

  case response.status
  when 200...300
    self.update_dsym_upload(api_token, owner_name, app_name, symbol_upload_id, 'committed')
    UI.success("dSYM uploaded")
  else
    UI.error("Error uploading dSYM #{response.status}: #{response.body}")
    self.update_dsym_upload(api_token, owner_name, app_name, symbol_upload_id, 'aborted')
    UI.error("dSYM upload aborted")
    false
  end
end