Skip to content

Commit 1d2adc6

Browse files
committed
feat: ensure latest pact version is returned when searching by pact version sha
1 parent d3b67c3 commit 1d2adc6

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/pact_broker/pacts/repository.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,20 @@ def find_latest_pact(consumer_name, provider_name, tag = nil)
103103
end
104104

105105
def find_pact consumer_name, consumer_version, provider_name, pact_version_sha = nil
106-
query = pact_version_sha ? AllPactPublications.pact_version_sha(pact_version_sha) : LatestPactPublicationsByConsumerVersion
106+
query = if pact_version_sha
107+
AllPactPublications
108+
.pact_version_sha(pact_version_sha)
109+
.reverse_order(:consumer_version_order)
110+
.limit(1)
111+
else
112+
LatestPactPublicationsByConsumerVersion
113+
end
107114
query = query
108115
.eager(:tags)
109116
.consumer(consumer_name)
110117
.provider(provider_name)
111118
query = query.consumer_version_number(consumer_version) if consumer_version
112-
query.limit(1).collect(&:to_domain_with_content)[0]
119+
query.collect(&:to_domain_with_content)[0]
113120
end
114121

115122
def find_all_revisions consumer_name, consumer_version, provider_name

spec/lib/pact_broker/pacts/repository_spec.rb

+18-1
Original file line numberDiff line numberDiff line change
@@ -347,15 +347,32 @@ module Pacts
347347
context "with a pact_version_sha" do
348348
subject { Repository.new.find_pact "Consumer", nil, "Provider", pact.pact_version_sha }
349349

350-
it "finds the pact with the given version and revision" do
350+
it "finds the pact with the given pact_version_sha" do
351351
expect(subject.pact_version_sha).to eq pact.pact_version_sha
352352
expect(subject.consumer.name).to eq "Consumer"
353353
expect(subject.provider.name).to eq "Provider"
354354
expect(subject.consumer_version_number).to eq "1.2.4"
355355
expect(subject.revision_number).to eq 2
356356

357357
end
358+
context "when there are multiple pact publications for the pact version" do
359+
before do
360+
# Double check the data is set up correctly...
361+
expect(pact_1.pact_version_sha).to eq(pact_2.pact_version_sha)
362+
end
363+
364+
let(:td) { TestDataBuilder.new }
365+
let!(:pact_1) { td.create_pact_with_hierarchy("Foo", "1", "Bar").and_return(:pact) }
366+
let!(:pact_2) { td.create_consumer_version("2").create_pact(json_content: pact_1.json_content).and_return(:pact) }
367+
368+
subject { Repository.new.find_pact "Foo", nil, "Bar", pact_1.pact_version_sha }
369+
370+
it "returns the latest pact, ordered by consumer version order" do
371+
expect(subject.consumer_version_number).to eq "2"
372+
end
373+
end
358374
end
375+
359376
end
360377

361378
describe "find_all_revisions" do

0 commit comments

Comments
 (0)