Class: EnvBranch::Base

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

Overview

Branch information object from environment variable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase #initialize { ... } ⇒ Base

Build branch information object from environment variables

Overloads:

  • #initializeBase

    Returns Branch information object.

    Examples:

    without user defined block

    env_branch = EnvBranch::Base.new
  • #initialize { ... } ⇒ Base

    Returns Branch information object.

    Examples:

    with user defined block

    env_branch =
      EnvBranch::Base.new do
        if ENV['USER_DEFINED_BRANCH'] &&
          !ENV['USER_DEFINED_BRANCH'].empty?
          ENV['USER_DEFINED_BRANCH']
        end
      end

    Yields:

    • user defined block



28
29
30
31
32
33
34
35
# File 'lib/env_branch/base.rb', line 28

def initialize
  @branch_name =
    if block_given?
      yield || fetch_branch_name
    else
      fetch_branch_name
    end
end

Instance Attribute Details

#branch_nameString? (readonly)

Returns branch name or nil.

Returns:

  • (String, nil)

    branch name or nil



5
6
7
# File 'lib/env_branch/base.rb', line 5

def branch_name
  @branch_name
end

Instance Method Details

#branch?Boolean

Returns true if this has branch name.

Returns:

  • (Boolean)

    true if this has branch name



68
69
70
# File 'lib/env_branch/base.rb', line 68

def branch?
  !branch_name.nil?
end

#fetch_branch_nameString?

Fetch branch name from environment variables

travis-ci.org:

ENV['TRAVIS_BRANCH']

circleci.com:

ENV['CIRCLE_BRANCH']

GitHub pull request builder plugin (for Jenkins):

ENV['ghprbSourceBranch']


57
58
59
60
61
62
63
64
65
# File 'lib/env_branch/base.rb', line 57

def fetch_branch_name
  if ENV['TRAVIS_BRANCH'] && !ENV['TRAVIS_BRANCH'].empty?
    ENV['TRAVIS_BRANCH']
  elsif ENV['CIRCLE_BRANCH'] && !ENV['CIRCLE_BRANCH'].empty?
    ENV['CIRCLE_BRANCH']
  elsif ENV['ghprbSourceBranch'] && !ENV['ghprbSourceBranch'].empty?
    ENV['ghprbSourceBranch']
  end
end