Class: Fastlane::Actions::AppaloosaAction

Inherits:
Fastlane::Action show all
Defined in:
fastlane/lib/fastlane/actions/appaloosa.rb

Constant Summary collapse

APPALOOSA_SERVER =
'https://www.appaloosa-store.com/api/v2'.freeze

Constants inherited from Fastlane::Action

Fastlane::Action::AVAILABLE_CATEGORIES, Fastlane::Action::RETURN_TYPES

Documentation collapse

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, author, deprecated_notes, lane_context, method_missing, other_action, output, return_type, return_value, sample_return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

[View source]

133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 133

def self.all_screenshots_links(screenshots)
  if screenshots.nil?
    screens = %w(screenshot1 screenshot2 screenshot3 screenshot4 screenshot5)
    screenshots = screens.map do |_k, _v|
      ''
    end
  else
    missings = 5 - screenshots.count
    (1..missings).map do |_i|
      screenshots << ''
    end
  end
  screenshots
end

.authorsObject

[View source]

230
231
232
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 230

def self.authors
  ['Appaloosa']
end

.available_optionsObject

[View source]

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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 183

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :binary,
                                 env_name: 'FL_APPALOOSA_BINARY',
                                 description: 'Binary path. Optional for ipa if you use the `ipa` or `xcodebuild` action',
                                 default_value: Actions.lane_context[SharedValues::IPA_OUTPUT_PATH],
                                 default_value_dynamic: true,
                                 verify_block: proc do |value|
                                   UI.user_error!("Couldn't find ipa || apk file at path '#{value}'") unless File.exist?(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :api_token,
                                 env_name: 'FL_APPALOOSA_API_TOKEN',
                                 sensitive: true,
                                 description: "Your API token"),
    FastlaneCore::ConfigItem.new(key: :store_id,
                                 env_name: 'FL_APPALOOSA_STORE_ID',
                                 description: "Your Store id"),
    FastlaneCore::ConfigItem.new(key: :group_ids,
                                 env_name: 'FL_APPALOOSA_GROUPS',
                                 description: 'Your app is limited to special users? Give us the group ids',
                                 default_value: '',
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :screenshots,
                                 env_name: 'FL_APPALOOSA_SCREENSHOTS',
                                 description: 'Add some screenshots application to your store or hit [enter]',
                                 default_value: Actions.lane_context[SharedValues::SNAPSHOT_SCREENSHOTS_PATH],
                                 default_value_dynamic: true),
    FastlaneCore::ConfigItem.new(key: :locale,
                                 env_name: 'FL_APPALOOSA_LOCALE',
                                 description: 'Select the folder locale for your screenshots',
                                 default_value: 'en-US',
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :device,
                                 env_name: 'FL_APPALOOSA_DEVICE',
                                 description: 'Select the device format for your screenshots',
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :description,
                                 env_name: 'FL_APPALOOSA_DESCRIPTION',
                                 description: 'Your app description',
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :changelog,
                                 env_name: 'FL_APPALOOSA_CHANGELOG',
                                 description: 'Your app changelog',
                                 optional: true)
  ]
end

.categoryObject

[View source]

266
267
268
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 266

def self.category
  :beta
end

.descriptionObject

[View source]

171
172
173
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 171

def self.description
  'Upload your app to [Appaloosa Store](https://www.appaloosa-store.com/)'
end

.detailsObject

[View source]

175
176
177
178
179
180
181
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 175

def self.details
  [
    "Appaloosa is a private mobile application store. This action offers a quick deployment on the platform.",
    "You can create an account, push to your existing account, or manage your user groups.",
    "We accept iOS and Android applications."
  ].join("\n")
end

.error_detected(errors) ⇒ Object

[View source]

154
155
156
157
158
159
160
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 154

def self.error_detected(errors)
  if errors
    UI.user_error!("ERROR: #{errors}")
  else
    false
  end
end

.example_codeObject

[View source]

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 243

