Class: DefaultCIBranch

Inherits:
Object
  • Object
show all
Defined in:
lib/hatchet.rb

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV) ⇒ DefaultCIBranch

Returns a new instance of DefaultCIBranch.



25
26
27
# File 'lib/hatchet.rb', line 25

def initialize(env: ENV)
  @env = env
end

Instance Method Details

#callObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hatchet.rb', line 29

def call
  # https://circleci.com/docs/variables
  return @env['CIRCLE_BRANCH'] if @env['CIRCLE_BRANCH']
  # https://docs.github.com/en/actions/learn-github-actions/environment-variables
  # GITHUB_HEAD_REF is provided for PRs, but blank for branch actions.
  return @env['GITHUB_HEAD_REF'] if @env['GITHUB_HEAD_REF'] && !@env['GITHUB_HEAD_REF']&.empty?
  # GITHUB_REF_NAME is incorrect on PRs (`1371/merge`), but correct for branch actions.
  return @env['GITHUB_REF_NAME'] if @env['GITHUB_REF_NAME']
  # https://devcenter.heroku.com/articles/heroku-ci#immutable-environment-variables
  return @env['HEROKU_TEST_RUN_BRANCH'] if @env['HEROKU_TEST_RUN_BRANCH']
  # TRAVIS_BRANCH works fine unless the build is a pull-request. In that case, it will contain the target branch
  # not the actual pull-request branch! TRAVIS_PULL_REQUEST_BRANCH contains the correct branch but will be empty
  # for push builds. See: https://docs.travis-ci.com/user/environment-variables/
  return @env['TRAVIS_PULL_REQUEST_BRANCH'] if @env['TRAVIS_PULL_REQUEST_BRANCH'] && !@env['TRAVIS_PULL_REQUEST_BRANCH']&.empty?
  return @env['TRAVIS_BRANCH'] if @env['TRAVIS_BRANCH']
end