Module: PactBroker::Client::Git

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

Constant Summary collapse

COMMAND =
'git rev-parse --abbrev-ref 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}
BUILD_URL_ENV_VAR_NAMES =
%w{BUILDKITE_BUILD_URL CIRCLE_BUILD_URL TRAVIS_BUILD_WEB_URL BUILD_URL }

Class Method Summary collapse

Class Method Details

.branch(options) ⇒ Object



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

def self.branch(options)
  find_branch_from_known_env_vars || find_branch_from_env_var_ending_with_branch || branch_from_git_command(options[:raise_error])
end

.branch_from_git_command(raise_error) ⇒ Object



71
72
73
74
75
# File 'lib/pact_broker/client/git.rb', line 71

def self.branch_from_git_command(raise_error)
  branch_names = execute_and_parse_command(raise_error)
  validate_branch_names(branch_names) if raise_error
  branch_names.size == 1 ? branch_names[0] : nil
end

.commitObject



33
34
35
# File 'lib/pact_broker/client/git.rb', line 33

def self.commit
  find_commit_from_env_vars
end

.execute_and_parse_command(raise_error) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pact_broker/client/git.rb', line 91

def self.execute_and_parse_command(raise_error)
  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
  if raise_error
    raise PactBroker::Client::Error, "Could not determine current git branch using command `#{COMMAND}`. #{e.class} #{e.message}"
  else
    return []
  end
end

.execute_git_commandObject



87
88
89
# File 'lib/pact_broker/client/git.rb', line 87

def self.execute_git_command
  `#{COMMAND}`
end

.find_branch_from_env_var_ending_with_branchObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/pact_broker/client/git.rb', line 51

def self.find_branch_from_env_var_ending_with_branch
  values = ENV.keys
    .select{ |env_var_name| env_var_name.end_with?("_BRANCH") }
    .collect{ |env_var_name| value_from_env_var(env_var_name) }.compact
  if values.size == 1
    values.first
  else
    nil
  end
end

.find_branch_from_known_env_varsObject



47
48
49
# File 'lib/pact_broker/client/git.rb', line 47

def self.find_branch_from_known_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



43
44
45
# File 'lib/pact_broker/client/git.rb', line 43

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



77
78
79
80
81
82
83
84
85
# File 'lib/pact_broker/client/git.rb', line 77

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



62
63
64
65
66
67
68
69
# File 'lib/pact_broker/client/git.rb', line 62

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