Class: Highway::Environment
- Inherits:
-
Object
- Object
- Highway::Environment
- Defined in:
- lib/highway/environment.rb
Overview
This class wraps ‘ENV` and additionaly provides wrappers and shortcuts to the most interesting values in the runtime environment.
Instance Method Summary collapse
-
#[](key) ⇒ String?
Get value for given key in the ‘ENV`.
-
#[]=(key, value) ⇒ Void
Set value for given key in the ‘ENV`.
-
#ci? ⇒ Boolean
Whether environment is running on a supported CI service.
-
#ci_build_number ⇒ String?
Build number on CI.
-
#ci_build_url ⇒ String?
Build URL on CI.
-
#ci_service ⇒ Symbol?
Detected CI service.
-
#ci_trigger ⇒ Symbol?
Detected trigger type on CI.
-
#find(*keys) ⇒ String?
Find value for any of the given keys.
-
#find_nonempty(*keys) ⇒ String?
Find a non-empty value for any of the given keys.
-
#git_branch ⇒ String?
Git branch that is triggeting CI or value from local repository.
-
#git_commit_hash ⇒ String?
Git commit hash that is triggeting CI or value from local repository.
-
#git_commit_message ⇒ String?
Git commit hash that is triggeting CI or value from local repository.
-
#git_pr_number ⇒ String?
Number of the Pull Request that is triggering CI.
-
#git_pr_source_branch ⇒ String?
Source Git branch of the Pull Request that is triggering CI.
-
#git_pr_source_repo_url ⇒ String?
Source Git repository URL of the Pull Request that is triggering CI.
-
#git_pr_target_branch ⇒ String?
Target Git branch of the Pull Request that is triggering CI.
-
#git_pr_title ⇒ String?
Title of the Pull Request that is triggering CI.
-
#git_pr_url ⇒ String?
URL of the Pull Request that is triggering CI.
-
#git_repo_url ⇒ String?
Git remote repository URL that is triggering CI.
-
#git_tag ⇒ String?
Git tag that is triggeting CI or value from local repository.
-
#include?(*keys) ⇒ Boolean
Check whether any of the given keys exists.
-
#include_nonempty?(*keys) ⇒ Boolean
Check whether any of the given keys exists and is not empty.
-
#verbose? ⇒ Boolean
Whether environment specifies running in verbose mode.
Instance Method Details
#[](key) ⇒ String?
Get value for given key in the ‘ENV`.
20 21 22 |
# File 'lib/highway/environment.rb', line 20 def [](key) ENV[key] end |
#[]=(key, value) ⇒ Void
Set value for given key in the ‘ENV`.
30 31 32 |
# File 'lib/highway/environment.rb', line 30 def []=(key, value) ENV[key] = value end |
#ci? ⇒ Boolean
Whether environment is running on a supported CI service.
82 83 84 |
# File 'lib/highway/environment.rb', line 82 def ci? ci_service != nil end |
#ci_build_number ⇒ String?
Build number on CI.
98 99 100 101 102 103 104 |
# 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 ⇒ String?
Build URL on CI.
109 110 111 112 113 114 115 |
# 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 ⇒ Symbol?
Detected CI service. One of: ‘:bitrise`, `:circle`, `:travis`.
89 90 91 92 93 |
# 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 ⇒ Symbol?
Detected trigger type on CI. One of: ‘:tag`, `:pr`, `:push`.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# 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) ⇒ String?
Find value for any of the given keys.
39 40 41 |
# File 'lib/highway/environment.rb', line 39 def find(*keys) keys.reduce(nil) { |memo, key| memo || self[key] } end |
#find_nonempty(*keys) ⇒ String?
Find a non-empty value for any of the given keys.
48 49 50 51 |
# File 'lib/highway/environment.rb', line 48 def find_nonempty(*keys) result = find(*keys) result if result != nil && !result.empty? end |
#git_branch ⇒ String?
Git branch that is triggeting CI or value from local repository.
153 154 155 156 157 158 159 160 |
# 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 ⇒ String?
Git commit hash that is triggeting CI or value from local repository.
165 166 167 168 169 170 171 172 |
# 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 ⇒ String?
Git commit hash that is triggeting CI or value from local repository.
177 178 179 180 181 182 183 |
# File 'lib/highway/environment.rb', line 177 def 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 ⇒ String?
Number of the Pull Request that is triggering CI.
228 229 230 231 232 233 |
# 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 ⇒ String?
Source Git branch of the Pull Request that is triggering CI.
208 209 210 211 212 213 |
# 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 ⇒ String?
Source Git repository URL of the Pull Request that is triggering CI.
199 200 201 202 203 |
# 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 ⇒ String?
Target Git branch of the Pull Request that is triggering CI.
218 219 220 221 222 223 |
# 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 ⇒ String?
Title of the Pull Request that is triggering CI.
238 239 240 241 242 |
# 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 ⇒ String?
URL of the Pull Request that is triggering CI.
247 248 249 |
# File 'lib/highway/environment.rb', line 247 def git_pr_url normalize_git_url(git_repo_url, git_pr_number) end |
#git_repo_url ⇒ String?
Git remote repository URL that is triggering CI.
188 189 190 191 192 193 194 |
# 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 ⇒ String?
Git tag that is triggeting CI or value from local repository.
141 142 143 144 145 146 147 148 |
# 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) ⇒ Boolean
Check whether any of the given keys exists.
58 59 60 |
# File 'lib/highway/environment.rb', line 58 def include?(*keys) find(*keys) != nil end |
#include_nonempty?(*keys) ⇒ Boolean
Check whether any of the given keys exists and is not empty.
67 68 69 70 |
# File 'lib/highway/environment.rb', line 67 def include_nonempty?(*keys) result = find(*keys) result != nil && !result.empty? end |
#verbose? ⇒ Boolean
Whether environment specifies running in verbose mode.
75 76 77 |
# File 'lib/highway/environment.rb', line 75 def verbose? FastlaneCore::Globals::verbose? end |