Class: Fastlane::Helper::AppcenterHelper

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

Constant Summary collapse

RELEASE_UPLOAD_STATUS_POLL_INTERVAL =

Time to wait between 2 status polls in seconds

1
MAX_REQUEST_RETRIES =

Maximum number of retries for a request

2
REQUEST_RETRY_INTERVAL =

Delay between retries in seconds

5

Class Method Summary collapse

Class Method Details

.add_new_app_to_distribution_group(api_token:, owner_name:, app_name:, destination_name:) ⇒ Object

add new created app to existing distribution group



958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 958

def self.add_new_app_to_distribution_group(api_token:, owner_name:, app_name:, destination_name:)
  url = URI::Parser.new.escape("/v0.1/orgs/#{owner_name}/distribution_groups/#{destination_name}/apps")
  body = {
    apps: [
      { name: app_name }
    ]
  }

  UI.message("DEBUG: POST #{url}") if ENV['DEBUG']
  UI.message("DEBUG: POST body #{JSON.pretty_generate(body)}\n") if ENV['DEBUG']

  status, message, response = retry_429_and_error do 
    response = connection.post(url) do |req|
      req.headers['X-API-Token'] = api_token
      req.headers['internal-request-source'] = "fastlane"
      req.body = body
    end
  end

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http adding to distribution group: #{message}")
    else
      UI.error("Retryable error adding to distribution group: #{status}: #{message}")
    end
  when 200...300
    response.body
    UI.success("Added new app #{app_name} to distribution group #{destination_name}")
  when 401
    UI.user_error!("Auth Error, provided invalid token")
  when 404
    UI.error("Not found, invalid distribution group name #{destination_name}")
  when 409
    UI.success("App already added to distribution group #{destination_name}")
  else
    UI.error("Error adding app to distribution group #{response.status}: #{response.body}")
  end
end

.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



692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 692

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

  url = "v0.1/apps/#{owner_name}/#{app_name}/releases/#{release_id}/#{destination_type}s"
  body = {
    id: destination_id
  }

  if destination_type == "group"
    body["mandatory_update"] = mandatory_update
    body["notify_testers"] = notify_testers
  end

  UI.message("DEBUG: POST #{url}") if ENV['DEBUG']
  UI.message("DEBUG: POST body #{JSON.pretty_generate(body)}\n") if ENV['DEBUG']

  status, message, response = retry_429_and_error do 
    response = connection.post(url) do |req|
      req.headers['X-API-Token'] = api_token
      req.headers['internal-request-source'] = "fastlane"
      req.body = body
    end
  end

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception adding to destination: #{message}")
    else
      UI.error("Retryable error adding to destination: #{status}: #{message}")
    end
    false
  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']

    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



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

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

  default_api_url = "https://api.appcenter.ms"
  if ENV['APPCENTER_ENV']&.upcase == 'INT'
    default_api_url = "https://api-gateway-core-integration.dev.avalanch.es"
  end
  options = {
    url: upload_url || default_api_url
  }

  UI.message("DEBUG: BASE URL #{options[:url]}") if ENV['DEBUG']

  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



