Class: Fastlane::Helper::AppliveryHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/applivery/helper/applivery_helper.rb

Constant Summary collapse

DEFAULT_DOMAIN =

class methods that you define here become available in your action as Helper::AppliveryHelper.your_method

"applivery.io".freeze
UPLOAD_MIME_TYPE =
"application/octet-stream".freeze
OPEN_TIMEOUT =

Seconds we wait for the connection to be established. The upload itself is limited by the timeout option of the action instead.

60
INTEGRATION_NUMBER_ENV_VARS =

Environment variables holding the build number of the current CI job, ordered by precedence.

[
  "XCS_INTEGRATION_NUMBER", # Xcode Server
  "BUILD_NUMBER",           # Jenkins
  "TRAVIS_BUILD_NUMBER",    # Travis CI
  "GITHUB_RUN_NUMBER",      # GitHub Actions
  "CI_PIPELINE_IID",        # GitLab CI
  "CIRCLE_BUILD_NUM",       # CircleCI
  "BITRISE_BUILD_NUMBER",   # Bitrise
  "BUILD_BUILDID"           # Azure Pipelines
].freeze

Class Method Summary collapse

Class Method Details

.add_git_remoteObject



133
134
135
# File 'lib/fastlane/plugin/applivery/helper/applivery_helper.rb', line 133

def self.add_git_remote
  return git_command("config", "--get", "remote.origin.url")
end

.file_part(path, mime_type = UPLOAD_MIME_TYPE) ⇒ Object

Wraps the build in the multipart class provided by the installed Faraday version: Faraday::Multipart::FilePart (Faraday 2 and Faraday >= 1.10), Faraday::FilePart or the long deprecated Faraday::UploadIO.



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fastlane/plugin/applivery/helper/applivery_helper.rb', line 88

def self.file_part(path, mime_type = UPLOAD_MIME_TYPE)
  if defined?(Faraday::Multipart::FilePart)
    Faraday::Multipart::FilePart.new(path, mime_type)
  elsif defined?(Faraday::FilePart)
    Faraday::FilePart.new(path, mime_type)
  elsif defined?(Faraday::UploadIO)
    Faraday::UploadIO.new(path, mime_type)
  else
    UI.user_error!("Faraday #{Faraday::VERSION} does not provide multipart support. Please add `gem 'faraday-multipart'` to your Gemfile and run `bundle install`")
  end
end

.get_base_domain(tenant = nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/fastlane/plugin/applivery/helper/applivery_helper.rb', line 48

def self.get_base_domain(tenant = nil)
  tenant = tenant.to_s.strip
  return DEFAULT_DOMAIN if tenant.empty?
  return tenant if tenant.include?(".")
  return "#{tenant}.#{DEFAULT_DOMAIN}"
end

.get_integration_numberObject



55
56
57
58
59
60
61
62
# File 'lib/fastlane/plugin/applivery/helper/applivery_helper.rb', line 55

def self.get_integration_number
  INTEGRATION_NUMBER_ENV_VARS.each do |env_var|
    integration_number = ENV[env_var].to_s.strip
    return integration_number unless integration_number.empty?
  end

  return ""
end

.git_branchObject

fastlane resolves the branch from the environment of the CI when possible, git is only asked when it has nothing to report.



117
118
119
120
121
# File 'lib/fastlane/plugin/applivery/helper/applivery_helper.rb', line 117

def self.git_branch
  branch = fastlane_git_value { Actions.git_branch }
  return branch unless branch.empty?
  return git_command("rev-parse", "--abbrev-ref", "HEAD")
end

.git_commitObject



123
124
125
# File 'lib/fastlane/plugin/applivery/helper/applivery_helper.rb', line 123

def self.git_commit
  return git_command("rev-parse", "--short", "HEAD")
end

.git_messageObject



127
128
129
130
131
# File 'lib/fastlane/plugin/applivery/helper/applivery_helper.rb', line 127

def self.git_message
  message = fastlane_git_value { Actions.last_git_commit_message }
  return message unless message.empty?
  return git_command("log", "-1", "--pretty=%B")
end

.git_tagObject

Returns the tag pointing at HEAD, or an empty String when the current commit is not tagged (or the repository has no tags at all).



139
140
141
142
143
144
145
146
147
# File 'lib/fastlane/plugin/applivery/helper/applivery_helper.rb', line 139

def self.git_tag
  tag = git_command("describe", "--abbrev=0", "--tags")
  return "" if tag.empty?

  tag_commit = git_command("rev-list", "-n", "1", tag)
  head_commit = git_command("rev-parse", "HEAD")
  return tag if !tag_commit.empty? && tag_commit == head_commit
  return ""
end

.parse_error(error, status = nil) ⇒ Object

error is the error object returned by the API. Anything else (a missing body, an HTML error page from a proxy, ...) ends up in the generic message including the HTTP status code.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/fastlane/plugin/applivery/helper/applivery_helper.rb', line 179

def self.parse_error(error, status = nil)
  unless error.kind_of?(Hash)
    details = error.to_s.strip
    details = details.empty? ? "" : ": #{details}"
    UI.user_error!("Upload failed unexpectedly. [HTTP #{status || 'unknown'}]#{details}")
  end

  case error["code"].to_i
  when 5006
    UI.user_error!("Upload failed. The build path seems to be wrong or the file is invalid")
  when 4004
    UI.user_error!("The app_token is not valid. Please, go to your app settings and double-check the integration tokens")
  when 4002
    UI.user_error!("The app_token is empty. Please, go to your app Settings->Integrations to generate a token")
  else
    UI.user_error!("Upload failed. [#{error['code']}]: #{error['message']}")
  end
end

.parse_response_body(response) ⇒ Object

Parses the response body, which is a Hash when a JSON middleware is installed in the connection and a String otherwise.



102
103
104
105
106
107
108
109
110
111
# File 'lib/fastlane/plugin/applivery/helper/applivery_helper.rb', line 102

def self.parse_response_body(response)
  body = response.body
  return body if body.kind_of?(Hash)

  parsed_body = JSON.parse(body.to_s)
  parsed_body.kind_of?(Hash) ? parsed_body : {}
rescue JSON::ParserError
  UI.verbose("Response body is not valid JSON: #{response.body}")
  return {}
end

.platformObject



39
40
41
42
43
44
45
46
# File 'lib/fastlane/plugin/applivery/helper/applivery_helper.rb', line 39

def self.platform
  platform = Actions.lane_context[Actions::SharedValues::PLATFORM_NAME]
  if platform == :ios or platform.nil?
    return "ios"
  elsif platform == :android
    return "android"
  end
end

.upload_connection(tenant = nil, timeout = nil) ⇒ Object

Connection used to upload the build. Both the read and the write timeouts are set from timeout so uploading big builds over a slow network doesn't fail after Net::HTTP's default 60 seconds.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fastlane/plugin/applivery/helper/applivery_helper.rb', line 69

def self.upload_connection(tenant = nil, timeout = nil)
  url = "https://upload.#{get_base_domain(tenant)}"
  UI.verbose("Applivery upload URL: #{url}")

  request_options = { open_timeout: OPEN_TIMEOUT }
  request_options[:timeout] = timeout if timeout

  Faraday.new(url: url, request: request_options) do |faraday|
    faraday.request(:multipart)
    faraday.request(:url_encoded)
    faraday.adapter(:net_http)
  end
rescue Faraday::Error => e
  UI.user_error!("Could not prepare the upload request: #{e.message}. Please add `gem 'faraday-multipart'` to your Gemfile and run `bundle install`")
end