Skip to content

Commit 6da6e02

Browse files
committed
feat(matrix): update validation to allow latest tag to be specified
1 parent fe498a7 commit 6da6e02

File tree

6 files changed

+67
-13
lines changed

6 files changed

+67
-13
lines changed

lib/pact_broker/matrix/repository.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def find_all selectors
6060
def look_up_versions_for_tags(selectors)
6161
selectors.collect do | selector |
6262
if selector[:latest_tag]
63-
version = version_repository.find_by_latest_tag(selector[:pacticipant_name], selector[:latest_tag])
63+
version = version_repository.find_by_pacticpant_name_and_latest_tag(selector[:pacticipant_name], selector[:latest_tag])
6464
# validation in resource should ensure we always have a version
6565
{
6666
pacticipant_name: selector[:pacticipant_name],

lib/pact_broker/matrix/service.rb

+15-8
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,32 @@ def find_compatible_pacticipant_versions criteria
2323
def validate_selectors selectors
2424
error_messages = []
2525

26-
selectors.each do | selector |
27-
if selector[:pacticipant_name].nil? && selector[:pacticipant_version_number].nil?
26+
selectors.each do | s |
27+
if s[:pacticipant_name].nil? && s[:pacticipant_version_number].nil?
2828
error_messages << "Please specify the pacticipant name and version"
29-
elsif selector[:pacticipant_name].nil?
29+
elsif s[:pacticipant_name].nil?
3030
error_messages << "Please specify the pacticipant name"
31+
else
32+
if s.key?(:pacticipant_version_number) && s.key?(:latest_tag)
33+
error_messages << "A version and a latest tag cannot both be specified for #{s[:pacticipant_name]}"
34+
end
3135
end
3236
end
3337

3438
selectors.collect{ |selector| selector[:pacticipant_name] }.compact.each do | pacticipant_name |
3539
unless pacticipant_service.find_pacticipant_by_name(pacticipant_name)
36-
error_messages << "Pacticipant '#{pacticipant_name}' not found"
40+
error_messages << "Pacticipant #{pacticipant_name} not found"
3741
end
3842
end
3943

4044
if error_messages.empty?
41-
selectors.each do | selector |
42-
if selector[:pacticipant_version_number]
43-
version = version_service.find_by_pacticipant_name_and_number(pacticipant_name: selector[:pacticipant_name], pacticipant_version_number: selector[:pacticipant_version_number])
44-
error_messages << "No pact or verification found for #{selector[:pacticipant_name]} version #{selector[:pacticipant_version_number]}" if version.nil?
45+
selectors.each do | s |
46+
if s[:pacticipant_version_number]
47+
version = version_service.find_by_pacticipant_name_and_number(pacticipant_name: s[:pacticipant_name], pacticipant_version_number: s[:pacticipant_version_number])
48+
error_messages << "No pact or verification found for #{s[:pacticipant_name]} version #{s[:pacticipant_version_number]}" if version.nil?
49+
elsif s[:latest_tag]
50+
version = version_service.find_by_pacticpant_name_and_latest_tag(s[:pacticipant_name], s[:latest_tag])
51+
error_messages << "No version of #{s[:pacticipant_name]} found with tag #{s[:latest_tag]}" if version.nil?
4552
end
4653
end
4754
end

lib/pact_broker/versions/repository.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def find_by_pacticipant_id_and_number pacticipant_id, number
1212
PactBroker::Domain::Version.where(number: number, pacticipant_id: pacticipant_id).single_record
1313
end
1414

15-
def find_by_latest_tag pacticipant_name, tag
15+
def find_by_pacticpant_name_and_latest_tag pacticipant_name, tag
1616
PactBroker::Domain::Version
1717
.select_all_qualified
1818
.join(:pacticipants, {id: :pacticipant_id}, {implicit_qualifier: :versions})

lib/pact_broker/versions/service.rb

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def self.find_by_pacticipant_name_and_number params
1111
version_repository.find_by_pacticipant_name_and_number params.fetch(:pacticipant_name), params.fetch(:pacticipant_version_number)
1212
end
1313

14+
def self.find_by_pacticpant_name_and_latest_tag(pacticipant_name, tag)
15+
version_repository.find_by_pacticpant_name_and_latest_tag(pacticipant_name, tag)
16+
end
17+
1418
def self.delete version
1519
tag_repository.delete_by_version_id version.id
1620
pact_repository.delete_by_version_id version.id

spec/lib/pact_broker/matrix/service_spec.rb

+44-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module Matrix
3636
let(:selectors) { [{ pacticipant_name: "Foo", pacticipant_version_number: "1" }] }
3737

3838
it "returns error messages" do
39-
expect(subject.first).to eq "Pacticipant 'Foo' not found"
39+
expect(subject.first).to eq "Pacticipant Foo not found"
4040
end
4141
end
4242

@@ -70,6 +70,49 @@ module Matrix
7070
expect(subject.first).to eq "Please specify the pacticipant name and version"
7171
end
7272
end
73+
74+
context "when the latest_tag is used instead of a version" do
75+
before do
76+
td.create_pacticipant("Foo")
77+
.create_version("1")
78+
.create_tag("prod")
79+
.create_pacticipant("Bar")
80+
.create_version("2")
81+
end
82+
83+
let(:selectors) { [{ pacticipant_name: "Foo", latest_tag: "prod" }, { pacticipant_name: "Bar", pacticipant_version_number: "2" }] }
84+
85+
context "when there is a version for the tag" do
86+
it "returns no error messages" do
87+
expect(subject).to eq []
88+
end
89+
end
90+
91+
context "when there is not a version for the tag" do
92+
93+
let(:selectors) { [{ pacticipant_name: "Foo", latest_tag: "wiffle" }, { pacticipant_name: "Bar", pacticipant_version_number: "2" }] }
94+
95+
it "returns an error message" do
96+
expect(subject).to eq ["No version of Foo found with tag wiffle"]
97+
end
98+
end
99+
end
100+
101+
context "when the latest_tag is used as well as a version" do
102+
before do
103+
td.create_pacticipant("Foo")
104+
.create_version("1")
105+
.create_tag("prod")
106+
.create_pacticipant("Bar")
107+
.create_version("2")
108+
end
109+
110+
let(:selectors) { [{ pacticipant_name: "Foo", pacticipant_version_number: "1", latest_tag: "prod" }, { pacticipant_name: "Bar", pacticipant_version_number: "2" }] }
111+
112+
it "returns an error message" do
113+
expect(subject).to eq ["A version and a latest tag cannot both be specified for Foo"]
114+
end
115+
end
73116
end
74117
end
75118
end

spec/lib/pact_broker/versions/repository_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Versions
1010
let(:version_number) { "1.2.3" }
1111

1212

13-
describe "#find_by_latest_tag" do
13+
describe "#find_by_pacticpant_name_and_latest_tag" do
1414
before do
1515
td.create_consumer("Bar")
1616
.create_consumer_version("2.3.4")
@@ -23,7 +23,7 @@ module Versions
2323
.create_consumer_version("5.6.7")
2424
end
2525

26-
subject { Repository.new.find_by_latest_tag("Foo", "prod") }
26+
subject { Repository.new.find_by_pacticpant_name_and_latest_tag("Foo", "prod") }
2727

2828
it "returns the most recent version that has the specified tag" do
2929

0 commit comments

Comments
 (0)