788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 788

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

  url = owner_type == "user" ? "v0.1/apps" : "v0.1/orgs/#{owner_name}/apps"
  body = {
    display_name: app_display_name,
    name: app_name,
    os: os,
    platform: platform
  }

  UI.message("DEBUG: POST #{url}") if ENV['DEBUG']
  UI.message("DEBUG: POST body #{JSON.pretty_generate(body)}\n") if ENV['DEBUG']

  status, message, response = retry_429_and_error do 
    response = connection.post(url) do |req|
      req.headers['X-API-Token'] = api_token
      req.headers['internal-request-source'] = "fastlane"
      req.body = body
    end
  end

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception creating app: #{message}")
    else
      UI.error("Retryable error creating app: #{status}: #{message}")
    end
    false
  when 200...300
    created = response.body
    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



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

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

  url = "v0.1/apps/#{owner_name}/#{app_name}/symbol_uploads"
  body = {
    symbol_type: 'Apple'
  }

  UI.message("DEBUG: POST #{url}") if ENV['DEBUG']
  UI.message("DEBUG: POST body #{JSON.pretty_generate(body)}\n") if ENV['DEBUG']

  status, message, response = retry_429_and_error do 
    response = connection.post(url) do |req|
      req.headers['X-API-Token'] = api_token
      req.headers['internal-request-source'] = "fastlane"
      req.body = body
    end
  end

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception creating dsym upload: #{message}")
    else
      UI.error("Retryable error creating dsym upload #{status}: #{message}")
    end
    false
  when 200...300
    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



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
144
145
146
147
148
149
150
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 108

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

  url = "v0.1/apps/#{owner_name}/#{app_name}/symbol_uploads"
  body = {
    symbol_type: "AndroidProguard",
    file_name: file_name,
    build: build_number,
    version: version,
  }

  UI.message("DEBUG: POST #{url}") if ENV['DEBUG']
  UI.message("DEBUG: POST body #{JSON.pretty_generate(body)}\n") if ENV['DEBUG']

  status, message, response = retry_429_and_error do 
    response = connection.post(url) do |req|
      req.headers['X-API-Token'] = api_token
      req.headers['internal-request-source'] = "fastlane"
      req.body = body
    end
  end

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception creating mapping upload: #{message}")
    else
      UI.error("Retryable error creating mapping upload #{status}: #{message}")
    end
    false
  when 200...300
    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



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

