Skip to content

Commit ed7d23a

Browse files
committed
feat: renamed pact-publish command to pact-broker publish
BREAKING CHANGE Since the pact-publish command was written, more commands have been written that will belong in the `pact-broker` CLI family, so it's better to move it now before anyone is using it. Sorry for the inconvenience.
1 parent 24a5473 commit ed7d23a

File tree

7 files changed

+63
-148
lines changed

7 files changed

+63
-148
lines changed

bin/pact-publish

Lines changed: 0 additions & 4 deletions
This file was deleted.

lib/pact_broker/client/cli/broker.rb

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
require 'pact_broker/client/can_i_deploy'
22
require 'pact_broker/client/version'
33
require 'pact_broker/client/cli/version_selector_options_parser'
4-
require 'thor'
4+
require 'pact_broker/client/cli/custom_thor'
5+
require 'pact_broker/client/publish_pacts'
6+
require 'rake/file_list'
57

68
module PactBroker
79
module Client
810
module CLI
9-
class Broker < Thor
11+
# Thor::Error will have its backtrace hidden
12+
class PactPublicationError < ::Thor::Error; end
13+
14+
class Broker < CustomThor
1015
desc 'can-i-deploy', "Returns exit code 0 or 1, indicating whether or not the specified application versions are compatible."
1116

1217
method_option :name, required: true, aliases: "-n", desc: "The application name. Use once for each pacticipant being checked."
@@ -24,12 +29,63 @@ def can_i_deploy
2429
exit(1) unless result.success
2530
end
2631

32+
desc 'publish PACT_DIRS_OR_FILES ...', "Publish pacts to a Pact Broker."
33+
method_option :consumer_app_version, required: true, aliases: "-a", desc: "The consumer application version"
34+
method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
35+
method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
36+
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
37+
method_option :tag, aliases: "-t", type: :array, banner: "TAG", desc: "Tag name for consumer version. Can be specified multiple times."
38+
method_option :verbose, aliases: "-v", desc: "Verbose output", :required => false
39+
40+
def publish(*pact_files)
41+
validate_pact_files(pact_files)
42+
success = publish_pacts(pact_files)
43+
raise PactPublicationError, "One or more pacts failed to be published" unless success
44+
rescue PactBroker::Client::Error => e
45+
raise PactPublicationError, "#{e.class} - #{e.message}"
46+
end
47+
2748
desc 'version', "Show the pact_broker-client gem version"
2849
def version
2950
$stdout.puts PactBroker::Client::VERSION
3051
end
3152

3253
no_commands do
54+
55+
def self.exit_on_failure?
56+
true
57+
end
58+
59+
def validate_pact_files pact_files
60+
unless pact_files && pact_files.any?
61+
raise RequiredArgumentMissingError, "No value provided for required pact_files"
62+
end
63+
end
64+
65+
def publish_pacts pact_files
66+
PactBroker::Client::PublishPacts.call(
67+
options.broker_base_url,
68+
file_list(pact_files),
69+
options.consumer_app_version,
70+
tags,
71+
pact_broker_client_options
72+
)
73+
end
74+
75+
def file_list pact_files
76+
Rake::FileList[pact_files].collect do | path |
77+
if File.directory?(path)
78+
Rake::FileList[File.join(path, "*.json")]
79+
else
80+
path
81+
end
82+
end.flatten
83+
end
84+
85+
def tags
86+
[*options.tag].compact
87+
end
88+
3389
def pact_broker_client_options
3490
if options.broker_username
3591
{

lib/pact_broker/client/cli/custom_thor.rb

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ module CLI
66
##
77
# Custom Thor task allows the following:
88
#
9-
# `script arg1 arg2` to be interpreted as `script <default_task> arg1 arg2`
109
# `--option 1 --option 2` to be interpreted as `--option 1 2` (the standard Thor format for multiple value options)
11-
# `script --help` to display the help for the default task instead of the command list
1210
#
1311
class CustomThor < ::Thor
1412

@@ -17,24 +15,8 @@ def self.start given_args = ARGV, config = {}
1715
super(massage_args(given_args))
1816
end
1917

20-
def help *args
21-
if args.empty?
22-
super(self.class.default_task)
23-
else
24-
super
25-
end
26-
end
27-
2818
def self.massage_args argv
29-
prepend_default_task_name(turn_muliple_tag_options_into_array(argv))
30-
end
31-
32-
def self.prepend_default_task_name argv
33-
if known_first_arguments.include?(argv[0])
34-
argv
35-
else
36-
[default_command] + argv
37-
end
19+
turn_muliple_tag_options_into_array(argv)
3820
end
3921

4022
# other task names, help, and the help shortcuts

lib/pact_broker/client/cli/publish.rb

Lines changed: 0 additions & 88 deletions
This file was deleted.

spec/lib/pact_broker/client/cli/publish_spec.rb renamed to spec/lib/pact_broker/client/cli/broker_publish_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
require 'pact_broker/client/cli/publish'
1+
require 'pact_broker/client/cli/broker'
22

33
module PactBroker::Client::CLI
4-
describe Publish do
4+
describe Broker do
55
describe ".broker" do
66
before do
77
allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(true)
@@ -17,7 +17,7 @@ module PactBroker::Client::CLI
1717
}
1818
end
1919

20-
let(:invoke_broker) { subject.broker(*file_list) }
20+
let(:invoke_broker) { subject.publish(*file_list) }
2121

2222
context "with minimum valid options" do
2323
it "invokes the PublishPacts command" do
@@ -26,7 +26,7 @@ module PactBroker::Client::CLI
2626
["spec/support/cli_test_pacts/foo.json"],
2727
"1.2.3",
2828
[],
29-
{}
29+
{verbose: nil}
3030
)
3131
invoke_broker
3232
end

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,11 @@ def test_multiple_options
2424
describe CustomThor do
2525
subject { TestThor.new }
2626

27-
it "invokes the default task when aguments are given without specifying a task" do
28-
expect(Delegate).to receive(:call).with(argument: 'foo')
29-
TestThor.start(%w{foo})
30-
end
31-
3227
it "converts options that are specified multiple times into a single array" do
3328
expect(Delegate).to receive(:call).with({'multi' => ['one', 'two']})
3429
TestThor.start(%w{test_multiple_options --multi one --multi two})
3530
end
3631

37-
describe ".prepend_default_task_name" do
38-
let(:argv_with) { [TestThor.default_command, 'foo'] }
39-
40-
context "when the default task name is given" do
41-
it "does not prepend the default task name" do
42-
expect(TestThor.prepend_default_task_name(argv_with)).to eq(argv_with)
43-
end
44-
end
45-
46-
context "when the first argument is --help" do
47-
let(:argv) { ['--help', 'foo'] }
48-
49-
it "does not prepend the default task name" do
50-
expect(TestThor.prepend_default_task_name(argv)).to eq(argv)
51-
end
52-
end
53-
54-
context "when the default task name is not given" do
55-
let(:argv) { ['foo'] }
56-
57-
it "prepends the default task name" do
58-
expect(TestThor.prepend_default_task_name(argv)).to eq(argv_with)
59-
end
60-
end
61-
end
62-
6332
describe ".turn_muliple_tag_options_into_array" do
6433
it "turns '--tag foo --tag bar' into '--tag foo bar'" do
6534
input = %w{--ignore this --tag foo --tag bar --wiffle --that}

0 commit comments

Comments
 (0)