Class: Fastlane::Helper::MsDevCenterHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb

Class Method Summary collapse

Class Method Details

.acquire_authorization_token(tenant_id, client_id, client_secret, timeout = 0) ⇒ Object



233
234
235
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
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 233

def self.acquire_authorization_token(tenant_id, client_id, client_secret, timeout = 0)
  body = {
    client_id: client_id,
    client_secret: client_secret,
    grant_type: "client_credentials",
    resource: HOST
  }
  headers = {
    "Content-Type": "application/x-www-form-urlencoded"
  }.merge(REQUEST_HEADERS)
  connection = Faraday.new("https://login.microsoftonline.com")

  begin
    response = connection.post("/#{tenant_id}/oauth2/token") do |req|
      req.headers = headers
      req.body = body
      req.options.timeout = timeout if timeout.positive?
    end
    data = JSON.parse(response.body)

    return data["access_token"] if response.status == 200

    error = data["error"]
    error_description = data["error_description"]

    UI.user_error!("Authorization request returned the error.\nCode: #{error}.\nDescription: #{error_description}")
  rescue StandardError => ex
    UI.user_error!("Authorization failed: #{ex}")
  end
end

.commit_submission(app_id, flight_id, submission_id, auth_token, timeout = 0) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 113

def self.commit_submission(app_id, flight_id, submission_id, auth_token, timeout = 0)
  check_app_id(app_id)
  check_submission_id(submission_id)

  is_flight = !flight_id.nil? && !flight_id.empty?
  check_flight_id(flight_id) if is_flight

  connection = Faraday.new(HOST)
  url = build_url_root(app_id)
  url += "/flights/#{flight_id}" if is_flight
  url += "/submissions/#{submission_id}/commit"

  begin
    response = connection.post(url) do |req|
      req.headers = build_headers(auth_token)
      req.options.timeout = timeout if timeout.positive?
    end

    UI.user_error!("Committing submission request returned the error.\nCode: #{response.status}") unless response.status == 202
  rescue StandardError => ex
    UI.user_error!("Committing submission process failed: #{ex}")
  end
end

.create_flight(app_id, friendly_name, group_ids, auth_token, timeout = 0) ⇒ Object



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
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 203

def self.create_flight(app_id, friendly_name, group_ids, auth_token, timeout = 0)
  check_app_id(app_id)

  friendly_name = !friendly_name.nil? && !friendly_name.empty? ? friendly_name : "Fastlane Sapfire Flight"
  body = {
    friendlyName: friendly_name,
    groupIds: group_ids
  }
  connection = Faraday.new(HOST)
  url = "#{build_url_root(app_id)}/flights"

  begin
    response = connection.post(url) do |req|
      req.headers = build_headers(auth_token)
      req.body = body.to_json
      req.options.timeout = timeout if timeout.positive?
    end
    data = JSON.parse(response.body)

    return data if response.status == 201

    code = data["code"]
    message = data["message"]

    UI.user_error!("Creating flight request returned the error.\nCode: #{response.status} #{code}.\nDescription: #{message}")
  rescue StandardError => ex
    UI.user_error!("Creating flight process failed: #{ex}")
  end
end

.create_submission(app_id, auth_token, timeout = 0) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 62

def self.create_submission(app_id, auth_token, timeout = 0)
  check_app_id(app_id)

  connection = Faraday.new(HOST)
  url = "#{build_url_root(app_id)}/submissions"

  begin
    response = connection.post(url) do |req|
      req.headers = build_headers(auth_token)
      req.options.timeout = timeout if timeout.positive?
    end
    data = JSON.parse(response.body)

    return data if response.status == 201

    code = data["code"]
    message = data["message"]

    UI.user_error!("Creating submission request returned the error.\nCode: #{response.status} #{code}.\nDescription: #{message}")
  rescue StandardError => ex
    UI.user_error!("Creating submission process failed: #{ex}")
  end
end

.get_app_info(app_id, auth_token, timeout = 0) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 11

