Class: Fastlane::Helper::HuaweiAppgalleryConnectHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/huawei_appgallery_connect/helper/huawei_appgallery_connect_helper.rb

Class Method Summary collapse

Class Method Details

.get_app_id(token, client_id, package_id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fastlane/plugin/huawei_appgallery_connect/helper/huawei_appgallery_connect_helper.rb', line 24

def self.get_app_id(token, client_id, package_id)
  UI.message("Fetching App ID")

  uri = URI.parse("https://connect-api.cloud.huawei.com/api/publish/v2/appid-list?packageName=#{package_id}")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new(uri.request_uri)
  request["client_id"] = client_id
  request["Authorization"] = "Bearer #{token}"
  response = http.request(request)
  if !response.kind_of? Net::HTTPSuccess
    UI.user_error!("Cannot obtain app id, please check API Token / Permissions (status code: #{response.code})")
    return false
  end
  result_json = JSON.parse(response.body)

  if result_json['ret']['code'] == 0
    UI.success("Successfully getting app id")
    return result_json['appids'][0]['value']
  else
    UI.user_error!(result_json)
    UI.user_error!("Failed to get app id")
  end

end

.get_app_info(token, client_id, app_id) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fastlane/plugin/huawei_appgallery_connect/helper/huawei_appgallery_connect_helper.rb', line 51

def self.get_app_info(token, client_id, app_id)
  UI.message("Fetching App Info")

  uri = URI.parse("https://connect-api.cloud.huawei.com/api/publish/v2/app-info?appId=#{app_id}")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new(uri.request_uri)
  request["client_id"] = client_id
  request["Authorization"] = "Bearer #{token}"
  response = http.request(request)
  if !response.kind_of? Net::HTTPSuccess
    UI.user_error!("Cannot obtain app info, please check API Token / Permissions (status code: #{response.code})")
    return false
  end
  result_json = JSON.parse(response.body)

  if result_json['ret']['code'] == 0
    UI.success("Successfully getting app info")
    return result_json['appInfo']
  else
    UI.user_error!(result_json)
    UI.user_error!("Failed to get app info")
  end

end

.get_token(client_id, client_secret) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fastlane/plugin/huawei_appgallery_connect/helper/huawei_appgallery_connect_helper.rb', line 9

def self.get_token(client_id, client_secret)
  UI.important("Fetching app access token")

  uri = URI('https://connect-api.cloud.huawei.com/api/oauth2/v1/token')
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
  req.body = {client_id: client_id, grant_type: 'client_credentials', client_secret: client_secret }.to_json
  res = http.request(req)

  result_json = JSON.parse(res.body)

  return result_json['access_token']
end

.query_aab_compilation_status(token, params, pkgVersion) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/fastlane/plugin/huawei_appgallery_connect/helper/huawei_appgallery_connect_helper.rb', line 213

def self.query_aab_compilation_status(token,params, pkgVersion)
  UI.important("Checking aab compilation status")
  uri = URI.parse("https://connect-api.cloud.huawei.com/api/publish/v2/aab/complile/status?appId=#{params[:app_id]}&pkgIds=#{pkgVersion}")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new(uri.request_uri)
  request["client_id"] = params[:client_id]
  request["Authorization"] = "Bearer #{token}"

  response = http.request(request)

  if !response.kind_of? Net::HTTPSuccess
    UI.user_error!("Cannot query compilation status (status code: #{response.code}, body: #{response.body})")
    return false
  end

  result_json = JSON.parse(response.body)

  if result_json['ret']['code'] == 0
    return result_json['pkgStateList'][0]['aabCompileStatus']
  else
    UI.user_error!(result_json)
    return -999
  end
end

.set_gms_dependency(token, client_id, app_id, gms_dependency) ⇒ Object



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
# File 'lib/fastlane/plugin/huawei_appgallery_connect/helper/huawei_appgallery_connect_helper.rb', line 373

def self.set_gms_dependency(token, client_id, app_id, gms_dependency)
  UI.message("Setting GMS Dependency")

  uri = URI.parse("https://connect-api.cloud.huawei.com/api/publish/v2/properties/gms?appId=#{app_id}")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Put.new(uri.request_uri)
  request["client_id"] = client_id
  request["Authorization"] = "Bearer #{token}"
  request["Content-Type"] = "application/json"

  request.body = {needGms: gms_dependency}.to_json

  response = http.request(request)
  if !response.kind_of? Net::HTTPSuccess
    UI.user_error!("Cannot update gms dependency, please check API Token / Permissions (status code: #{response.code})")
    return false
  end
  result_json = JSON.parse(response.body)

  if result_json['ret']['code'] == 0
    UI.success("Successfully updated GMS Dependency")
  else
    UI.user_error!(result_json)
    UI.user_error!("Failed to update GMS Dependency")
  end
end

.submit_app_for_review(token, params) ⇒ Object



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
279
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
# File 'lib/fastlane/plugin/huawei_appgallery_connect/helper/huawei_appgallery_connect_helper.rb', line 240

def self.submit_app_for_review(token, params)
  UI.important("Submitting app for review")

  release_type = ''
  release_time = ''

  if (params[:phase_wise_release] != nil && params[:phase_wise_release]) && (
        params[:phase_release_start_time] == nil ||
        params[:phase_release_end_time] == nil ||
        params[:phase_release_percent] == nil ||
        params[:phase_release_description] == nil
  )
    UI.user_error!("Submit for review failed. Phase wise release requires Start time, End time Release Percent & Descrption")
    return
  elsif params[:phase_wise_release] != nil && params[:phase_wise_release]
    release_type = '&releaseType=3'
  end

  if params[:release_time] != nil
    params[:release_time] = CGI.escape(params[:release_time])
    release_time = "&releaseTime=#{params[:release_time]}"
  end

  changelog = ''

  if params[:changelog_path] != nil
    changelog_data = File.read(params[:changelog_path])

    if changelog_data.length < 3 || changelog_data.length > 300
      UI.user_error!("Failed to submit app for review. Changelog file length is invalid")
      return
    else
      changelog = "&remark=" + CGI.escape(changelog_data)
    end
  end

  uri = URI.parse("https://connect-api.cloud.huawei.com/api/publish/v2/app-submit?appId=#{params[:app_id]}" + changelog + release_type + release_time)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri.request_uri)
  request["client_id"] = params[:client_id]
  request["Authorization"] = "Bearer #{token}"
  request["Content-Type"] = "application/json"

  if params[:phase_wise_release] != nil && params[:phase_wise_release]
    request.body = {
        phasedReleaseStartTime: params[:phase_release_start_time],
        phasedReleaseEndTime: params[:phase_release_end_time],
        phasedReleasePercent: params[:phase_release_percent],
        phasedReleaseDescription: params[:phase_release_description]
    }.to_json
  end

  response = http.request(request)

  if !response.kind_of? Net::HTTPSuccess
    UI.user_error!("Cannot submit app for review (status code: #{response.code}, body: #{response.body})")
    return false
  end

  result_json = JSON.parse(response.body)

  if result_json['ret']['code'] == 0
      UI.success("Successfully submitted app for review")
  elsif result_json['ret']['code'] == 204144660 && result_json['ret']['msg'].include?("It may take 2-5 minutes")
    UI.important(result_json)
    UI.important("Build is currently processing, waiting for 2 minutes before submitting again...")
    sleep(120)
    self.submit_app_for_review(token, params)
  else
    UI.user_error!(result_json)
    UI.user_error!("Failed to submit app for review.")
  end

