Class: Fastlane::Actions::UploadToLoadlyAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/upload_to_loadly/actions/upload_to_loadly_action.rb

Constant Summary collapse

UPLOAD_URL =
"https://api.loadly.io/apiv2/app/upload"
"https://i.loadly.io"

Class Method Summary collapse

Class Method Details

.authorsObject



194
195
196
# File 'lib/fastlane/plugin/upload_to_loadly/actions/upload_to_loadly_action.rb', line 194

def self.authors
  ["aliffazfar"]
end

.available_optionsObject



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
# File 'lib/fastlane/plugin/upload_to_loadly/actions/upload_to_loadly_action.rb', line 152

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :api_key,
                            env_name: "LOADLY_API_KEY",
                         description: "Loadly API Key",
                            optional: false),
    FastlaneCore::ConfigItem.new(key: :file,
                            env_name: "LOADLY_FILE",
                         description: "Path to .ipa or .apk file. Default - `IPA_OUTPUT_PATH` or `GRADLE_APK_OUTPUT_PATH` based on platform",
                            optional: true,
                       default_value: self.default_file_path),
    FastlaneCore::ConfigItem.new(key: :build_password,
                            env_name: "LOADLY_BUILD_PASSWORD",
                         description: "Set the App installation password. If the password is empty, public installation is used by default",
                            optional: true),
    FastlaneCore::ConfigItem.new(key: :build_description,
                            env_name: "LOADLY_BUILD_UPDATE_DESCRIPTION",
                         description: "Additional information to your users on this build: the comment will be displayed on the installation page",
                            optional: true),
    FastlaneCore::ConfigItem.new(key: :callback_url,
                            env_name: "LOADLY_CALLBACK_URL",
                         description: "The URL loadly should call with the result",
                            optional: true),
    FastlaneCore::ConfigItem.new(key: :timeout,
                            env_name: "LOADLY_TIMEOUT",
                         description: "Timeout for uploading file to Loadly. Default: 600, range: (60, 1800)",
                           is_string: false,
                            optional: true,
                       default_value: 600),
  ]
end

.callback(url, data) ⇒ Object



139
140
141
142
143
# File 'lib/fastlane/plugin/upload_to_loadly/actions/upload_to_loadly_action.rb', line 139

def self.callback(url, data)
  UI.success("Performing callback to #{url}")
  RestClient.post(url, data)
  UI.success("Callback successfully")
end

.default_file_pathObject



145
146
147
148
149
150
# File 'lib/fastlane/plugin/upload_to_loadly/actions/upload_to_loadly_action.rb', line 145

def self.default_file_path
  platform = Actions.lane_context[SharedValues::PLATFORM_NAME]
  ios_path = Actions.lane_context[SharedValues::IPA_OUTPUT_PATH]
  android_path = Actions.lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]
  return platform == :ios ? ios_path : android_path
end

.descriptionObject



190
191
192
# File 'lib/fastlane/plugin/upload_to_loadly/actions/upload_to_loadly_action.rb', line 190

def self.description
  "Upload .ipa/.apk file to loadly.io"
end

.detailsObject



198
199
200
# File 'lib/fastlane/plugin/upload_to_loadly/actions/upload_to_loadly_action.rb', line 198

def self.details
    "This action upload .ipa/.apk file to https://loadly.io and return link to uploaded file."
end

.format_size(size_in_bytes) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fastlane/plugin/upload_to_loadly/actions/upload_to_loadly_action.rb', line 112

def self.format_size(size_in_bytes)
  units = ['B', 'KB', 'MB', 'GB']
  unit_index = 0
  size = size_in_bytes.to_f

  while size > 1024 && unit_index < units.length - 1
    size /= 1024
    unit_index += 1
  end

  "%.2f %s" % [size, units[unit_index]]
end

.format_time(seconds) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/fastlane/plugin/upload_to_loadly/actions/upload_to_loadly_action.rb', line 125

