Class: Fastlane::Helper::EmergeHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/emerge/helper/emerge_helper.rb

Constant Summary collapse

API_URL =
'https://api.emergetools.com/upload'.freeze

Class Method Summary collapse

Class Method Details

.clean_params(params) ⇒ Object



70
71
72
# File 'lib/fastlane/plugin/emerge/helper/emerge_helper.rb', line 70

def self.clean_params(params)
  params.reject { |_, v| v.nil? }
end

.copy_config(config_path, tmp_dir) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fastlane/plugin/emerge/helper/emerge_helper.rb', line 55

def self.copy_config(config_path, tmp_dir)
  return if config_path.nil?

  expanded_path = File.expand_path(config_path)
  unless File.exist?(expanded_path)
    UI.error("No config file found at path '#{expanded_path}'.\nUploading without config file")
    return
  end

  emerge_config_path = "#{tmp_dir}/emerge_config.yaml"
  FileUtils.cp(expanded_path, emerge_config_path)
end

.create_upload(api_token, params) ⇒ Object



82
83
84
85
# File 'lib/fastlane/plugin/emerge/helper/emerge_helper.rb', line 82

def self.create_upload(api_token, params)
  response = Faraday.post(API_URL, params.to_json, headers(api_token, params, 'application/json'))
  parse_response(response)
end

.handle_upload_response(api_token, response, file_path) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fastlane/plugin/emerge/helper/emerge_helper.rb', line 109

def self.handle_upload_response(api_token, response, file_path)
  upload_url = response.fetch('uploadURL')
  upload_id = response.fetch('upload_id')

  warning = response.dig('warning')
  if warning
    UI.important(warning)
  end

  UI.message('Starting zip file upload')
  upload_file(api_token, upload_url, file_path)
  upload_id
end

.headers(api_token, params, content_type) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/fastlane/plugin/emerge/helper/emerge_helper.rb', line 87

def self.headers(api_token, params, content_type)
  {
    'Content-Type' => content_type,
    'X-API-Token' => api_token,
    'User-Agent' => "fastlane-plugin-emerge/#{Fastlane::Emerge::VERSION}"
  }
end

.make_git_paramsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fastlane/plugin/emerge/helper/emerge_helper.rb', line 33

def self.make_git_params
  git_result = if Helper::Github.is_supported_github_event?
                 UI.message("Fetching Git info from Github event")
                 GitResult.new(
                   sha: Helper::Github.sha,
                   base_sha: Helper::Github.base_sha,
                   branch: Helper::Github.branch,
                   pr_number: Helper::Github.pr_number,
                   repo_name: Helper::Github.repo_name
                 )
               else
                 UI.message("Fetching Git info from system Git")
                 GitResult.new(
                   sha: Helper::Git.sha,
                   base_sha: Helper::Git.base_sha,
                   branch: Helper::Git.branch
                 )
               end
  UI.message("Got git result #{git_result.inspect}")
  git_result
end

.parse_response(response) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fastlane/plugin/emerge/helper/emerge_helper.rb', line 95

def self.parse_response(response)
  case response.status
  when 200
    JSON.parse(response.body)
  when 400
    error_message = JSON.parse(response.body)['errorMessage']
    raise "Invalid parameters: #{error_message}"
  when 401, 403
    raise 'Invalid API token'
  else
    raise "Creating upload failed with status #{response.status}"
  end
end

.perform_upload(api_token, params, file_path) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/fastlane/plugin/emerge/helper/emerge_helper.rb', line 23

def self.perform_upload(api_token, params, file_path)
  cleaned_params = clean_params(params)
  print_summary(cleaned_params)

  upload_response = create_upload(api_token, cleaned_params)
  handle_upload_response(api_token, upload_response, file_path)
rescue StandardError => e
  UI.user_error!(e.message)
end


74
75
76
77
78
79
80
# File 'lib/fastlane/plugin/emerge/helper/emerge_helper.rb', line 74

def self.print_summary(params)
  FastlaneCore::PrintTable.print_values(
    config: params,
    hide_keys: [],
    title: "Summary for Emerge Upload #{Fastlane::Emerge::VERSION}"
  )
end

.upload_file(api_token, upload_url, file_path) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/fastlane/plugin/emerge/helper/emerge_helper.rb', line 123

def self.upload_file(api_token, upload_url, file_path)
  response = Faraday.put(upload_url) do |req|
    req.headers = headers(api_token, nil, 'application/zip')
    req.headers['Content-Length'] = File.size(file_path).to_s
    req.body = Faraday::UploadIO.new(file_path, 'application/zip')
  end

  raise "Uploading zip file failed #{response.status}" unless response.status == 200
end