end

.update_app_localization_info(token, params) ⇒ Object



317
318
319
320
321
322
323
324
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
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/fastlane/plugin/huawei_appgallery_connect/helper/huawei_appgallery_connect_helper.rb', line 317

def self.update_app_localization_info(token, params)
   = if !params[:metadata_path].nil?
                    params[:metadata_path]
                  else
                    'fastlane/metadata/huawei'
                  end

  UI.important("Uploading app localization information from path: #{}")

  # gather info from metadata folders
  Dir.glob("#{}/*") do |folder|
    uri = URI.parse("https://connect-api.cloud.huawei.com/api/publish/v2/app-language-info?appId=#{params[:app_id]}")
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    request = Net::HTTP::Put.new(uri.request_uri)
    request['client_id'] = params[:client_id]
    request['Authorization'] = "Bearer #{token}"
    request['Content-Type'] = 'application/json'
    lang = File.basename(folder)
    body = { "lang": lang }

    Dir.glob("#{folder}/*") do |file|
      case file
      when /app_name/
        body[:appName] = File.read(file)
      when /app_description/
        body[:appDesc] = File.read(file)
      when /introduction/
        body[:briefInfo] = File.read(file)
      when /release_notes/
        body[:newFeatures] = File.read(file)
      end
    end

    body.length.zero? && next
    UI.important(body.to_json)
    request.body = body.to_json
    response = http.request(request)

    UI.important(response)

    unless response.is_a? Net::HTTPSuccess
      UI.user_error!("Cannot upload localization info (status code: #{response.code}, body: #{response.body})")
      return false
    end

    result_json = JSON.parse(response.body)

    if result_json['ret']['code'].zero?
      UI.success("Successfully uploaded app localization info for #{File.basename(folder)}")
    else
      UI.user_error!(result_json)
    end
  end
end

