Class: Fastlane::Helper::RustoreHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/rustore/helper/rustore_helper.rb

Class Method Summary collapse

Class Method Details

.commit_version(token, draft_id, package_name) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/fastlane/plugin/rustore/helper/rustore_helper.rb', line 99

def self.commit_version(token, draft_id, package_name)
  url = "/public/v1/application/#{package_name}/version/#{draft_id}/commit"
  response = connection.post(url) do |req|
    req.headers['Public-Token'] = token
  end

  UI.message("Debug: response #{response.body}") if ENV['DEBUG']
end

.connectionObject



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

def self.connection
  require 'faraday'
  require 'faraday_middleware'

  options = {
    url: "https://public-api.rustore.ru"
  }

  logger = Logger.new($stderr)
  logger.level = Logger::DEBUG

  Faraday.new(options) do |builder|
    builder.request(:multipart)
    builder.request(:json)
    builder.request(:url_encoded)
    builder.response(:json, content_type: /\bjson$/)
    builder.response(:logger, logger)
    builder.use(FaradayMiddleware::FollowRedirects)
    builder.adapter(:net_http)
  end
end

.create_draft(token, package_name, publish_type) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fastlane/plugin/rustore/helper/rustore_helper.rb', line 51

def self.create_draft(token, package_name, publish_type)
  url = "/public/v1/application/#{package_name}/version"
  response = connection.post(url) do |req|
    req.headers['Public-Token'] = token
    req.body = {}
    req.body['publishType'] = publish_type unless publish_type.nil?
  end

  UI.message("Debug: response #{response.body}") if ENV['DEBUG']
  if response.body["body"]
    # Если черновика не было, и мы создали новый, здесь будет draftId
    return response.body["body"]
  elsif response.body["message"]
    # Если черновик уже существовал, в message будет ошибка вида
    # "You already have draft version with ID = XXXXXXXXXX", откуда достаем ID существующего черновика.
    return response.body["message"].scan(/\d+/).first.to_i
  else
    raise "Couldn't get draftId from RuStore"
  end
end

.get_token(key_id:, private_key:) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fastlane/plugin/rustore/helper/rustore_helper.rb', line 38

def self.get_token(key_id:, private_key:)
  timestamp = DateTime.now.iso8601(3)
  signature = rsa_sign(timestamp, key_id, private_key)
  url = "/public/auth/"
  response = connection.post(url) do |req|
    req.body = { keyId: key_id, timestamp: timestamp, signature: signature }
  end

  UI.message("Debug: response #{response.body}") if ENV['DEBUG']

  response.body["body"]["jwe"]
end

.rsa_sign(timestamp, key_id, private_key) ⇒ Object



32
33
34
35
36
# File 'lib/fastlane/plugin/rustore/helper/rustore_helper.rb', line 32

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

.upload_apk(token, draft_id, is_hms, file_path, package_name) ⇒ Object



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
# File 'lib/fastlane/plugin/rustore/helper/rustore_helper.rb', line 72

def self.upload_apk(token, draft_id, is_hms, file_path, package_name)
  if is_hms
    apk_type = "HMS"
    is_main = false
  else
    apk_type = "Unknown"
    is_main = true
  end

  url = "/public/v1/application/#{package_name}/version/#{draft_id}/apk"
  payload = {}
  payload[:file] = Faraday::Multipart::FilePart.new(file_path, 'application/vnd.android.package-archive')

  response = connection.post(url) do |req|
    req.headers['Public-Token'] = token
    req.params['servicesType'] = apk_type
    req.params['isMainApk'] = is_main
    req.body = payload
  end

  if response.body["message"] == "File was not uploaded successfully: The code of this version must be larger than that of the previous one"
    raise "Build with this version code was already uploaded earlier"
  end

  UI.message("Debug: response #{response.body}") if ENV['DEBUG']
end