Module: Fastlane::Helper::ZealotHelper

Included in:
Actions::ZealotAction, Actions::ZealotDebugFileAction, Actions::ZealotSyncDevicesAction, Actions::ZealotVersionCheckAction
Defined in:
lib/fastlane/plugin/zealot/helper/zealot_helper.rb

Constant Summary collapse

UPLOAD_APP_PARAMS_KEYS =
%w[
  name changelog release_type slug branch password
  git_commit custom_fields source ci_url
].freeze

Instance Method Summary collapse

Instance Method Details

#avialable_upload_app_params(params) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 78

def avialable_upload_app_params(params)
  UPLOAD_APP_PARAMS_KEYS.each_with_object({}) do |key, obj|
    value = params.fetch(key.to_sym, ask: false)
    value = JSON.dump(value) if key == 'custom_fields' && value
    value = detect_ci_url(params) if key == 'ci_url'
    value = detect_source(params) if key == 'source'
    obj[key.to_sym] = value if value && !value.empty?
  end
end

#build_app_version_check_params(params) ⇒ Object



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
132
133
134
135
136
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 106

def build_app_version_check_params(params)
  token = params[:token]
  channel_key = params[:channel_key]
  bundle_id = params[:bundle_id]
  release_version = params[:release_version]
  build_version = params[:build_version]
  git_commit = params[:git_commit]

  UI.user_error! 'bundle id is missing' if bundle_id.to_s.empty?

  has_version = !(release_version.to_s.empty? && build_version.to_s.empty?)
  has_git_commit = !git_commit.to_s.empty?
  UI.user_error! 'release_version + build_version or git_commit is missing' unless has_version || has_git_commit

  if has_version
    {
      token: token,
      channel_key: channel_key,
      bundle_id: bundle_id,
      release_version: release_version,
      build_version: build_version
    }
  else
    {
      token: token,
      channel_key: channel_key,
      bundle_id: bundle_id,
      git_commit: git_commit
    }
  end
end

#build_table_data(params, devices) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 145

def build_table_data(params, devices)
  data = {
    'Endpoint' => params[:endpoint],
    'Token' => params[:token],
    "Devices (#{devices.size})" => devices.map {|d| "#{d.name}: #{d.udid}"}.join("\n")
  }
end

#check_app_version(params) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 90

def check_app_version(params)
  query = build_app_version_check_params(params)
  print_table(query, title: 'zealot_version_check', hidden_keys: hidden_keys(params))

  UI.success("Checking app version from Zealot ...")
  connection = make_connection(params[:endpoint], params[:verify_ssl])
  connection.get do |req|
    req.url '/api/apps/version_exist'
    req.params = query
  end
rescue Faraday::ConnectionFailed
  show_error('Can not connecting to Zealot', params[:fail_on_error])
rescue Faraday::TimeoutError
  show_error('Check app version from Zealot timed out ⏳', params[:fail_on_error])
end

#detect_ci_url(params) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 214

def detect_ci_url(params)
  return params[:ci_url] if params[:ci_url]

  if ENV['BUILD_URL']
    # Jenkins
    return ENV['BUILD_URL']
  elsif ENV['CI_JOB_URL']
    # Gitlab >= 11.1, Runner 0.5
    return ENV['CI_JOB_URL']
  elsif ENV['CI_PROJECT_URL']
    # Gitlab >= 8.10, Runner 0.5
    return "#{ENV['CI_PROJECT_URL']}/-/jobs/#{ENV['CI_BUILD_ID']}"
  end
end

#detect_source(params) ⇒ Object



207
208
209
210
211
212
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 207

def detect_source(params)
  return 'jenkins' if jenkins?
  return 'gitlab-ci' if gitlab?

  params[:source]
end

#gitlab?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 237

def gitlab?
  ENV.key?('GITLAB_CI')
end

#hidden_keys(params) ⇒ Object



201
202
203
204
205
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 201

def hidden_keys(params)
  return [] unless params[:hide_user_token]

  [:token]
end

#http_request(method, uri, body, params) ⇒ Object



155
156
157
158
159
160
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 155

def http_request(method, uri, body, params)
  connection = make_connection(params[:endpoint], params[:verify_ssl])
  connection.run_request(method, uri, body, nil) do |req|
    req.options.timeout = params[:timeout] if params[:timeout]
  end