def self.get_app_info(app_id, auth_token, timeout = 0)
  check_app_id(app_id)

  connection = Faraday.new(HOST)
  url = build_url_root(app_id)

  begin
    response = connection.get(url) do |req|
      req.headers = build_headers(auth_token)
      req.options.timeout = timeout if timeout.positive?
    end
    data = JSON.parse(response.body)

    return data if response.status == 200

    UI.user_error!("Getting app request returned the error.\nCode: #{response.status}")
  rescue StandardError => ex
    UI.user_error!("Getting app info process failed: #{ex}")
  end
end

.get_submission(app_id, flight_id, submission_id, auth_token, timeout = 0) ⇒ Object



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

def self.get_submission(app_id, flight_id, submission_id, auth_token, timeout = 0)
  check_app_id(app_id)
  check_submission_id(submission_id)

  is_flight = !flight_id.nil? && !flight_id.empty?
  check_flight_id(flight_id) if is_flight

  connection = Faraday.new(HOST)
  url = build_url_root(app_id)
  url += "/flights/#{flight_id}" if is_flight
  url += "/submissions/#{submission_id}"

  begin
    response = connection.get(url) do |req|
      req.headers = build_headers(auth_token)
      req.options.timeout = timeout if timeout.positive?
    end
    data = JSON.parse(response.body)

    return data if response.status == 200

    code = data["code"]
    message = data["message"]

    UI.user_error!("Getting flight submission request returned the error.\nCode: #{response.status} #{code}.\nDescription: #{message}")
  rescue StandardError => ex
    UI.user_error!("Getting flight submission process failed: #{ex}")
  end
end

.get_submission_status(app_id, flight_id, submission_id, auth_token, timeout = 0) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 156

def self.get_submission_status(app_id, flight_id, submission_id, auth_token, timeout = 0)
  check_app_id(app_id)
  check_flight_id(flight_id) if !flight_id.nil? && !flight_id.empty?
  check_submission_id(submission_id)

  response = get_submission_status_internal(app_id, flight_id, submission_id, auth_token, timeout)

  # Sometimes MS can return internal server error code (500) that is not directly related to uploading process.
  # Once it happens, retry 3 times until we'll get a success response.
  if response[:status] == 500
    server_error_500_retry_counter = 0

    until server_error_500_retry_counter < 2
      server_error_500_retry_counter += 1
      response = get_submission_status_internal(app_id, flight_id, submission_id, auth_token, timeout)
      break if response.nil? || response[:status] == 200
    end
  end

  return response[:data] if !response.nil? && response[:status] == 200

  UI.user_error!("Submission status obtaining request returned the error.\nCode: #{response[:status]}")
end

.remove_submission(app_id, submission_id, auth_token, timeout = 0) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 137

def self.remove_submission(app_id, submission_id, auth_token, timeout = 0)
  check_app_id(app_id)
  check_submission_id(submission_id)

  connection = Faraday.new(HOST)
  url = "#{build_url_root(app_id)}/submissions/#{submission_id}"

  begin
    response = connection.delete(url) do |req|
      req.headers = build_headers(auth_token)
      req.options.timeout = timeout if timeout.positive?
    end

    UI.user_error!("Deleting submission request returned the error.\nCode: #{response.status}") unless response.status == 204
  rescue StandardError => ex
    UI.user_error!("Deleting submission process failed: #{ex}")
  end
end

.update_submission(app_id, submission_obj, auth_token, timeout = 0) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 86

def self.update_submission(app_id, submission_obj, auth_token, timeout = 0)
  check_app_id(app_id)
  UI.user_error!("Submission data object need to be provided") if submission_obj.nil?

  connection = Faraday.new(HOST)
  flight_id = submission_obj["flightId"]
  submission_id = submission_obj["id"]
  url = build_url_root(app_id)
  url += "/flights/#{flight_id}" if !flight_id.nil? && !flight_id.empty?
  url += "/submissions/#{submission_id}"

  begin
    response = connection.put(url) do |req|
      req.headers = build_headers(auth_token)
      req.body = submission_obj.to_json
      req.options.timeout = timeout if timeout.positive?
    end
    data = JSON.parse(response.body)

    return data if response.status == 200

    UI.user_error!("Updating submission request returned the error.\nCode: #{response.status}")
  rescue StandardError => ex
    UI.user_error!("Updating submission process failed: #{ex}")
  end
end