def self.example_code
  [
    "appaloosa(
      # Path tor your IPA or APK
      binary: '/path/to/binary.ipa',
      # You can find your store’s id at the bottom of the “Settings” page of your store
      store_id: 'your_store_id',
      # You can find your api_token at the bottom of the “Settings” page of your store
      api_token: 'your_api_key',
      # User group_ids visibility, if it's not specified we'll publish the app for all users in your store'
      group_ids: '112, 232, 387',
      # You can use fastlane/snapshot or specify your own screenshots folder.
      # If you use snapshot please specify a local and a device to upload your screenshots from.
      # When multiple values are specified in the Snapfile, we default to 'en-US'
      locale: 'en-US',
      # By default, the screenshots from the last device will be used
      device: 'iPhone6',
      # Screenshots' filenames should start with device's name like 'iphone6-s1.png' if device specified
      screenshots: '/path/to_your/screenshots'
    )"
  ]
end
[View source]

16
17
18
19
20
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 16

def self.get_binary_link(binary, api_key, store_id, group_ids)
  key_s3 = upload_on_s3(binary, api_key, store_id, group_ids)
  return if key_s3.nil?
  get_s3_url(api_key, store_id, key_s3)
end

.get_env_value(option) ⇒ Object

[View source]

148
149
150
151
152
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 148

def self.get_env_value(option)
  available_options.map do |opt|
    opt if opt.key == option.to_sym
  end.compact[0].default_value
end

.get_s3_url(api_key, store_id, path) ⇒ Object

[View source]

45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 45

def self.get_s3_url(api_key, store_id, path)
  uri = URI("#{APPALOOSA_SERVER}/#{store_id}/upload_services/url_for_download")
  params = { store_id: store_id, api_key: api_key, key: path }
  uri.query = URI.encode_www_form(params)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  url_for_download_response = http.request(Net::HTTP::Get.new(uri.request_uri))
  if invalid_response?(url_for_download_response)
    UI.user_error!("ERROR: A problem occurred with your API token and your store id. Please try again.")
  end
  json_res = JSON.parse(url_for_download_response.body)
  return if error_detected(json_res['errors'])
  json_res['binary_url']
end

.get_screenshots(screenshots_path, locale, device) ⇒ Object

[View source]

89
90
91
92
93
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 89

def self.get_screenshots(screenshots_path, locale, device)
  get_env_value('screenshots').nil? ? locale = '' : locale.concat('/')
  device.nil? ? device = '' : device.concat('-')
  screenshots_path.strip.empty? ? nil : screenshots_list(screenshots_path, locale, device)
end
[View source]

81
82
83
84
85
86
87
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 81

def self.get_screenshots_links(api_key, store_id, screenshots_path, locale, device)
  screenshots = get_screenshots(screenshots_path, locale, device)
  return if screenshots.nil?
  uploaded = upload_screenshots(screenshots, api_key, store_id)
  links = get_uploaded_links(uploaded, api_key, store_id)
  links.kind_of?(Array) ? links.flatten : nil
end
[View source]

73
74
75
76
77
78
79
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 73

def self.get_uploaded_links(uploaded_screenshots, api_key, store_id)
  return if uploaded_screenshots.nil?
  urls = []
  urls << uploaded_screenshots.flatten.map do |url|
    get_s3_url(api_key, store_id, url)
  end
end

.invalid_response?(url_for_download_response) ⇒ Boolean

Returns:

[View source]

238
239
240
241
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 238

def self.invalid_response?(url_for_download_response)
  url_for_download_response.kind_of?(Net::HTTPNotFound) ||
    url_for_download_response.kind_of?(Net::HTTPForbidden)
end

.is_supported?(platform) ⇒ Boolean

Returns:

[View source]

234
235
236
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 234

def self.is_supported?(platform)
  [:ios, :mac, :android].include?(platform)
end

.remove_extra_screenshots_file(screenshots_env) ⇒ Object

[View source]

60
61
62
63
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 60

def self.remove_extra_screenshots_file(screenshots_env)
  extra_file = "#{screenshots_env}/screenshots.html"
  File.unlink(extra_file) if File.exist?(extra_file)
