diff --git a/lib/pact_broker/client/cli/broker.rb b/lib/pact_broker/client/cli/broker.rb index d91ba7d1..b5143e05 100644 --- a/lib/pact_broker/client/cli/broker.rb +++ b/lib/pact_broker/client/cli/broker.rb @@ -1,5 +1,6 @@ -require 'pact_broker/client/can_i_deploy' require 'pact_broker/client/version' +require 'pact_broker/client/can_i_deploy' +require 'pact_broker/client/git' require 'pact_broker/client/cli/version_selector_options_parser' require 'pact_broker/client/cli/custom_thor' require 'pact_broker/client/publish_pacts' @@ -38,6 +39,7 @@ def can_i_deploy(*ignored_but_necessary) method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password" method_option :tag, aliases: "-t", type: :array, banner: "TAG", desc: "Tag name for consumer version. Can be specified multiple times." method_option :verbose, aliases: "-v", desc: "Verbose output", :required => false + method_option :tag_with_git_branch, aliases: "-g", type: :boolean, default: false, required: false, desc: "Tag consumer version with the name of the current git branch. Default: false" def publish(*pact_files) validate_pact_files(pact_files) @@ -90,7 +92,9 @@ def file_list pact_files end def tags - [*options.tag].compact + t = [*options.tag] + t << PactBroker::Client::Git.branch if options.tag_with_git_branch + t.compact.uniq end def pact_broker_client_options diff --git a/lib/pact_broker/client/git.rb b/lib/pact_broker/client/git.rb new file mode 100644 index 00000000..d5875808 --- /dev/null +++ b/lib/pact_broker/client/git.rb @@ -0,0 +1,15 @@ +require 'pact_broker/client/error' + +module PactBroker + module Client + module Git + COMMAND = 'git rev-parse --abbrev-ref HEAD' + + 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}" + end + end + end +end diff --git a/spec/lib/pact_broker/client/cli/broker_publish_spec.rb b/spec/lib/pact_broker/client/cli/broker_publish_spec.rb index 833d314d..01608432 100644 --- a/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +++ b/spec/lib/pact_broker/client/cli/broker_publish_spec.rb @@ -5,6 +5,7 @@ module PactBroker::Client::CLI describe ".broker" do before do allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(true) + allow(PactBroker::Client::Git).to receive(:branch).and_return("bar") subject.options = OpenStruct.new(minimum_valid_options) end @@ -79,6 +80,30 @@ module PactBroker::Client::CLI end end + context "with tag-with-git-branch" do + before do + subject.options = OpenStruct.new( + minimum_valid_options.merge(tag_with_git_branch: true) + ) + end + + it "determines the git branch name" do + expect(PactBroker::Client::Git).to receive(:branch) + invoke_broker + end + + it "adds it to the list of tags when publishing" do + expect(PactBroker::Client::PublishPacts).to receive(:call).with( + anything, + anything, + anything, + ['bar'], + anything + ) + invoke_broker + end + end + context "with basic auth options specified" do before do subject.options = OpenStruct.new(