Class: Fastlane::Helper::AppcenterHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb

Class Method Summary collapse

Class Method Details

.add_to_destination(api_token, owner_name, app_name, release_id, destination_type, destination_id, mandatory_update = false, notify_testers = false) ⇒ Object

add release to distribution group or store



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 375

def self.add_to_destination(api_token, owner_name, app_name, release_id, destination_type, destination_id, mandatory_update = false, notify_testers = false)
  connection = self.connection

  UI.message("DEBUG: getting #{release_id}") if ENV['DEBUG']

  body = { "id" => destination_id }
  if destination_type == "group"
    body["mandatory_update"] = mandatory_update
    body["notify_testers"] = notify_testers
  end

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

  case response.status
  when 200...300
    # get full release info
    release = self.get_release(api_token, owner_name, app_name, release_id)
    return false unless release

    download_url = release['download_url']

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

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

    UI.message("Release '#{release_id}' (#{release['short_version']}) was successfully distributed'")

    release
  when 404
    UI.error("Not found, invalid distribution #{destination_type} name")
    false
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  else
    UI.error("Error adding to #{destination_type} #{response.status}: #{response.body}")
    false
  end
end

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

create request



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 17

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

  options = {
    url: upload_url || ENV.fetch('APPCENTER_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$/ unless csv
    builder.use FaradayMiddleware::FollowRedirects
    builder.adapter :net_http
  end
end

.create_app(api_token, owner_type, owner_name, app_name, app_display_name, os, platform) ⇒ Object

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



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
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 446

def self.create_app(api_token, owner_type, owner_name, app_name, app_display_name, os, platform)
  connection = self.connection

  endpoint = owner_type == "user" ? "v0.1/apps" : "v0.1/orgs/#{owner_name}/apps"

  response = connection.post(endpoint) do |req|
    req.headers['X-API-Token'] = api_token
    req.headers['internal-request-source'] = "fastlane"
    req.body = {
      "display_name" => app_display_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']}\" and display name \"#{created['display_name']}\" for #{owner_type} \"#{owner_name}\"")
    true
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  else
    UI.error("Error creating app #{response.status}: #{response.body}")
    false
  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



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
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 109

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

  response = connection.post("v0.1/apps/#{owner_name}/#{app_name}/symbol_uploads") do |req|
    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_mapping_upload(api_token, owner_name, app_name, file_name, build_number, version) ⇒ Object

creates new mapping upload in appcenter returns: symbol_upload_id upload_url expiration_date



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
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 74

def self.create_mapping_upload(api_token, owner_name, app_name, file_name, build_number, version)
  connection = self.connection

  response = connection.post("v0.1/apps/#{owner_name}/#{app_name}/symbol_uploads") do |req|
    req.headers['X-API-Token'] = api_token
    req.headers['internal-request-source'] = "fastlane"
    req.body = {
      symbol_type: "AndroidProguard",
      file_name: file_name,
      build: build_number,
      version: version,
    }
  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, body) ⇒ Object

creates new release upload returns: upload_id upload_url



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
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 42

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

  response = connection.post("v0.1/apps/#{owner_name}/#{app_name}/release_uploads") do |req|
    req.headers['X-API-Token'] = api_token
    req.headers['internal-request-source'] = "fastlane"
    req.body = body.nil? && {} || 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
  when 500...600
    UI.crash!("Internal Service Error, please try again later")
  else
    UI.error("Error #{response.status}: #{response.body}")
    false
  end
end

.fetch_devices(api_token:, owner_name:, app_name:, distribution_group:) ⇒ Object



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 503

def self.fetch_devices(api_token:, owner_name:, app_name:, distribution_group:)
  connection = self.connection(nil, false, true)

  endpoint = "/v0.1/apps/#{owner_name}/#{app_name}/distribution_groups/#{ERB::Util.url_encode(distribution_group)}/devices/download_devices_list"

  response = connection.get(endpoint) do |req|
    req.headers['X-API-Token'] = api_token
    req.headers['internal-request-source'] = "fastlane"
  end

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

.fetch_distribution_groups(api_token:, owner_name:, app_name:) ⇒ Object



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
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 477

def self.fetch_distribution_groups(api_token:, owner_name:, app_name:)
  connection = self.connection

  endpoint = "/v0.1/apps/#{owner_name}/#{app_name}/distribution_groups"

  response = connection.get(endpoint) do |req|
    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']
    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

.file_extname_full(path) ⇒ Object

basic utility method to check file types that App Center will accept, accounting for file types that can and should be zip-compressed before they are uploaded



8
9
10
11
12
13
14
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 8

def self.file_extname_full(path)
  %w(.app.zip .dSYM.zip).each do |suffix|
    return suffix if path.to_s.downcase.end_with? suffix.downcase
  end

  File.extname path
end

.get_app(api_token, owner_name, app_name) ⇒ Object

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



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 421

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

  response = connection.get("v0.1/apps/#{owner_name}/#{app_name}") do |req|
    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
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  else
    UI.error("Error getting app #{owner_name}/#{app_name}, #{response.status}: #{response.body}")
    false
  end
end

.get_destination(api_token, owner_name, app_name, destination_type, destination_name) ⇒ Object

get distribution group or store



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 277

def self.get_destination(api_token, owner_name, app_name, destination_type, destination_name)
  connection = self.connection

  response = connection.get("v0.1/apps/#{owner_name}/#{app_name}/distribution_#{destination_type}s/#{ERB::Util.url_encode(destination_name)}") do |req|
    req.headers['X-API-Token'] = api_token
    req.headers['internal-request-source'] = "fastlane"
  end

  case response.status
  when 200...300
    destination = response.body
    UI.message("DEBUG: received #{destination_type} #{JSON.pretty_generate(destination)}") if ENV['DEBUG']
    destination
  when 404
    UI.error("Not found, invalid distribution #{destination_type} name")
    false
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  else
    UI.error("Error getting #{destination_type} #{response.status}: #{response.body}")
    false
  end
end

.get_install_url(owner_type, owner_name, app_name) ⇒ Object

Note: This does not support testing environment (INT)



536
537
538
539
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 536

def self.get_install_url(owner_type, owner_name, app_name)
  owner_path = owner_type == "user" ? "users/#{owner_name}" : "orgs/#{owner_name}"
  return "https://install.appcenter.ms/#{owner_path}/apps/#{app_name}"
end

.get_release(api_token, owner_name, app_name, release_id) ⇒ Object

get existing release



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 252

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

  case response.status
  when 200...300
    release = response.body
    UI.message("DEBUG: #{JSON.pretty_generate(release)}") if ENV['DEBUG']
    release
  when 404
    UI.error("Not found, invalid release url")
    false
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  else
    UI.error("Error fetching information about release #{response.status}: #{response.body}")
    false
  end
end

.get_release_url(owner_type, owner_name, app_name, release_id) ⇒ Object

Note: This does not support testing environment (INT)



530
531
532
533
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 530

def self.get_release_url(owner_type, owner_name, app_name, release_id)
  owner_path = owner_type == "user" ? "users/#{owner_name}" : "orgs/#{owner_name}"
  return "https://appcenter.ms/#{owner_path}/apps/#{app_name}/distribute/releases/#{release_id}"
end

.update_release(api_token, owner_name, app_name, release_id, release_notes = '') ⇒ Object

add release to destination



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 303

def self.update_release(api_token, owner_name, app_name, release_id, release_notes = '')
  connection = self.connection

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

  case response.status
  when 200...300
    # get full release info
    release = self.get_release(api_token, owner_name, app_name, release_id)
    return false unless release

    download_url = release['download_url']

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

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

    UI.message("Release '#{release_id}' (#{release['short_version']}) was successfully updated")

    release
  when 404
    UI.error("Not found, invalid release id")
    false
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  else
    UI.error("Error adding updating release #{response.status}: #{response.body}")
    false
  end
end

.update_release_metadata(api_token, owner_name, app_name, release_id, dsa_signature) ⇒ Object

updates release metadata



343
344
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
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 343

def self.(api_token, owner_name, app_name, release_id, dsa_signature)
  return if dsa_signature.to_s == ''

   = {
    dsa_signature: dsa_signature
  }

  connection = self.connection
  response = connection.patch("v0.1/apps/#{owner_name}/#{app_name}/releases/#{release_id}") do |req|
    req.headers['X-API-Token'] = api_token
    req.headers['internal-request-source'] = "fastlane"
    req.body = {
      metadata: 
    }
  end

  case response.status
  when 200...300
    UI.message("Release Metadata was successfully updated for release '#{release_id}'")
  when 404
    UI.error("Not found, invalid release id")
    false
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  else
    UI.error("Error adding updating release metadata #{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



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 225

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

  response = connection.patch("v0.1/apps/#{owner_name}/#{app_name}/release_uploads/#{upload_id}") do |req|
    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
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  when 500...600
    UI.crash!("Internal Service Error, please try again later")
  else
    UI.error("Error #{response.status}: #{response.body}")
    false
  end
end

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

commits or aborts symbol upload



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 137

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

  response = connection.patch("v0.1/apps/#{owner_name}/#{app_name}/symbol_uploads/#{symbol_upload_id}") do |req|
    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
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  else
    UI.error("Error #{response.status}: #{response.body}")
    false
  end
end

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

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



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
221
222
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 195

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

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

  response = connection.post do |req|
    req.options.timeout = timeout
    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')
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  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_symbol(api_token, owner_name, app_name, symbol, symbol_type, symbol_upload_id, upload_url) ⇒ Object

upload symbol (dSYM or mapping) files to specified upload url if succeed, then commits the upload otherwise aborts



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
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 164

def self.upload_symbol(api_token, owner_name, app_name, symbol, symbol_type, 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(symbol).to_s
    req.headers['internal-request-source'] = "fastlane"
    req.body = Faraday::UploadIO.new(symbol, 'application/octet-stream') if symbol && File.exist?(symbol)
  end

  logType = "dSYM" if (symbol_type == "Apple")
  logType = "mapping" if (symbol_type == "Android")

  case response.status
  when 200...300
    self.update_symbol_upload(api_token, owner_name, app_name, symbol_upload_id, 'committed')
    UI.success("#{logType} uploaded")
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  else
    UI.error("Error uploading #{logType} #{response.status}: #{response.body}")
    self.update_symbol_upload(api_token, owner_name, app_name, symbol_upload_id, 'aborted')
    UI.error("#{logType} upload aborted")
    false
  end
end