Skip to content

Commit 8e7d0e4

Browse files
author
Antonello Caboni
committed
feat: make contract_content_changed tag aware
1 parent 0c70bef commit 8e7d0e4

File tree

4 files changed

+145
-35
lines changed

4 files changed

+145
-35
lines changed

lib/pact_broker/pacts/repository.rb

+14-7
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,20 @@ def find_all_revisions consumer_name, consumer_version, provider_name
187187
.order(:consumer_version_order, :revision_number).collect(&:to_domain_with_content)
188188
end
189189

190-
def find_previous_pact pact
191-
LatestPactPublicationsByConsumerVersion
192-
.eager(:tags)
193-
.consumer(pact.consumer.name)
194-
.provider(pact.provider.name)
195-
.consumer_version_order_before(pact.consumer_version.order)
196-
.latest.collect(&:to_domain_with_content)[0]
190+
def find_previous_pact pact, tag = nil
191+
query = LatestPactPublicationsByConsumerVersion
192+
.eager(:tags)
193+
.consumer(pact.consumer.name)
194+
.provider(pact.provider.name)
195+
196+
if tag == :untagged
197+
query = query.untagged
198+
elsif tag
199+
query = query.tag(tag)
200+
end
201+
202+
query.consumer_version_order_before(pact.consumer_version.order)
203+
.latest.collect(&:to_domain_with_content)[0]
197204
end
198205

199206
def find_next_pact pact

lib/pact_broker/pacts/service.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def find_distinct_pacts_between consumer, options
104104

105105
# TODO also take into account overridden revisions
106106
def pact_is_new_or_pact_has_changed_since_previous_version? pact
107-
previous_pact = pact_repository.find_previous_pact pact
108-
previous_pact.nil? || pact.json_content != previous_pact.json_content
107+
previous_pacts = find_previous_pacts pact
108+
previous_pacts.any? { |previous_pact| previous_pact.nil? || pact.json_content != previous_pact.json_content}
109109
end
110110

111111
private
@@ -140,6 +140,14 @@ def trigger_webhooks pact
140140
logger.debug "Pact has not changed since previous version, not triggering webhooks"
141141
end
142142
end
143+
144+
def find_previous_pacts pact
145+
if pact.consumer_version_tag_names.any?
146+
pact.consumer_version_tag_names.map { |tag| pact_repository.find_previous_pact(pact, tag) }
147+
else
148+
[pact_repository.find_previous_pact(pact, :untagged)]
149+
end
150+
end
143151
end
144152
end
145153
end

spec/lib/pact_broker/pacts/repository_spec.rb

+56-9
Original file line numberDiff line numberDiff line change
@@ -513,27 +513,74 @@ module Pacts
513513
.create_consumer("Consumer")
514514
.create_consumer_version("1.2.2")
515515
.create_provider("Provider")
516+
.create_consumer_version_tag("a_tag")
517+
.create_pact
518+
.create_consumer_version("1.2.3")
516519
.create_pact
517520
.create_consumer_version("1.2.4")
521+
.create_consumer_version_tag("another_tag")
518522
.create_pact
519-
.create_consumer_version("1.2.6")
523+
.create_consumer_version("1.2.5")
524+
.create_consumer_version_tag("a_tag")
525+
.create_pact
526+
.create_consumer_version("1.2.7")
527+
.create_consumer_version_tag("another_tag")
520528
.create_pact
521529
.create_provider("Another Provider")
522-
.create_consumer_version("1.2.5")
530+
.create_consumer_version("1.2.6")
531+
.create_consumer_version_tag("a_tag")
523532
.create_pact
524533
end
525534

526-
let(:pact) { Repository.new.find_latest_pact "Consumer", "Provider" }
535+
context "regardless of tag" do
536+
context "when a previous version with a pact exists" do
527537

528-
subject { Repository.new.find_previous_pact pact }
538+
let(:pact) { Repository.new.find_latest_pact "Consumer", "Provider", "another_tag" }
529539

530-
it "finds the previous pact" do
531-
expect(subject.consumer_version_number).to eq "1.2.4"
532-
expect(subject.consumer_version.number).to eq "1.2.4"
540+
subject { Repository.new.find_previous_pact pact }
541+
542+
it "finds the previous pact" do
543+
expect(subject.consumer_version_number).to eq "1.2.5"
544+
end
545+
546+
it "sets the json_content" do
547+
expect(subject.json_content).to_not be nil
548+
end
549+
end
533550
end
534551

