Skip to content

Commit

Permalink
feat(cli): add option to --tag-with-git-branch when publishing pacts
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 30, 2017
1 parent 0f3bfea commit 484d5b8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions lib/pact_broker/client/git.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions spec/lib/pact_broker/client/cli/broker_publish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 484d5b8

Please sign in to comment.