Class: Highway::Environment

Inherits:
Object
  • Object
show all
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

Instance Method Details

#[](key) ⇒ String?

Get value for given key in the ‘ENV`.

Parameters:

  • key (String)

    A key.

Returns:

  • (String, nil)


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`.

Parameters:

  • key (String)

    A key.

  • value (String, nil)

    A value.

Returns:

  • (Void)


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.

Returns:

  • (Boolean)


82
83
84
# File 'lib/highway/environment.rb', line 82

def ci?
  ci_service != nil
end

#ci_build_numberString?

Build number on CI.

Returns:

  • (String, nil)


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_urlString?

Build URL on CI.

Returns:

  • (String, nil)


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_serviceSymbol?

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

Returns:

  • (Symbol, nil)

    .



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_triggerSymbol?

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

Returns:

  • (Symbol, nil)


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.

Parameters:

  • *keys (String)

    Keys to look for.

Returns:

  • (String, nil)


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.

Parameters:

  • *keys (String)

    Keys to look for.

Returns:

  • (String, nil)


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_branchString?

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

Returns:

  • (String, nil)


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_hashString?

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

Returns:

  • (String, nil)


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_messageString?

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

Returns:

  • (String, nil)


177
178
179
180
181
182
183
# 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_numberString?

Number of the Pull Request that is triggering CI.

Returns:

  • (String, nil)


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_branchString?

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

Returns:

  • (String, nil)


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_urlString?

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

Returns:

  • (String, nil)


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_branchString?

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

Returns:

  • (String, nil)


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_titleString?

Title of the Pull Request that is triggering CI.

Returns:

  • (String, nil)


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_urlString?

URL of the Pull Request that is triggering CI.

Returns:

  • (String, nil)


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_urlString?

Git remote repository URL that is triggering CI.

Returns:

  • (String, nil)


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_tagString?

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

Returns:

  • (String, nil)


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.

Parameters:

  • *keys (String)

    Keys to look for.

  • (Boolean)

Returns:

  • (Boolean)


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.

Parameters:

  • *keys (String)

    Keys to look for.

  • (Boolean)

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


75
76
77
# File 'lib/highway/environment.rb', line 75

def verbose?
  FastlaneCore::Globals::verbose?
end