Skip to content

Commit

Permalink
feat: attempt to get git branch name from environment variable first …
Browse files Browse the repository at this point in the history
…before running git
  • Loading branch information
bethesque committed Feb 12, 2020
1 parent fc6a2d0 commit d512ef0
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 12 deletions.
69 changes: 57 additions & 12 deletions lib/pact_broker/client/git.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,69 @@
require 'pact_broker/client/error'

# BUILDKITE_BRANCH BUILDKITE_COMMIT https://buildkite.com/docs/pipelines/environment-variables
# CIRCLE_BRANCH CIRCLE_SHA1 https://circleci.com/docs/2.0/env-vars/
# TRAVIS_COMMIT TRAVIS_BRANCH - TRAVIS_PULL_REQUEST_BRANCH TRAVIS_PULL_REQUEST_SHA https://docs.travis-ci.com/user/environment-variables/
# GIT_COMMIT GIT_BRANCH https://wiki.jenkins.io/display/JENKINS/Building+a+software+project
# GIT_COMMIT GIT_LOCAL_BRANCH https://hudson.eclipse.org/webtools/env-vars.html/
# APPVEYOR_REPO_COMMIT APPVEYOR_REPO_BRANCH https://www.appveyor.com/docs/environment-variables/
# bamboo.repository.git.branch https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html
=begin
# Keep in sync with pact-provider-verifier/lib/pact/provider_verifier/git.rb
BUILDKITE_BRANCH BUILDKITE_COMMIT https://buildkite.com/docs/pipelines/environment-variables
CIRCLE_BRANCH CIRCLE_SHA1 https://circleci.com/docs/2.0/env-vars/
TRAVIS_COMMIT TRAVIS_BRANCH - TRAVIS_PULL_REQUEST_BRANCH TRAVIS_PULL_REQUEST_SHA https://docs.travis-ci.com/user/environment-variables/
GIT_COMMIT GIT_BRANCH https://wiki.jenkins.io/display/JENKINS/Building+a+software+project
GIT_COMMIT GIT_LOCAL_BRANCH https://hudson.eclipse.org/webtools/env-vars.html/
APPVEYOR_REPO_COMMIT APPVEYOR_REPO_BRANCH https://www.appveyor.com/docs/environment-variables/
CI_COMMIT_REF_NAME https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
CI_BRANCH CI_COMMIT_ID https://documentation.codeship.com/pro/builds-and-configuration/environment-variables/
bamboo.repository.git.branch https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html
=end

# Keep in sync with pact-provider-verifier/lib/pact/provider_verifier/git.rb
module PactBroker
module Client
module Git
COMMAND = 'git rev-parse --abbrev-ref HEAD'
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}.freeze

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

# private

def self.find_branch_from_env_vars
BRANCH_ENV_VAR_names.collect { |env_var_name| branch_from_env_var(env_var_name) }.compact.first
end

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

def self.branch_from_git_command
branch_name = nil
begin
branch_name = execute_git_command.strip
rescue StandardError => e
raise PactBroker::Client::Error, "Could not determine current git branch using command `#{COMMAND}`. #{e.class} #{e.message}"
end

validate_branch_name(branch_name)
branch_name
end

def self.validate_branch_name(branch_name)
if !branch_name || branch_name.size == 0
raise PactBroker::Client::Error, "Command `#{COMMAND}` returned an empty string when trying to determine the git branch name. This is most likely not the value you want. You will need to get the branch name another way."
end

if branch_name == "HEAD"
raise PactBroker::Client::Error, "Command `#{COMMAND}` returned 'HEAD' when trying to determine the git branch name. This is probably because the repository is in detatched HEAD state. HEAD is most likely not the value you want. You will need to get the branch name another way."
end
end

def self.execute_git_command
`#{COMMAND}`
end
end
end
Expand Down
62 changes: 62 additions & 0 deletions spec/lib/pact_broker/client/git_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'pact_broker/client/git'

module PactBroker
module Client
module Git
describe ".branch" do
before do
allow(ENV).to receive(:[]).and_call_original
end

subject { Git.branch }

context "when there is a known environment variable for the branch" do
before do
allow(ENV).to receive(:[]).with("BUILDKITE_BRANCH").and_return("")
allow(ENV).to receive(:[]).with("TRAVIS_BRANCH").and_return("foo")
end

it "returns the value of the environment variable" do
expect(subject).to eq "foo"
end
end

context "when there is no known environment variable for the branch" do
it "attempts to execute a git command to determine the value" do
expect { subject }.to_not raise_error
end
end

context "when the git branch name comes back as HEAD" do
before do
allow(Git).to receive(:execute_git_command).and_return("HEAD")
end

it "raises an error" do
expect { subject }.to raise_error PactBroker::Client::Error, /returned 'HEAD'/
end
end

context "when the git branch name comes back as an empty string" do
before do
allow(Git).to receive(:execute_git_command).and_return("")
end

it "raises an error" do
expect { subject }.to raise_error PactBroker::Client::Error, /an empty string/
end
end

context "when there is an error executing the git command" do
before do
allow(Git).to receive(:execute_git_command).and_raise("some error")
end

it "raises an error" do
expect { subject }.to raise_error PactBroker::Client::Error, /some error/
end
end
end
end
end
end

0 comments on commit d512ef0

Please sign in to comment.