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



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
279
280
281
282
283
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 254

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, submission_id, auth_token, timeout = 0) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 175

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

  connection = Faraday.new(HOST)

  begin
    response = connection.post("/#{API_VERSION}/#{API_ROOT}/#{app_id}/submissions/#{submission_id}/commit") 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_submission(app_id, auth_token, timeout = 0) ⇒ Object



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/sapfire/helper/ms_devcenter_helper.rb', line 129

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

  connection = Faraday.new(HOST)

  begin
    response = connection.post("/#{API_VERSION}/#{API_ROOT}/#{app_id}/submissions") 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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 109

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

  connection = Faraday.new(HOST)

  begin
    response = connection.get("/#{API_VERSION}/#{API_ROOT}/#{app_id}") 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_status(app_id, submission_id, auth_token, timeout = 0) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 211

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

  response = get_submission_status_internal(app_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, 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

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



285
286
287
288
289
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 285

def self.non_published_submission(app_id, auth_token, timeout = 0)
  check_app_id(app_id)
  app_info = get_app_info(app_id, auth_token, timeout)
  app_info["pendingApplicationSubmission"]
end

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



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 193

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)

  begin
    response = connection.delete("/#{API_VERSION}/#{API_ROOT}/#{app_id}/submissions/#{submission_id}") 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



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/sapfire/helper/ms_devcenter_helper.rb', line 152

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?

  submission_id = submission_obj["id"]
  connection = Faraday.new(HOST)

  begin
    response = connection.put("/#{API_VERSION}/#{API_ROOT}/#{app_id}/submissions/#{submission_id}") 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

.upload_blob(url, zip_path, timeout = 0) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fastlane/plugin/sapfire/helper/ms_devcenter_helper.rb', line 13

def self.upload_blob(url, zip_path, timeout = 0)
  UI.user_error!("File upload URL need to be provided") if !url.is_a?(String) || url.nil? || url.empty?
  UI.user_error!("File path is invalid") if !zip_path.is_a?(String) || zip_path.nil? || zip_path.empty?

  expand_path = File.expand_path(zip_path)
  UI.user_error!("The provided path doesn't point to ZIP file") unless File.exist?(expand_path) && zip_path.end_with?(".zip")

  File.open(expand_path) do |file|
    block_list = []
    chunks_count = (file.size.to_f / FILE_CHUNK_SIZE).ceil
    current_chunk = 1

    until file.eof?
      bytes = file.read(FILE_CHUNK_SIZE)
      id = SecureRandom.uuid.delete("-")
      block_list.append(id)
      retry_count = 0
      result = false

      UI.message("Upload chunk [#{current_chunk} / #{chunks_count}]")

      while !result && retry_count < UPLOAD_RETRIES
        result = upload_block(url, bytes, id, timeout)
        retry_count += 1
      end

      UI.user_error!("Uploading failed: some chunks have not been uploaded") unless result
      current_chunk += 1
    end

    result = upload_block_list(url, block_list, timeout)
    UI.user_error!("Uploading failed: block list hasn't been uploaded") unless result
  end
end