class Highway::Environment

This class wraps `ENV` and additionaly provides wrappers and shortcuts to the most interesting values in the runtime environment.

Public Instance Methods

[](key) click to toggle source

Get value for given key in the `ENV`.

@param key [String] A key.

@return [String, nil]

# File lib/highway/environment.rb, line 20
def [](key)
  ENV[key]
end
[]=(key, value) click to toggle source

Set value for given key in the `ENV`.

@param key [String] A key. @param value [String, nil] A value.

@return [Void]

# File lib/highway/environment.rb, line 30
def []=(key, value)
  ENV[key] = value
end
ci?() click to toggle source

Whether environment is running on a supported CI service.

@return [Boolean]

# File lib/highway/environment.rb, line 82
def ci?
  ci_service != nil
end
ci_build_number() click to toggle source

Build number on CI.

@return [String, nil]

# File lib/highway/environment.rb, line 98
def ci_build_number
  case ci_service
    when :bitrise then find_nonempty("BITRISE_BUILD_NUMBER")
    when :circle then find_nonempty("CIRCLE_BUILD_NUM")
    when :travis then find_nonempty("TRAVIS_BUILD_NUMBER")
  end
end
ci_build_url() click to toggle source

Build URL on CI.

@return [String, nil]

# File lib/highway/environment.rb, line 109
def ci_build_url
  case ci_service
    when :bitrise then find_nonempty("BITRISE_BUILD_URL")
    when :circle then find_nonempty("CIRCLE_BUILD_URL")
    when :travis then find_nonempty("TRAVIS_BUILD_WEB_URL")
  end
end
ci_service() click to toggle source

Detected CI service. One of: `:bitrise`, `:circle`, `:travis`.

@return [Symbol, nil].

# File lib/highway/environment.rb, line 89
def ci_service
  return :bitrise if include?("BITRISE_IO")
  return :circle if include?("CIRCLECI")
  return :travis if include?("TRAVIS")
end
ci_trigger() click to toggle source

Detected trigger type on CI. One of: `:tag`, `:pr`, `:push`.

@return [Symbol, nil]

# File lib/highway/environment.rb, line 120
def ci_trigger
  if ci_service == :bitrise
    return :tag if include_nonempty?("BITRISE_GIT_TAG")
    return :pr if include_nonempty?("BITRISE_PULL_REQUEST")
    return :push if include_nonempty?("BITRISE_GIT_BRANCH")
    return :manual
  elsif ci_service == :circle
    return :tag if include_nonempty?("CIRCLE_TAG")
    return :push if include_nonempty?("CIRCLE_BRANCH")
    return :manual
  elsif ci_service == :travis
    return :tag if include_nonempty?("TRAVIS_TAG")
    return :pr if find_nonempty("TRAVIS_EVENT_TYPE") == "pull_request"
    return :push if find_nonempty("TRAVIS_EVENT_TYPE") == "push"
    return :manual
  end
end
find(*keys) click to toggle source

Find value for any of the given keys.

@param *keys [String] Keys to look for.

@return [String, nil]

# File lib/highway/environment.rb, line 39
def find(*keys)
  keys.reduce(nil) { |memo, key| memo || self[key] }
end
find_nonempty(*keys) click to toggle source

Find a non-empty value for any of the given keys.

@param *keys [String] Keys to look for.

@return [String, nil]

# File lib/highway/environment.rb, line 48
def find_nonempty(*keys)
  result = find(*keys)
  result if result != nil && !result.empty?
end
git_branch() click to toggle source

Git branch that is triggeting CI or value from local repository.

@return [String, nil]

# File lib/highway/environment.rb, line 153
def git_branch
  case ci_service
    when :bitrise then find_nonempty("BITRISE_GIT_BRANCH")
    when :circle then find_nonempty("CIRCLE_BRANCH")
    when :travis then find_nonempty("TRAVIS_PULL_REQUEST_BRANCH", "TRAVIS_BRANCH")
    else safe_sh("git", "rev-parse --abbrev-ref HEAD")
  end
end
git_commit_hash() click to toggle source

Git commit hash that is triggeting CI or value from local repository.

@return [String, nil]

# File lib/highway/environment.rb, line 165
def git_commit_hash
  case ci_service
    when :bitrise then find_nonempty("GIT_CLONE_COMMIT_HASH")
    when :circle then find_nonempty("CIRCLE_SHA1")
    when :travis then find_nonempty("TRAVIS_COMMIT")
    else safe_sh("git", "rev-parse HEAD")
  end
end
git_commit_message() click to toggle source

Git commit hash that is triggeting CI or value from local repository.

@return [String, nil]

# File lib/highway/environment.rb, line 177
def git_commit_message
  case ci_service
    when :bitrise then find_nonempty("GIT_CLONE_COMMIT_MESSAGE_SUBJECT").split("\n").first
    when :travis then find_nonempty("TRAVIS_COMMIT_MESSAGE").split("\n").first
    else safe_sh("git", "log -1 --pretty=%B").split("\n").first
  end
