diff --git a/lib/pact_broker/client/git.rb b/lib/pact_broker/client/git.rb index f14a4475..e91312cf 100644 --- a/lib/pact_broker/client/git.rb +++ b/lib/pact_broker/client/git.rb @@ -25,9 +25,8 @@ module Git using PactBroker::Client::HashRefinements 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} - + BRANCH_ENV_VAR_NAMES = %w{GITHUB_REF 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{GITHUB_SHA 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 } def self.commit @@ -38,6 +37,10 @@ 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 + def self.build_url + github_build_url || BUILD_URL_ENV_VAR_NAMES.collect{ | name | value_from_env_var(name) }.compact.first + end + # private def self.find_commit_from_env_vars @@ -45,7 +48,8 @@ def self.find_commit_from_env_vars end 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 + val = BRANCH_ENV_VAR_NAMES.collect { |env_var_name| value_from_env_var(env_var_name) }.compact.first + val.gsub(%r{^refs/heads/}, "") if val end def self.find_branch_from_env_var_ending_with_branch @@ -104,6 +108,13 @@ def self.execute_and_parse_command(raise_error) return [] end end + + def self.github_build_url + parts = %w{GITHUB_SERVER_URL GITHUB_REPOSITORY GITHUB_RUN_ID}.collect{ | name | value_from_env_var(name) } + if parts.all? + [parts[0], parts[1], "actions", "runs", parts[2]].join("/") + end + end end end end diff --git a/spec/lib/pact_broker/client/git_spec.rb b/spec/lib/pact_broker/client/git_spec.rb index 94354cb0..cea57a80 100644 --- a/spec/lib/pact_broker/client/git_spec.rb +++ b/spec/lib/pact_broker/client/git_spec.rb @@ -35,6 +35,16 @@ module Git end end + context "when the branch starts with refs/heads/" do + before do + allow(ENV).to receive(:[]).with("GITHUB_REF").and_return("refs/heads/feature-x") + end + + it "trims off the refs/heads/" do + expect(subject).to eq "feature-x" + end + end + context "when there is one environment variable ending with _BRANCH" do before do allow(ENV).to receive(:keys).and_return(%w{FOO_BRANCH BAR_BRANCH BLAH}) @@ -108,6 +118,40 @@ module Git include_examples "when raise_error is false" end end + + describe ".build_url" do + before do + allow(ENV).to receive(:[]).and_call_original + end + + subject { Git.build_url } + + context "when nothing is set" do + before do + allow(ENV).to receive(:[]).and_return(nil) + end + + it { is_expected.to eq nil } + end + + context "when BUILDKITE_BUILD_URL is set" do + before do + allow(ENV).to receive(:[]).with("BUILDKITE_BUILD_URL").and_return("http://build") + end + + it { is_expected.to eq "http://build" } + end + + context "when the Github Actions env vars are set" do + before do + allow(ENV).to receive(:[]).with("GITHUB_SERVER_URL").and_return("https://github.com") + allow(ENV).to receive(:[]).with("GITHUB_REPOSITORY").and_return("org/repo") + allow(ENV).to receive(:[]).with("GITHUB_RUN_ID").and_return("1") + end + + it { is_expected.to eq "https://github.com/org/repo/actions/runs/1" } + end + end end end end