end

.run(params) ⇒ Object

[View source]

5
6
7
8
9
10
11
12
13
14
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 5

def self.run(params)
  api_key = params[:api_token]
  store_id = params[:store_id]
  binary = params[:binary]
  remove_extra_screenshots_file(params[:screenshots])
  binary_url = get_binary_link(binary, api_key, store_id, params[:group_ids])
  return if binary_url.nil?
  screenshots_url = get_screenshots_links(api_key, store_id, params[:screenshots], params[:locale], params[:device])
  upload_on_appaloosa(api_key, store_id, binary_url, screenshots_url, params[:group_ids], params[:description], params[:changelog])
end

.screenshots_list(path, locale, device) ⇒ Object

[View source]

95
96
97
98
99
100
101
102
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 95

def self.screenshots_list(path, locale, device)
  return warning_detected("screenshots folder not found") unless Dir.exist?("#{path}/#{locale}")
  list = Dir.entries("#{path}/#{locale}") - ['.', '..']
  list.map do |screen|
    next if screen.match(device).nil?
    "#{path}/#{locale}#{screen}" unless Dir.exist?("#{path}/#{locale}#{screen}")
  end.compact
end

.upload_on_appaloosa(api_key, store_id, binary_path, screenshots, group_ids, description, changelog) ⇒ Object

[View source]

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 104

def self.upload_on_appaloosa(api_key, store_id, binary_path, screenshots, group_ids, description, changelog)
  screenshots = all_screenshots_links(screenshots)
  uri = URI("#{APPALOOSA_SERVER}/#{store_id}/mobile_application_updates/upload")
  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 = { store_id: store_id,
               api_key: api_key,
               mobile_application_update: {
                 description: description,
                 changelog: changelog,
                 binary_path: binary_path,
                 screenshot1: screenshots[0],
                 screenshot2: screenshots[1],
                 screenshot3: screenshots[2],
                 screenshot4: screenshots[3],
                 screenshot5: screenshots[4],
                 group_ids: group_ids,
                 provider: 'fastlane'
               } }.to_json
  uoa_response = http.request(req)
  json_res = JSON.parse(uoa_response.body)
  if json_res['errors']
    UI.error("App: #{json_res['errors']}")
  else
    UI.success("Binary processing: Check your app': #{json_res['link']}")
  end
end

.upload_on_s3(file, api_key, store_id, group_ids = '') ⇒ Object

[View source]

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 22

def self.upload_on_s3(file, api_key, store_id, group_ids = '')
  file_name = file.split('/').last
  uri = URI("#{APPALOOSA_SERVER}/upload_services/presign_form")
  params = { file: file_name, store_id: store_id, group_ids: group_ids, api_key: api_key }
  uri.query = URI.encode_www_form(params)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  presign_form_response = http.request(Net::HTTP::Get.new(uri.request_uri))
  json_res = JSON.parse(presign_form_response.body)
  return if error_detected(json_res['errors'])
  s3_sign = json_res['s3_sign']
  path = json_res['path']
  uri = URI.parse(Base64.decode64(s3_sign))
  File.open(file, 'rb') do |f|
    http = Net::HTTP.new(uri.host)
    put = Net::HTTP::Put.new(uri.request_uri)
    put.body = f.read
    put['content-type'] = ''
    http.request(put)
  end
  path
end

.upload_screenshots(screenshots, api_key, store_id) ⇒ Object

[View source]

65
66
67
68
69
70
71
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 65

def self.upload_screenshots(screenshots, api_key, store_id)
  return if screenshots.nil?
  list = []
  list << screenshots.map do |screen|
    upload_on_s3(screen, api_key, store_id)
  end
end

.warning_detected(warning) ⇒ Object

[View source]

162
163
164
165
# File 'fastlane/lib/fastlane/actions/appaloosa.rb', line 162

def self.warning_detected(warning)
  UI.important("WARNING: #{warning}")
  nil
end