Class: Fastlane::Helper::RuStoreHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_ru_store_helper.rb

Class Method Summary collapse

Class Method Details

.create_draft(token:, package_name:, changelog_path:, publish_type:, publish_date_time:, timeout:) ⇒ Object



60
61
62
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
# File 'lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_ru_store_helper.rb', line 60

def self.create_draft(
  token:,
  package_name:,
  changelog_path:,
  publish_type:, publish_date_time:,
  timeout:
)
  http = self.get_http(timeout: timeout)

  request = Net::HTTP::Post.new("/public/v1/application/#{package_name}/version")
  self.fill_headers(
    request: request,
    token: token
  )

  changelog = ''
  unless changelog_path.nil?
    changelog_data = File.read(changelog_path)
    if changelog_data.length > 5000
      UI.user_error!("Файл 'Что нового?' содержит более 5000 символов")
      return
    else
      changelog = changelog_data
    end
  end

  request.body = {
    whatsNew: changelog,
    publishType: publish_type,
    publishDateTime: publish_date_time
  }.to_json

  response = http.request(request)

  if response.kind_of? Net::HTTPSuccess
    json_body = JSON.parse(response.body)
    return json_body["body"]
  else
    UI.user_error!("Unable to create RuStore application draft: #{response.message}, #{response.body}")
  end
end

.fill_headers(request:, token: "", content_type: 'application/json') ⇒ Object



139
140
141
142
# File 'lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_ru_store_helper.rb', line 139

def self.fill_headers(request:, token: "", content_type: 'application/json')
  request.add_field('Content-Type', content_type)
  request.add_field('Public-Token', token)
end

.get_draft_id(token:, package_name:, timeout:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_ru_store_helper.rb', line 36

def self.get_draft_id(token:, package_name:, timeout:)
  http = self.get_http(timeout: timeout)

  query_params = URI.encode_www_form({
    versionStatuses: "DRAFT"
  })

  request = Net::HTTP::Get.new("/public/v1/application/#{package_name}/version?#{query_params}")
  self.fill_headers(
    request: request,
    token: token
  )

  response = http.request(request)

  if response.kind_of? Net::HTTPSuccess
    json_body = JSON.parse(response.body)
    versions = json_body["body"]["content"]
    return versions.empty? ? nil : versions[0]["versionId"]
  else
    UI.user_error!("Unable to get RuStore application draft: #{response.message}, #{response.body}")
  end
end

.get_http(timeout:) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_ru_store_helper.rb', line 129

def self.get_http(timeout:)
  uri = URI.parse("https://public-api.rustore.ru")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.read_timeout = timeout
  http.set_debug_output($stdout)

  http
end

.get_token(key_id:, private_key:, timeout:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_ru_store_helper.rb', line 10

def self.get_token(key_id:, private_key:, timeout:)
  timestamp = DateTime.now.iso8601(3)
  signature = rsa_sign(timestamp, key_id, private_key)

  http = self.get_http(timeout: timeout)

  request = Net::HTTP::Post.new("/public/auth/")
  self.fill_headers(
    request: request
  )

  request.body = {
    keyId: key_id,
    timestamp: timestamp,
    signature: signature
  }.to_json
  response = http.request(request)

  if response.kind_of? Net::HTTPSuccess
    json_body = JSON.parse(response.body)
    return json_body["body"]["jwe"]
  else
    UI.user_error!("Unable to get RuStore token: #{response.message}, #{response.body}")
  end
end

.rsa_sign(timestamp, key_id, private_key) ⇒ Object



144
145
146
147
148
# File 'lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_ru_store_helper.rb', line 144

def self.rsa_sign(timestamp, key_id, private_key)
  key = OpenSSL::PKey::RSA.new("-----BEGIN RSA PRIVATE KEY-----\n#{private_key}\n-----END RSA PRIVATE KEY-----")
  signature = key.sign(OpenSSL::Digest.new('SHA512'), key_id + timestamp)
  Base64.encode64(signature)
end

.send_to_review(token:, package_name:, draft_id:, timeout:) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_ru_store_helper.rb', line 115

def self.send_to_review(token:, package_name:, draft_id:, timeout:)
  http = self.get_http(timeout: timeout)
  request = Net::HTTP::Post.new("/public/v1/application/#{package_name}/version/#{draft_id}/commit")
  self.fill_headers(
    request: request,
    token: token
  )

  response = http.request(request)
  unless response.kind_of? Net::HTTPSuccess
    UI.user_error!("Unable send to review RuStore application draft: #{response.message}, #{response.body}")
  end
end

.upload_aab(token:, package_name:, draft_id:, aab_path:, timeout:) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fastlane/plugin/sq_ci_tools/helper/sq_ci_ru_store_helper.rb', line 102

def self.upload_aab(token:, package_name:, draft_id:, aab_path:, timeout:)
  http = self.get_http(timeout: timeout)
  request = Net::HTTP::Post.new("/public/v1/application/#{package_name}/version/#{draft_id}/aab")
  
  request.add_field('Public-Token', token)
  request.set_form([['file', File.open(aab_path)]], 'multipart/form-data')

  response = http.request(request)
  unless response.kind_of? Net::HTTPSuccess
    UI.user_error!("Unable to upload aab to RuStore application draft: #{response.message}, #{response.body}")
  end
end