def self.format_time(seconds)
  if seconds < 60
    "#{seconds.round}s"
  elsif seconds < 3600
    minutes = (seconds / 60).floor
    remaining_seconds = (seconds % 60).round
    "#{minutes}m #{remaining_seconds}s"
  else
    hours = (seconds / 3600).floor
    remaining_minutes = ((seconds % 3600) / 60).floor
    "#{hours}h #{remaining_minutes}m"
  end
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/fastlane/plugin/upload_to_loadly/actions/upload_to_loadly_action.rb', line 202

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

.outputObject



184
185
186
187
188
# File 'lib/fastlane/plugin/upload_to_loadly/actions/upload_to_loadly_action.rb', line 184

def self.output
  [
    ['UPLOADED_FILE_LINK_TO_LOADLY', 'URL to uploaded .ipa or .apk file to loadly.']
  ]
end

.run(options) ⇒ 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
42
43
44
45
46
47
48
49
50
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
77
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
106
107
108
109
110
# File 'lib/fastlane/plugin/upload_to_loadly/actions/upload_to_loadly_action.rb', line 15

def self.run(options)
  Actions.verify_gem!('rest-client')
  require 'rest-client'
  require 'json'
  require 'thread'

  if options[:file].nil?
    UI.important("File didn't come to LOADLY_plugin. Uploading is unavailable.")
    return
  end

  if options[:api_key].nil?
    UI.important("Loadly.io api_key is empty - uploading is unavailable.")
    UI.important("Try to upload file by yourself. Path: #{options[:file]}")
    return
  end

  file_size = File.size(options[:file])
  file_name = File.basename(options[:file])

  upload_options = {
    _api_key: options[:api_key],
    buildPassword: options[:build_password],
    buildUpdateDescription: options[:build_description],
    file: File.new(options[:file], 'rb'),
  }

  timeout = options[:timeout]

  UI.message("📦 Preparing to upload #{file_name} (#{format_size(file_size)})")
  UI.message("⏳ Initializing upload to Loadly.io...")

  started_at = Time.now
  spinner_chars = ['', '', '', '', '', '', '', '', '', '']
  spinner_index = 0
  spinner_running = true

  # Start the spinner thread
  spinner_thread = Thread.new do
    while spinner_running
      spinner = spinner_chars[spinner_index % spinner_chars.length]
      elapsed = Time.now - started_at
      UI.message("#{spinner} Uploading... (#{format_time(elapsed)})")
      spinner_index += 1
      sleep(1)
    end
  end

  begin
    response = RestClient::Request.execute(
      method: :post,
      url: UPLOAD_URL,
      timeout: timeout,
      payload: upload_options
    )
    spinner_running = false
    spinner_thread.join # Wait for spinner to finish
    UI.message("\r ") # Clear spinner line

    data = JSON.parse(response.body)['data']

    if data
      download_url = "#{LOADLY_FILE_LINK}/#{data['buildKey']}"
      qr_code_url = data['buildQRCodeURL']
      shortcut_url = "#{LOADLY_FILE_LINK}/#{data['buildShortcutUrl']}"

      Actions.lane_context[SharedValues::UPLOADED_FILE_LINK_TO_LOADLY] = download_url
      Actions.lane_context[SharedValues::QR_CODE_URL_TO_LOADLY] = qr_code_url
      Actions.lane_context[SharedValues::SHORTCUT_URL_TO_LOADLY] = shortcut_url

      total_time = Time.now - started_at
      UI.success("✅ Upload completed successfully: #{file_name} (#{format_size(file_size)})")
      UI.success("📱 Install URLs:")
      UI.message("   • Download URL:  #{download_url}")
      UI.message("   • Shortcut URL: #{shortcut_url}")
      UI.message("   • QR Code URL:  #{qr_code_url}")

      if !options[:callback_url].nil?
        data['download_url'] = download_url
        self.callback(options[:callback_url], data)
      end

      return
    else
      UI.error("❌ Upload failed! No response data received.")
    end
  rescue RestClient::ExceptionWithResponse => error
    spinner_running = false
    spinner_thread.join
    UI.message("\r ") # Clear spinner line
    UI.error("❌ Upload failed!")
    UI.important("Failed to upload file to loadly, because of:")
    UI.important(error.message)
    UI.important("Try to upload file by yourself. Path: #{options[:file]}")
  end
end