def self.create_release_upload(api_token, owner_name, app_name, body)
  connection = self.connection
  url = "v0.1/apps/#{owner_name}/#{app_name}/uploads/releases"
  body ||= {}

  UI.message("DEBUG: POST #{url}") if ENV['DEBUG']
  UI.message("DEBUG: POST body: #{JSON.pretty_generate(body)}\n") if ENV['DEBUG']

  status, message, response = retry_429_and_error do 
    response = connection.post(url) do |req|
      req.headers['X-API-Token'] = api_token
      req.headers['internal-request-source'] = "fastlane"
      req.body = body
    end
  end

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception creating release upload: #{message}")
    else
      UI.error("Retryable error creating release upload #{status}: #{message}")
    end
    false
  when 200...300
    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.abort_with_message!("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



867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 867

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

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

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

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

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http fetching devices: #{message}")
    else
      UI.error("Retryable error fetching devices: #{status}: #{message}")
    end
    false
  when 200...300
    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



831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 831

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

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

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

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

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http fetching destribution groups: #{message}")
    else
      UI.error("Retryable error fetching destribution groups: #{status}: #{message}")
    end
    false
  when 200...300
    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

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



903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 903

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

  url = "/v0.1/apps/#{owner_name}/#{app_name}/releases"

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

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

 case status
 when 0, 429
   if status == 0
     UI.error("Faraday http fetching releases: #{message}")
   else
     UI.error("Retryable error fetching releases: #{status}: #{message}")
   end
   false
 when 200...300
   JSON.parse(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



23
24
25
26
27
28
29
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 23

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

.finish_release_upload(finish_url, api_token, owner_name, app_name, upload_id, timeout) ⇒ Object

Verifies a successful upload to App Center returns: successful upload response body.



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 325

def self.finish_release_upload(finish_url, api_token, owner_name, app_name, upload_id, timeout)
  connection = self.connection(finish_url)

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

  status, message, response = retry_429_and_error do 
    response = connection.post do |req|
      req.options.timeout = timeout
      req.headers['internal-request-source'] = "fastlane"
    end
  end

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception finishing release upload: #{message}")
    else
      UI.error("Retryable error finishing release upload #{status}: #{message}")
    end
    false
  when 200...300
    if response.body['error'] == false
      UI.message("Upload finished")
      self.update_release_upload(api_token, owner_name, app_name, upload_id, 'uploadFinished')
    else
      UI.error("Error finishing upload: #{response.body['message']}")
      false
    end
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  else
    UI.error("Error finishing upload: #{response.status}: #{response.body}")
    false
  end
end

.get_app(api_token, owner_name, app_name) ⇒ Object

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



750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 750

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

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

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

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

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception getting app: #{message}")
    else
      UI.error("Retryable error getting app: #{status}: #{message}")
    end
    false
  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



550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 550

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

  url = "v0.1/apps/#{owner_name}/#{app_name}/distribution_#{destination_type}s/#{ERB::Util.url_encode(destination_name)}"

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

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

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception getting destination: #{message}")
    else
      UI.error("Retryable error getting destination: #{status}: #{message}")
    end
    false
  when 200...300
    destination = response.body
    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



948
949
950
951
952
953
954
955
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 948

def self.get_install_url(owner_type, owner_name, app_name)
  owner_path = owner_type == "user" ? "users/#{owner_name}" : "orgs/#{owner_name}"
  if ENV['APPCENTER_ENV']&.upcase == 'INT'
    return "https://install.portal-server-core-integration.dev.avalanch.es/#{owner_path}/apps/#{app_name}"
  end

  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



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

def self.get_release(api_token, owner_name, app_name, release_id)
  connection = self.connection

  url = "v0.1/apps/#{owner_name}/#{app_name}/releases/#{release_id}"

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

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

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception getting release: #{message}")
    else
      UI.error("Retryable error getting release: #{status}: #{message}")
    end
    false
  when 200...300
    release = response.body
    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



939
940
941
942
943
944
945
946
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 939

def self.get_release_url(owner_type, owner_name, app_name, release_id)
  owner_path = owner_type == "user" ? "users/#{owner_name}" : "orgs/#{owner_name}"
  if ENV['APPCENTER_ENV']&.upcase == 'INT'
    return "https://portal-server-core-integration.dev.avalanch.es/#{owner_path}/apps/#{app_name}/distribute/releases/#{release_id}"
  end

  return "https://appcenter.ms/#{owner_path}/apps/#{app_name}/distribute/releases/#{release_id}"
end

.poll_for_release_id(api_token, url) ⇒ Object

Polls the upload for a release id. When a release is uploaded, we have to check for a successful extraction before we can continue. returns: release_distinct_id



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 511

def self.poll_for_release_id(api_token, url)
  connection = self.connection

  while true
    UI.message("DEBUG: GET #{url}") if ENV['DEBUG']

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

    case status
    when 0, 429
      if status == 0
        UI.error("Faraday http exception polling for release id: #{message}")
      else
        UI.error("Retryable error polling for release id: #{status}: #{message}")
      end
      return false
    when 200...300
      case response.body['upload_status']
      when "readyToBePublished"
        return response.body['release_distinct_id']
      when "error"
        UI.error("Error fetching release: #{response.body['error_details']}")
        return false
      else
        sleep(RELEASE_UPLOAD_STATUS_POLL_INTERVAL)
      end
    else
      UI.error("Error fetching information about release #{response.status}: #{response.body}")
      return false
    end
  end
end

.retry_429_and_error(&block) ⇒ Object



998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 998

def self.retry_429_and_error(&block)
  retries = 0
  status = 0

  # status == 0   - Faraday error
  # status == 429 - retryable error code from server
  while ((status == 0) || (status == 429)) && (retries <= MAX_REQUEST_RETRIES)
    begin
      # calling request sending logic
      response = block.call

      # checking reponse
      status = response.status
      message = response.body
      UI.message("DEBUG: #{status} #{JSON.pretty_generate(message)}\n") if ENV['DEBUG']
    rescue Faraday::Error => e
      status = 0
      message = e.message
    end

    # Pause before retrying
    if (status == 0) || (status == 429)
      sleep(REQUEST_RETRY_INTERVAL)
    end
    
    retries += 1
  end

  return status, message, response
end

.set_release_upload_metadata(set_metadata_url, api_token, owner_name, app_name, upload_id, timeout) ⇒ Object

sets metadata for new upload in App Center returns: chunk size



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

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

  UI.message("DEBUG: POST #{}") if ENV['DEBUG']
  UI.message("DEBUG: POST body <data>\n") if ENV['DEBUG']

  status, message, response = retry_429_and_error do 
    response = connection.post do |req|
      req.options.timeout = timeout
      req.headers['internal-request-source'] = "fastlane"
    end
  end

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception releasing upload metadata: #{message}")
    else
      UI.error("Retryable error releasing upload metadata #{status}: #{message}")
    end
    false
  when 200...300
    chunk_size = response.body['chunk_size']
    unless chunk_size.is_a? Integer
      UI.error("Set metadata didn't return chunk size: #{response.status}: #{response.body}")
      false
    else
      UI.message("Metadata set")
      chunk_size
    end
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  else
    UI.error("Error setting metadata: #{response.status}: #{response.body}")
    false
  end
end

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

add release to destination



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 588

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

  url = "v0.1/apps/#{owner_name}/#{app_name}/releases/#{release_id}"
  body = {
    release_notes: release_notes
  }

  UI.message("DEBUG: PUT #{url}") if ENV['DEBUG']
  UI.message("DEBUG: PUT body #{JSON.pretty_generate(body)}\n") if ENV['DEBUG']

  status, message, response = retry_429_and_error do 
    response = connection.put(url) do |req|
      req.headers['X-API-Token'] = api_token
      req.headers['internal-request-source'] = "fastlane"
      req.body = body
    end
  end

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception updating release: #{message}")
    else
      UI.error("Retryable error updating release: #{status}: #{message}")
    end
    false
  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']

    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 = '', ed_signature = '') ⇒ Object

updates release metadata



641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 641

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

  url = "v0.1/apps/#{owner_name}/#{app_name}/releases/#{release_id}"
  body = {
    metadata: {}
  }
  
  if dsa_signature.to_s != ''
    body[:metadata]["dsa_signature"] = dsa_signature
  end
  if ed_signature.to_s != ''
    body[:metadata]["ed_signature"] = ed_signature
  end

  UI.message("DEBUG: PATCH #{url}") if ENV['DEBUG']
  UI.message("DEBUG: PATCH body #{JSON.pretty_generate(body)}\n") if ENV['DEBUG']

  connection = self.connection

  status, message, response = retry_429_and_error do 
    response = connection.patch(url) do |req|
      req.headers['X-API-Token'] = api_token
      req.headers['internal-request-source'] = "fastlane"
      req.body = body
    end
  end

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception updating release metadata: #{message}")
    else
      UI.error("Retryable error updating release metadata: #{status}: #{message}")
    end
    false
  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



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

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

  url = "v0.1/apps/#{owner_name}/#{app_name}/uploads/releases/#{upload_id}"
  body = {
    upload_status: status,
    id: upload_id
  }

  UI.message("DEBUG: PATCH #{url}") if ENV['DEBUG']
  UI.message("DEBUG: PATCH body #{JSON.pretty_generate(body)}\n") if ENV['DEBUG']

  status, message, response = retry_429_and_error do 
    response = connection.patch(url) do |req|
      req.headers['X-API-Token'] = api_token
      req.headers['internal-request-source'] = "fastlane"
      req.body = body
    end
  end

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception updating release upload: #{message}")
    else
      UI.error("Retryable error updating release upload #{status}: #{message}")
    end
    false
  when 200...300
    response.body
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  when 500...600
    UI.abort_with_message!("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



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

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

  url = "v0.1/apps/#{owner_name}/#{app_name}/symbol_uploads/#{symbol_upload_id}"
  body = {
    status: status
  }

  UI.message("DEBUG: PATCH #{url}") if ENV['DEBUG']
  UI.message("DEBUG: PATCH body #{JSON.pretty_generate(body)}\n") if ENV['DEBUG']

  status, message, response = retry_429_and_error do 
    response = connection.patch(url) do |req|
      req.headers['X-API-Token'] = api_token
      req.headers['internal-request-source'] = "fastlane"
      req.body = body
    end
  end

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception updating symbol upload: #{message}")
    else
      UI.error("Retryable error updating symbol upload #{status}: #{message}")
    end
    false
  when 200...300
    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, content_type, chunk_size, timeout) ⇒ Object

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



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
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
419
420
421
422
423
424
425
# File 'lib/fastlane/plugin/appcenter/helper/appcenter_helper.rb', line 365

def self.upload_build(api_token, owner_name, app_name, file, upload_id, upload_url, content_type, chunk_size, timeout)
  block_number = 1

  File.open(file).each_chunk(chunk_size) do |chunk|
    upload_chunk_url = "#{upload_url}&block_number=#{block_number}"
    retries = 0

    while retries <= MAX_REQUEST_RETRIES
      begin
        connection = self.connection(upload_chunk_url, true)

        UI.message("DEBUG: POST #{upload_chunk_url}") if ENV['DEBUG']
        UI.message("DEBUG: POST body <data>\n") if ENV['DEBUG']
        response = connection.post do |req|
          req.options.timeout = timeout
          req.headers['internal-request-source'] = "fastlane"
          req.headers['Content-Length'] = chunk.length.to_s
          req.headers['Content-Type'] = content_type
          req.body = chunk
        end
        UI.message("DEBUG: #{response.status} #{JSON.pretty_generate(response.body)}\n") if ENV['DEBUG']
        status = response.status
        message = response.body
      rescue Faraday::Error => e

        # Low level HTTP errors, we will retry them
        status = 0
        message = e.message
      end

      case status
      when 200...300
        if response.body['error'] == false
          UI.message("Chunk uploaded")
          block_number += 1
          break
        else
          UI.error("Error uploading binary #{response.body['message']}")
          return false
        end
      when 401
        UI.user_error!("Auth Error, provided invalid token")
        return false
      when 400...407, 409...428, 430...499
        UI.user_error!("Client error: #{response.status}: #{response.body}")
        return false
      else
        if retries < MAX_REQUEST_RETRIES
          UI.message("DEBUG: Retryable error uploading binary #{status}: #{message}")
          retries += 1
          sleep(REQUEST_RETRY_INTERVAL)
        else
          UI.error("Error uploading binary #{status}: #{message}")
          return false
        end
      end
    end
  end
  UI.message("Binary uploaded")
  return true
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



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

def self.upload_symbol(api_token, owner_name, app_name, symbol, symbol_type, symbol_upload_id, upload_url)
  connection = self.connection(upload_url, true)

  UI.message("DEBUG: PUT #{upload_url}") if ENV['DEBUG']
  UI.message("DEBUG: PUT body <data>\n") if ENV['DEBUG']

  log_type = "dSYM" if symbol_type == "Apple"
  log_type = "mapping" if symbol_type == "Android"

  status, message, response = retry_429_and_error do 
    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
  end

  case status
  when 0, 429
    if status == 0
      UI.error("Faraday http exception updating symbol upload: #{message}")
    else
      UI.error("Retryable error updating symbol upload #{status}: #{message}")
    end
    false
  when 200...300
    self.update_symbol_upload(api_token, owner_name, app_name, symbol_upload_id, 'committed')
    UI.success("#{log_type} uploaded")
  when 401
    UI.user_error!("Auth Error, provided invalid token")
    false
  else
    UI.error("Error uploading #{log_type} #{response.status}: #{response.body}")
    self.update_symbol_upload(api_token, owner_name, app_name, symbol_upload_id, 'aborted')
    UI.error("#{log_type} upload aborted")
    false
  end
end