end
git_pr_number() click to toggle source

Number of the Pull Request that is triggering CI.

@return [String, nil]

# File lib/highway/environment.rb, line 228
def git_pr_number
  case ci_service
    when :bitrise then find_nonempty("BITRISE_PULL_REQUEST")
    when :travis then find_nonempty("TRAVIS_PULL_REQUEST")
  end
end
git_pr_source_branch() click to toggle source

Source Git branch of the Pull Request that is triggering CI.

@return [String, nil]

# File lib/highway/environment.rb, line 208
def git_pr_source_branch
  case ci_service
    when :bitrise then find_nonempty("BITRISE_GIT_BRANCH")
    when :travis then find_nonempty("TRAVIS_PULL_REQUEST_BRANCH")
  end
end
git_pr_source_repo_url() click to toggle source

Source Git repository URL of the Pull Request that is triggering CI.

@return [String, nil]

# File lib/highway/environment.rb, line 199
def git_pr_source_repo_url
  case ci_service
    when :bitrise then normalize_git_url(find_nonempty("BITRISEIO_PULL_REQUEST_REPOSITORY_URL"))
  end
end
git_pr_target_branch() click to toggle source

Target Git branch of the Pull Request that is triggering CI.

@return [String, nil]

# File lib/highway/environment.rb, line 218
def git_pr_target_branch
  case ci_service
    when :bitrise then find_nonempty("BITRISEIO_GIT_BRANCH_DEST")
    when :travis then find_nonempty("TRAVIS_BRANCH")
  end
end
git_pr_title() click to toggle source

Title of the Pull Request that is triggering CI.

@return [String, nil]

# File lib/highway/environment.rb, line 238
def git_pr_title
  case ci_service
    when :bitrise then find_nonempty("BITRISE_GIT_MESSAGE")
  end
end
git_pr_url() click to toggle source

URL of the Pull Request that is triggering CI.

@return [String, nil]

# File lib/highway/environment.rb, line 247
def git_pr_url
  normalize_git_url(git_repo_url, git_pr_number)
end
git_repo_url() click to toggle source

Git remote repository URL that is triggering CI.

@return [String, nil]

# File lib/highway/environment.rb, line 188
def git_repo_url
  case ci_service
    when :bitrise then normalize_git_url(find_nonempty("GIT_REPOSITORY_URL"))
    when :circle then normalize_git_url(find_nonempty("CIRCLE_REPOSITORY_URL"))
    else normalize_git_url(safe_sh("git", "remote get-url origin"))
  end
end
git_tag() click to toggle source

Git tag that is triggeting CI or value from local repository.

@return [String, nil]

# File lib/highway/environment.rb, line 141
def git_tag
  case ci_service
    when :bitrise then find_nonempty("BITRISE_GIT_TAG")
    when :circle then find_nonempty("CIRCLE_TAG")
    when :travis then find_nonempty("TRAVIS_TAG")
    else safe_sh("git", "describe --exact-match --tags HEAD")
  end
end
include?(*keys) click to toggle source

Check whether any of the given keys exists.

@param *keys [String] Keys to look for.

@param [Boolean]

# File lib/highway/environment.rb, line 58
def include?(*keys)
  find(*keys) != nil
end
include_nonempty?(*keys) click to toggle source

Check whether any of the given keys exists and is not empty.

@param *keys [String] Keys to look for.

@param [Boolean]

# File lib/highway/environment.rb, line 67
def include_nonempty?(*keys)
  result = find(*keys)
  result != nil && !result.empty?
end
verbose?() click to toggle source

Whether environment specifies running in verbose mode.

@return [Boolean]

# File lib/highway/environment.rb, line 75
def verbose?
  FastlaneCore::Globals::verbose?
end

Private Instance Methods

normalize_git_url(uri_string, pr_number = nil) click to toggle source
# File lib/highway/environment.rb, line 258
def normalize_git_url(uri_string, pr_number = nil)

  return nil unless uri_string

  uri = URI.parse(uri_string) if uri_string.start_with?("https://")
  uri = URI::SshGit.parse(uri_string) if uri_string.start_with?("git@")

  return nil unless uri

  host = uri.host
  repo_path = File.join(File.dirname(uri.path), File.basename(uri.path, ".git"))

  return File.join("https://#{host}", repo_path) unless pr_number

  pr_path = "pull/#{pr_number}" if host == "github.com"
  pr_path = "merge_requests/#{pr_number}" if host == "gitlab.com"
  pr_path = "pull-requests/#{pr_number}" if host == "bitbucket.org"

  return File.join("https://#{host}", repo_path, pr_path) if pr_path

end
safe_sh(executable, *command) click to toggle source
# File lib/highway/environment.rb, line 253
def safe_sh(executable, *command)
  result = `which #{executable} > /dev/null && #{executable} #{command.join(" ")} 2> /dev/null`.strip
  result if !result.empty?
end