Skip to content

Commit 484d5b8

Browse files
committed
feat(cli): add option to --tag-with-git-branch when publishing pacts
1 parent 0f3bfea commit 484d5b8

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

lib/pact_broker/client/cli/broker.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
require 'pact_broker/client/can_i_deploy'
21
require 'pact_broker/client/version'
2+
require 'pact_broker/client/can_i_deploy'
3+
require 'pact_broker/client/git'
34
require 'pact_broker/client/cli/version_selector_options_parser'
45
require 'pact_broker/client/cli/custom_thor'
56
require 'pact_broker/client/publish_pacts'
@@ -38,6 +39,7 @@ def can_i_deploy(*ignored_but_necessary)
3839
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
3940
method_option :tag, aliases: "-t", type: :array, banner: "TAG", desc: "Tag name for consumer version. Can be specified multiple times."
4041
method_option :verbose, aliases: "-v", desc: "Verbose output", :required => false
42+
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"
4143

4244
def publish(*pact_files)
4345
validate_pact_files(pact_files)
@@ -90,7 +92,9 @@ def file_list pact_files
9092
end
9193

9294
def tags
93-
[*options.tag].compact
95+
t = [*options.tag]
96+
t << PactBroker::Client::Git.branch if options.tag_with_git_branch
97+
t.compact.uniq
9498
end
9599

96100
def pact_broker_client_options

lib/pact_broker/client/git.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'pact_broker/client/error'
2+
3+
module PactBroker
4+
module Client
5+
module Git
6+
COMMAND = 'git rev-parse --abbrev-ref HEAD'
7+
8+
def self.branch
9+
`#{COMMAND}`.strip
10+
rescue StandardError => e
11+
raise PactBroker::Client::Error, "Could not determine current git branch using command `#{COMMAND}`. #{e.class} #{e.message}"
12+
end
13+
end
14+
end
15+
end

spec/lib/pact_broker/client/cli/broker_publish_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module PactBroker::Client::CLI
55
describe ".broker" do
66
before do
77
allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(true)
8+
allow(PactBroker::Client::Git).to receive(:branch).and_return("bar")
89
subject.options = OpenStruct.new(minimum_valid_options)
910
end
1011

@@ -79,6 +80,30 @@ module PactBroker::Client::CLI
7980
end
8081
end
8182

83+
context "with tag-with-git-branch" do
84+
before do
85+
subject.options = OpenStruct.new(
86+
minimum_valid_options.merge(tag_with_git_branch: true)
87+
)
88+
end
89+
90+
it "determines the git branch name" do
91+
expect(PactBroker::Client::Git).to receive(:branch)
92+
invoke_broker
93+
end
94+
95+
it "adds it to the list of tags when publishing" do
96+
expect(PactBroker::Client::PublishPacts).to receive(:call).with(
97+
anything,
98+
anything,
99+
anything,
100+
['bar'],
101+
anything
102+
)
103+
invoke_broker
104+
end
105+
end
106+
82107
context "with basic auth options specified" do
83108
before do
84109
subject.options = OpenStruct.new(

0 commit comments

Comments
 (0)