Skip to content

Commit

Permalink
feat: support publishing pacts for multiple consumers at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Aug 5, 2020
1 parent 02fc242 commit 573e97c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/pact_broker/client/publish_pacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def pact_files
@pact_files ||= pact_file_paths.collect{ |pact_file_path| PactFile.new(pact_file_path) }
end

def consumer_names
pact_files.collect(&:consumer_name).uniq
end

def publish_pact pact
begin
$stdout.puts "Publishing #{pact.pact_name} to pact broker at #{pact_broker_base_url}"
Expand All @@ -72,12 +76,14 @@ def apply_tags
def tag_consumer_version tag
versions = pact_broker_client.pacticipants.versions
Retry.while_error do
$stdout.puts "Tagging version #{consumer_version} of #{consumer_name} as #{tag.inspect}"
versions.tag(pacticipant: consumer_name, version: consumer_version, tag: tag)
true
consumer_names.collect do | consumer_name |
$stdout.puts "Tagging version #{consumer_version} of #{consumer_name} as #{tag.inspect}"
versions.tag(pacticipant: consumer_name, version: consumer_version, tag: tag)
true
end
end
rescue => e
$stderr.puts "Failed to tag version #{consumer_version} of #{consumer_name} due to error: #{e.class} - #{e}}"
$stderr.puts "Failed to tag versions due to error: #{e.class} - #{e}"
false
end

Expand All @@ -94,10 +100,6 @@ def publish_pact_contents(pact)
end
end

def consumer_name
pact_files.first.consumer_name
end

def validate
raise PactBroker::Client::Error.new("Please specify the consumer_version") unless (consumer_version && consumer_version.to_s.strip.size > 0)
raise PactBroker::Client::Error.new("Please specify the pact_broker_base_url") unless (pact_broker_base_url && pact_broker_base_url.to_s.strip.size > 0)
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/pact_broker/client/publish_pacts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ module Client
end
end

context "when publishing multiple files with different consumers" do
let(:pact_file_paths) { ['spec/pacts/consumer-provider.json','spec/pacts/foo-bar.json']}
let(:tags) { ['dev'] }

it "tags each consumer" do
expect(pact_versions_client).to receive(:tag).with(
pacticipant: "Consumer",
version: consumer_version,
tag: "dev"
)
expect(pact_versions_client).to receive(:tag).with(
pacticipant: "Foo",
version: consumer_version,
tag: "dev"
)
subject.call
end
end

context "when publishing one or more pacts fails" do
let(:pact_file_paths) { ['spec/pacts/consumer-provider.json','spec/pacts/foo-bar.json']}

Expand Down

0 comments on commit 573e97c

Please sign in to comment.