Class: Fastlane::Helper::RustoreSdkHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/rustore_sdk/helper/rustore_sdk_helper.rb

Constant Summary collapse

BASE_URL =
'https://public-api.rustore.ru/public'

Class Method Summary collapse

Class Method Details

.create_draft(token:, package_name:, version_number: nil, whats_new: "Internal update") ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fastlane/plugin/rustore_sdk/helper/rustore_sdk_helper.rb', line 57

def self.create_draft(token:, package_name:, version_number: nil, whats_new: "Internal update")
  uri = URI("#{BASE_URL}/v1/application/#{package_name}/version")
  request = Net::HTTP::Post.new(uri, { 'Content-Type' => 'application/json', 'Public-Token' => token })

  body = { whatsNew: whats_new }
  body[:versionName] = version_number if version_number
  request.body = body.to_json

  response = perform_request(uri, request)
  handle_response(response) { |b| b['body'] } # Returns versionId
end

.get_app_version(token:, package_name:, status: 'ACTIVE', testing_type: 'RELEASE') ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fastlane/plugin/rustore_sdk/helper/rustore_sdk_helper.rb', line 43

def self.get_app_version(token:, package_name:, status: 'ACTIVE', testing_type: 'RELEASE')
  query = "versionStatuses=#{status}&filterTestingType=#{testing_type}"
  uri = URI("#{BASE_URL}/v1/application/#{package_name}/version?#{query}")

  request = Net::HTTP::Get.new(uri)
  request['Public-Token'] = token

  response = perform_request(uri, request)
  handle_response(response) do |b|
    versions = b['body']['content']
    versions.first if versions && !versions.empty?
  end
end

.get_auth_token(key_id:, private_key_path: nil, private_key_data: nil) ⇒ Object



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
# File 'lib/fastlane/plugin/rustore_sdk/helper/rustore_sdk_helper.rb', line 15

def self.get_auth_token(key_id:, private_key_path: nil, private_key_data: nil)
  key_content = if private_key_data
                  private_key_data
                elsif private_key_path
                  UI.user_error!("Private key file not found at: #{private_key_path}") unless File.exist?(private_key_path)
                  File.read(private_key_path)
                end

  UI.user_error!("No private key provided. Set RUSTORE_PRIVATE_KEY_PATH or RUSTORE_PRIVATE_KEY_DATA") if key_content.nil? || key_content.empty?

  unless key_content.include?("-----BEGIN")
    clean_key = key_content.gsub(/\s+/, "")
    key_content = "-----BEGIN PRIVATE KEY-----\n#{clean_key.scan(/.{1,64}/).join("\n")}\n-----END PRIVATE KEY-----"
  end

  rsa_key = OpenSSL::PKey.read(key_content)
  timestamp = Time.now.iso8601(3)
  data_to_sign = "#{key_id}#{timestamp}"
  signature = Base64.strict_encode64(rsa_key.sign(OpenSSL::Digest::SHA512.new, data_to_sign))

  uri = URI("#{BASE_URL}/auth/")
  request = Net::HTTP::Post.new(uri, { 'Content-Type' => 'application/json' })
  request.body = { keyId: key_id, timestamp: timestamp, signature: signature }.to_json

  response = perform_request(uri, request)
  handle_response(response) { |b| b['body']['jwe'] }
end

.handle_response(response) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fastlane/plugin/rustore_sdk/helper/rustore_sdk_helper.rb', line 107

def self.handle_response(response)
  case response
  when Net::HTTPSuccess
    body = JSON.parse(response.body)
    if body['code'] == 'OK'
      yield(body)
    else
      UI.user_error!("RuStore API error: #{body['message']}")
    end
  else
    UI.user_error!("HTTP error: #{response.code} #{response.message}\n#{response.body}")
  end
end

.perform_request(uri, request) ⇒ Object



101
102
103
104
105
# File 'lib/fastlane/plugin/rustore_sdk/helper/rustore_sdk_helper.rb', line 101

def self.perform_request(uri, request)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.request(request)
end

.submit_version(token:, package_name:, version_id:) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/fastlane/plugin/rustore_sdk/helper/rustore_sdk_helper.rb', line 91

def self.submit_version(token:, package_name:, version_id:)
  uri = URI("#{BASE_URL}/v1/application/#{package_name}/version/#{version_id}/submit")
  request = Net::HTTP::Post.new(uri, { 'Public-Token' => token })

  response = perform_request(uri, request)
  handle_response(response) { true }
end

.upload_aab(token:, package_name:, version_id:, aab_path:) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fastlane/plugin/rustore_sdk/helper/rustore_sdk_helper.rb', line 69

def self.upload_aab(token:, package_name:, version_id:, aab_path:)
  uri = URI("#{BASE_URL}/v1/application/#{package_name}/version/#{version_id}/aab")

  command = [
    "curl -s -X POST '#{uri}'",
    "-H 'Public-Token: #{token}'",
    "-F 'file=@\"#{aab_path}\"'"
  ].join(" ")

  result = `#{command}`
  begin
    body = JSON.parse(result)
    if body['code'] == 'OK'
      return true
    else
      UI.user_error!("RuStore upload error: #{body['message']}")
    end
  rescue JSON::ParserError
    UI.user_error!("Failed to parse RuStore API response: #{result}")
  end
end