535-
it "sets the json_content" do
536-
expect(subject.json_content).to_not be nil
552+
context "by tag" do
553+
context "when a previous version with a pact with a specific tag exists" do
554+
555+
let(:pact) { Repository.new.find_latest_pact "Consumer", "Provider", "a_tag" }
556+
557+
subject { Repository.new.find_previous_pact pact, "a_tag" }
558+
559+
it "finds the previous pact" do
560+
expect(subject.consumer_version_number).to eq "1.2.2"
561+
end
562+
563+
it "sets the json_content" do
564+
expect(subject.json_content).to_not be nil
565+
end
566+
end
567+
end
568+
569+
context "that is untagged" do
570+
context "when a previous version with a an untagged pact exists" do
571+
572+
let(:pact) { Repository.new.find_latest_pact "Consumer", "Provider" }
573+
574+
subject { Repository.new.find_previous_pact pact, :untagged }
575+
576+
it "finds the previous pact" do
577+
expect(subject.consumer_version_number).to eq "1.2.3"
578+
end
579+
580+
it "sets the json_content" do
581+
expect(subject.json_content).to_not be nil
582+
end
583+
end
537584
end
538585
end
539586

spec/lib/pact_broker/pacts/service_spec.rb

+65-17
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,83 @@ module Pacts
3434
let(:json_content) { { 'some' => 'json'}.to_json }
3535
let(:pact) { instance_double(PactBroker::Domain::Pact, json_content: json_content)}
3636

37-
before do
38-
allow_any_instance_of(Pacts::Repository).to receive(:find_previous_pact).and_return(previous_pact)
39-
end
40-
4137
subject { Service.pact_is_new_or_pact_has_changed_since_previous_version? pact }
4238

43-
context "when a previous pact is found" do
44-
let(:previous_pact) { instance_double(PactBroker::Domain::Pact, json_content: previous_json_content)}
45-
let(:previous_json_content) { {'some' => 'json'}.to_json }
39+
context "when consumer version is untagged" do
40+
before do
41+
allow(pact).to receive(:consumer_version_tag_names).and_return([]);
42+
allow_any_instance_of(Pacts::Repository).to receive(:find_previous_pact).with(pact, :untagged).and_return(previous_pact)
43+
end
44+
45+
context "when a previous pact is found" do
46+
let(:previous_pact) { instance_double(PactBroker::Domain::Pact, json_content: previous_json_content)}
47+
let(:previous_json_content) { {'some' => 'json'}.to_json }
4648

47-
context "when the json_content is the same" do
48-
it "returns false" do
49-
expect(subject).to be_falsey
49+
context "when the json_content is the same" do
50+
it "returns false" do
51+
expect(subject).to be_falsey
52+
end
53+
end
54+
55+
context "when the json_content is not the same" do
56+
let(:previous_json_content) { {'some-other' => 'json'}.to_json }
57+
it "returns truthy" do
58+
expect(subject).to be_truthy
59+
end
5060
end
5161
end
5262

53-
context "when the json_content is not the same" do
54-
let(:previous_json_content) { {'some-other' => 'json'}.to_json }
55-
it "returns truthy" do
63+
context "when a previous pact is not found" do
64+
let(:previous_pact) { nil }
65+
66+
it "returns true" do
5667
expect(subject).to be_truthy
5768
end
5869
end
5970
end
6071

61-
context "when a previous pact is not found" do
62-
let(:previous_pact) { nil }
72+
context "when consumer version has two tags" do
73+
before do
74+
allow(pact).to receive(:consumer_version_tag_names).and_return(['tag_1', 'tag_2']);
75+
allow_any_instance_of(Pacts::Repository).to receive(:find_previous_pact).with(pact, 'tag_1').and_return(previous_pact_tag_1)
76+
allow_any_instance_of(Pacts::Repository).to receive(:find_previous_pact).with(pact, 'tag_2').and_return(previous_pact_tag_2)
77+
end
78+
79+
context "when a previous pact is found for both tags" do
80+
let(:previous_pact_tag_1) { instance_double(PactBroker::Domain::Pact, json_content: previous_json_content_tag_1)}
81+
let(:previous_json_content_tag_1) { {'some' => 'json'}.to_json }
82+
83+
let(:previous_pact_tag_2) { instance_double(PactBroker::Domain::Pact, json_content: previous_json_content_tag_2)}
84+
let(:previous_json_content_tag_2) { {'some' => 'json'}.to_json }
6385

64-
it "returns true" do
65-
expect(subject).to be_truthy
86+
context "when the json_content of both previous pacts and new pact is the same" do
87+
it "returns false" do
88+
expect(subject).to be_falsey
89+
end
90+
end
91+
92+
context "when the json_content of first previous pact is not the same" do
93+
let(:previous_json_content_tag_1) { {'some-other' => 'json'}.to_json }
94+
it "returns truthy" do
95+
expect(subject).to be_truthy
96+
end
97+
end
98+
99+
context "when the json_content of second previous pact not the same" do
100+
let(:previous_json_content_tag_2) { {'some-other' => 'json'}.to_json }
101+
it "returns truthy" do
102+
expect(subject).to be_truthy
103+
end
104+
end
105+
end
106+
107+
context "when no previous pacts are found" do
108+
let(:previous_pact_tag_1) { nil }
109+
let(:previous_pact_tag_2) { nil }
110+
111+
it "returns true" do
112+
expect(subject).to be_truthy
113+
end
66114
end
67115
end
68116
end

0 commit comments

Comments
 (0)