Skip to content

Commit 3eaa79c

Browse files
committed
feat: add pb:diff relation to pact resource to view a diff with an arbitrary pact version
Closes: pact-foundation#190
1 parent 6b04d70 commit 3eaa79c

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

lib/pact_broker/api/decorators/pact_decorator.rb

+9
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ def to_hash(options = {})
9999
}
100100
end
101101

102+
link :'pb:diff' do | options |
103+
{
104+
title: "Diff with another specified version of this pact",
105+
href: templated_diff_url(represented, options.fetch(:base_url)),
106+
templated: true
107+
108+
}
109+
end
110+
102111
link :'pb:pact-webhooks' do | options |
103112
{
104113
title: "Webhooks for the pact between #{represented.consumer.name} and #{represented.provider.name}",

lib/pact_broker/api/pact_broker_urls.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def pact_url base_url, pact
5151
end
5252

5353
def pact_version_url pact, base_url
54-
"#{pactigration_base_url(base_url, path)}/pact-version/#{pact.sha}"
54+
"#{pactigration_base_url(base_url, pact)}/pact-version/#{pact.pact_version_sha}"
5555
end
5656

5757
def pact_url_from_params base_url, params
@@ -81,6 +81,10 @@ def previous_distinct_diff_url pact, base_url
8181
pact_url(base_url, pact) + "/diff/previous-distinct"
8282
end
8383

84+
def templated_diff_url pact, base_url = ''
85+
pact_version_url(pact, base_url) + "/diff/pact-version/{pactVersion}"
86+
end
87+
8488
def previous_distinct_pact_version_url pact, base_url
8589
pact_url(base_url, pact) + "/previous-distinct"
8690
end

spec/lib/pact_broker/api/decorators/pact_decorator_spec.rb

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
require 'pact_broker/api/decorators/pact_decorator'
33

44
module PactBroker
5-
65
module Api
7-
86
module Decorators
9-
107
describe PactDecorator do
118

9+
before do
10+
allow_any_instance_of(PactDecorator).to receive(:templated_diff_url).and_return('templated-diff-url')
11+
end
1212
let(:content_hash) {
1313
{
1414
'consumer' => {'name' => 'Consumer'},
@@ -105,6 +105,11 @@ module Decorators
105105
expect(subject[:_links][:'pb:publish-verification-results'][:href]).to match %r{http://example.org/.*/verification-results}
106106
end
107107

108+
it "includes a link to diff this pact version with another pact version" do
109+
expect(subject[:_links][:'pb:diff'][:href]).to eq 'templated-diff-url'
110+
expect(subject[:_links][:'pb:diff'][:templated]).to eq true
111+
end
112+
108113
it "includes a curie" do
109114
expect(subject[:_links][:curies]).to eq [{ name: "pb", href: "http://example.org/doc/{rel}", templated: true }]
110115
end
@@ -116,7 +121,6 @@ module Decorators
116121
end
117122
end
118123
end
119-
120124
end
121125
end
122126
end

spec/lib/pact_broker/api/pact_broker_urls_spec.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ module PactBroker
44
module Api
55
describe PactBrokerUrls do
66

7+
# Regex find all the URL parameter names
8+
# \/\{[^\}\s\[\(\.]+\}
9+
710
let(:base_url) { "http://example.org" }
8-
let(:pact) { double('pact', consumer: consumer, provider: provider, consumer_version_number: "123") }
11+
let(:pact) { double('pact', consumer: consumer, provider: provider, consumer_version_number: "123", pact_version_sha: "5hbfu") }
912
let(:consumer) { double('pacticipant', name: "Foo") }
1013
let(:provider) { double('pacticipant', name: "Bar") }
1114
let(:verification) do
@@ -33,6 +36,12 @@ module Api
3336

3437
it { is_expected.to eq "http://example.org/pacts/provider/Bar/consumer/Foo/pact-version/1234/verification-results/1/triggered-webhooks" }
3538
end
39+
40+
describe "templated_diff_url" do
41+
subject { PactBrokerUrls.templated_diff_url(pact, base_url) }
42+
43+
it { is_expected.to eq "http://example.org/pacts/provider/Bar/consumer/Foo/pact-version/5hbfu/diff/pact-version/{pactVersion}" }
44+
end
3645
end
3746
end
3847
end

0 commit comments

Comments
 (0)