.update_appinfo(client_id, token, app_id, privacy_policy_url) ⇒ Object



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
103
104
105
# File 'lib/fastlane/plugin/huawei_appgallery_connect/helper/huawei_appgallery_connect_helper.rb', line 78

def self.update_appinfo(client_id, token, app_id, privacy_policy_url)
  UI.important("Updating app info")

  uri = URI.parse("https://connect-api.cloud.huawei.com/api/publish/v2/app-info?appId=#{app_id}")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Put.new(uri.request_uri)
  request["client_id"] = client_id
  request["Authorization"] = "Bearer #{token}"
  request["Content-Type"] = "application/json"

  request.body = {privacyPolicy: privacy_policy_url}.to_json

  response = http.request(request)
  if !response.kind_of? Net::HTTPSuccess
    UI.user_error!("Cannot update app info, please check API Token / Permissions (status code: #{response.code})")
    return false
  end
  result_json = JSON.parse(response.body)

  if result_json['ret']['code'] == 0
    UI.success("Successfully updated app info")
  else
    UI.user_error!(result_json)
    UI.user_error!("Failed to update app info")
  end
end

.upload_app(token, client_id, app_id, apk_path, is_aab) ⇒ Object



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
151
152
153
154
155
156
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/fastlane/plugin/huawei_appgallery_connect/helper/huawei_appgallery_connect_helper.rb', line 108

def self.upload_app(token, client_id, app_id, apk_path, is_aab)
  UI.message("Fetching upload URL")

  responseData = JSON.parse("{}")
  responseData["success"] = false
  responseData["code"] = 0

  if(is_aab)
    uri = URI.parse("https://connect-api.cloud.huawei.com/api/publish/v2/upload-url?appId=#{app_id}&suffix=aab")
    upload_filename = "release.aab"
  else
    uri = URI.parse("https://connect-api.cloud.huawei.com/api/publish/v2/upload-url?appId=#{app_id}&suffix=apk")
    upload_filename = "release.apk"
  end

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new(uri.request_uri)
  request["client_id"] = client_id
  request["Authorization"] = "Bearer #{token}"
  request["Content-Type"] = "application/json"

  response = http.request(request)

  if !response.kind_of? Net::HTTPSuccess
    UI.user_error!("Cannot obtain upload url, please check API Token / Permissions (status code: #{response.code})")
    responseData["success"] = false
    return responseData
  end

  result_json = JSON.parse(response.body)

  if result_json['uploadUrl'].nil?
    UI.user_error!('Cannot obtain upload url')
    responseData["success"] = false
    return responseData
  else
    UI.important('Uploading app')
    # Upload App
    boundary = "755754302457647"
    uri = URI(result_json['uploadUrl'])
    # uri = URI("http://localhost/dashboard/test")
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    request = Net::HTTP::Post.new(uri)

    form_data = [['file', File.open(apk_path.to_s)],['authCode', result_json['authCode']],['fileCount', '1']]
    request.set_form form_data, 'multipart/form-data'

    result = http.request(request)
    if !result.kind_of? Net::HTTPSuccess
      UI.user_error!("Cannot upload app, please check API Token / Permissions (status code: #{result.code})")
      responseData["success"] = false
      return responseData
    end
    result_json = JSON.parse(result.body)

    if result_json['result']['result_code'].to_i == 0
      UI.success('Upload app to AppGallery Connect successful')
      UI.important("Saving app information")

      uri = URI.parse("https://connect-api.cloud.huawei.com/api/publish/v2/app-file-info?appId=#{app_id}")

      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      request = Net::HTTP::Put.new(uri.request_uri)
      request["client_id"] = client_id
      request["Authorization"] = "Bearer #{token}"
      request["Content-Type"] = "application/json"

      data = {fileType: 5, files: [{

          fileName: upload_filename,
          fileDestUrl: result_json['result']['UploadFileRsp']['fileInfoList'][0]['fileDestUlr'],
          size: result_json['result']['UploadFileRsp']['fileInfoList'][0]['size'].to_s

      }] }.to_json

      request.body = data
      response = http.request(request)
      if !response.kind_of? Net::HTTPSuccess
        UI.user_error!("Cannot save app info, please check API Token / Permissions (status code: #{response.code})")
        responseData["success"] = false
        return responseData
      end
      result_json = JSON.parse(response.body)

      if result_json['ret']['code'] == 0
        UI.success("App information saved.")
        responseData["success"] = true
        responseData["pkgVersion"] = result_json["pkgVersion"][0]
        return responseData
      else
        UI.user_error!(result_json)
        UI.user_error!("Failed to save app information")
        responseData["success"] = false
        return responseData
      end
    else
      responseData["success"] = false
      return responseData
    end
  end
end