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



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

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



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

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



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

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



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

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



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

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



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

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

  params[:source]
end

#gitlab?Boolean

Returns:

  • (Boolean)


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

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

#hidden_keys(params) ⇒ Object



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

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

  [:token]
end

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



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

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)


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

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



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

def make_connection(endpoint, verify_ssl = true)
  require 'faraday'
  require 'faraday/multipart'
  require 'faraday/net_http'
  require 'faraday/retry'

  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


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

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?(Faraday::Multipart::FilePart)
  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



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

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



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

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

#upload_app(params) ⇒ Object



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

def upload_app(params)
  form = upload_app_params(params)
  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 service, make sure it be health to upload.', 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



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

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

  form.merge(avialable_upload_app_params(params))
end

#upload_debug_file(params, file) ⇒ Object



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

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



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

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::Multipart::FilePart.new(file, 'application/octet-stream')
  }
end