Module: PactBroker::Client::Git

Defined in:
lib/pact_broker/client/git.rb

Constant Summary collapse

COMMAND =
'git name-rev --name-only HEAD'.freeze
BRANCH_ENV_VAR_NAMES =
%w{BUILDKITE_BRANCH CIRCLE_BRANCH TRAVIS_BRANCH GIT_BRANCH GIT_LOCAL_BRANCH APPVEYOR_REPO_BRANCH CI_COMMIT_REF_NAME BITBUCKET_BRANCH}.freeze
COMMIT_ENV_VAR_NAMES =
%w{BUILDKITE_COMMIT CIRCLE_SHA1 TRAVIS_COMMIT GIT_COMMIT APPVEYOR_REPO_COMMIT CI_COMMIT_ID BITBUCKET_COMMIT}

Class Method Summary collapse

Class Method Details

.branchObject



30
31
32
# File 'lib/pact_broker/client/git.rb', line 30

def self.branch
  find_branch_from_env_vars || branch_from_git_command
end

.branch_from_git_commandObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pact_broker/client/git.rb', line 53

def self.branch_from_git_command
  branch_names = nil
  begin
    branch_names = execute_git_command
      .split("\n")
      .collect(&:strip)
      .reject(&:empty?)
      .collect(&:split)
      .collect(&:first)
      .collect{ |line| line.gsub(/^origin\//, '') }
      .reject{ |line| line == "HEAD" }

  rescue StandardError => e
    raise PactBroker::Client::Error, "Could not determine current git branch using command `#{COMMAND}`. #{e.class} #{e.message}"
  end

  validate_branch_names(branch_names)
  branch_names[0]
end

.commitObject



26
27
28
# File 'lib/pact_broker/client/git.rb', line 26

def self.commit
  find_commit_from_env_vars
end

.execute_git_commandObject



83
84
85
# File 'lib/pact_broker/client/git.rb', line 83

def self.execute_git_command
  `#{COMMAND}`
end

.find_branch_from_env_varsObject



40
41
42
# File 'lib/pact_broker/client/git.rb', line 40

def self.find_branch_from_env_vars
  BRANCH_ENV_VAR_NAMES.collect { |env_var_name| value_from_env_var(env_var_name) }.compact.first
end

.find_commit_from_env_varsObject

private



36
37
38
# File 'lib/pact_broker/client/git.rb', line 36

def self.find_commit_from_env_vars
  COMMIT_ENV_VAR_NAMES.collect { |env_var_name| value_from_env_var(env_var_name) }.compact.first
end

.validate_branch_names(branch_names) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/pact_broker/client/git.rb', line 73

def self.validate_branch_names(branch_names)
  if branch_names.size == 0
    raise PactBroker::Client::Error, "Command `#{COMMAND}` didn't return anything that could be identified as the current branch."
  end

  if branch_names.size > 1
    raise PactBroker::Client::Error, "Command `#{COMMAND}` returned multiple branches: #{branch_names.join(", ")}. You will need to get the branch name another way."
  end
end

.value_from_env_var(env_var_name) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/pact_broker/client/git.rb', line 44

def self.value_from_env_var(env_var_name)
  val = ENV[env_var_name]
  if val && val.strip.size > 0
    val
  else
    nil
  end
end