end

#jenkins?Boolean

Returns:

  • (Boolean)


229
230
231
232
233
234
235
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 229

def jenkins?
  %w(JENKINS_URL JENKINS_HOME).each do |current|
    return true if ENV.key?(current)
  end

  return false
end

#make_connection(endpoint, verify_ssl = true) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 162

def make_connection(endpoint, verify_ssl = true)
  require 'faraday'
  require 'faraday_middleware'

  Faraday.new(url: endpoint, ssl: { verify: verify_ssl }) do |builder|
    builder.request(:multipart)
    builder.request(:url_encoded)
    builder.request(:retry, max: 3, interval: 5)
    builder.response(:json, content_type: /\bjson$/)
    builder.adapter(:net_http)
  end
end


175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 175

def print_table(form, title: 'zealot', hidden_keys: [], remove_empty_value: true)
  rows = form.dup
  rows.keys.each do |k|
    rows.delete(k) if remove_empty_value && !rows[k]
    rows.delete(k) if hidden_keys.include?(k.to_sym)
    rows[k] = rows[k].path if rows[k].is_a?(UploadIO)
  end

  puts Terminal::Table.new(
    title: "Summary for #{title} #{Fastlane::Zealot::VERSION}".green,
    rows: rows
  )
end

#show_error(message, fail_on_error, store_shared_value = true) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 189

def show_error(message, fail_on_error, store_shared_value = true)
  Actions.lane_context[Fastlane::Actions::SharedValues::ZEAALOT_ERROR_MESSAGE] = message if store_shared_value

  if fail_on_error
    UI.user_error!(message)
  else
    UI.error(message)
  end

  false
end

#sync_deivce(params, device) ⇒ Object



140
141
142
143
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 140

def sync_deivce(params, device)
  body = { token: params[:token], name: device.name }
  http_request(:put, "/api/devices/#{device.udid}", body, params)
end

#upload_app(params) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 42

def upload_app(params)
  form = upload_app_params(params)
  # recommand generate collect changelog plugin: https://github.com/icyleaf/fastlane-plugin-ci_changelog
  form[:changelog] = changelog_raw if changelog_raw = ENV['CICL_CHANGLOG']

  print_table(form, title: 'zealot', hidden_keys: hidden_keys(params))

  endpoint = params[:endpoint]
  UI.success("Uploading to #{endpoint} ...")
  connection = make_connection(params[:endpoint], params[:verify_ssl])
  connection.post do |req|
    req.url('/api/apps/upload')
    req.options.timeout = params[:timeout]
    req.body = form
  end
rescue Faraday::ConnectionFailed
  show_error('Can not connecting to Zealot', params[:fail_on_error])
rescue Faraday::TimeoutError
  show_error('Uploading build to Zealot timed out ⏳', params[:fail_on_error])
end

#upload_app_params(params) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 63

def upload_app_params(params)
  form = {
    token: params[:token],
    channel_key: params[:channel_key],
    file: Faraday::UploadIO.new(params[:file], 'application/octet-stream')
  }

  form.merge(avialable_upload_app_params(params))
end

#upload_debug_file(params, file) ⇒ Object



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

def upload_debug_file(params, file)
  form = upload_debug_file_params(params, file)
  print_table(form, title: 'zealot_debug_file')

  endpoint = params[:endpoint]
  UI.success("Uploading to #{endpoint} ...")
  connection = make_connection(params[:endpoint], params[:verify_ssl])
  begin
    connection.post do |req|
      req.url('/api/debug_files/upload')
      req.options.timeout = params[:timeout]
      req.body = form
    end
  rescue Faraday::ConnectionFailed
    show_error('Can not connecting to Zealot', params[:fail_on_error])
  rescue Faraday::TimeoutError
    show_error('Uploading build to Zealot timed out ⏳', params[:fail_on_error])
  end
end

#upload_debug_file_params(params, file) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/fastlane/plugin/zealot/helper/zealot_helper.rb', line 30

def upload_debug_file_params(params, file)
  {
    token: params[:token],
    channel_key: params[:channel_key],
    release_version: params[:release_version],
    build_version: params[:build_version],
    file: Faraday::UploadIO.new(file, 'application/octet-stream')
  }
end