Skip to content

Commit

Permalink
feat: support Github Actions environment variables for --tag-with-git…
Browse files Browse the repository at this point in the history
…-branch
  • Loading branch information
bethesque committed Oct 3, 2021
1 parent 580896c commit 2b8dac5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/pact_broker/client/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,14 +37,19 @@ 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
COMMIT_ENV_VAR_NAMES.collect { |env_var_name| value_from_env_var(env_var_name) }.compact.first
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
Expand Down Expand Up @@ -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
44 changes: 44 additions & 0 deletions spec/lib/pact_broker/client/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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

0 comments on commit 2b8dac5

Please sign in to comment.