Skip to content

Commit f02a1ca

Browse files
committed
feat: add resource to get latest verification for a pact
1 parent 6725f3d commit f02a1ca

12 files changed

+116
-4
lines changed

lib/pact_broker/api.rb

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module PactBroker
2626
# Verifications
2727
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'pact-version', :pact_version_sha, 'verification-results'], Api::Resources::Verifications, {resource_name: "verification_results"}
2828
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'pact-version', :pact_version_sha, 'metadata', :metadata, 'verification-results'], Api::Resources::Verifications, {resource_name: "verification_results"}
29+
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'version', :consumer_version_number, 'verification-results', 'latest'], Api::Resources::LatestVerificationForPact, {resource_name: "latest_verification_results_for_pact_publication"}
30+
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'pact-version', :pact_version_sha, 'verification-results', 'latest'], Api::Resources::LatestVerificationForPact, {resource_name: "latest_verification_results_for_pact_version"}
2931
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'pact-version', :pact_version_sha, 'verification-results', :verification_number], Api::Resources::Verification, {resource_name: "verification_result"}
3032
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'pact-version', :pact_version_sha, 'verification-results', :verification_number, 'triggered-webhooks'], Api::Resources::VerificationTriggeredWebhooks, {resource_name: "verification_result_triggered_webhooks"}
3133
add ['verification-results', 'consumer', :consumer_name, 'version', :consumer_version_number,'latest'], Api::Resources::LatestVerificationsForConsumerVersion, {resource_name: "verification_results_for_consumer_version"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'pact_broker/api/decorators/verification_decorator'
2+
3+
module PactBroker
4+
module Api
5+
module Decorators
6+
class ExtendedVerificationDecorator < VerificationDecorator
7+
class TagDecorator < BaseDecorator
8+
property :name
9+
property :latest?, as: :latest
10+
end
11+
12+
collection :provider_version_tags, as: :tags, embedded: true, extend: TagDecorator
13+
end
14+
end
15+
end
16+
end

lib/pact_broker/api/decorators/pact_decorator.rb

+6
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ def to_hash(options = {})
146146
}
147147
end
148148

149+
link :'pb:latest-verification-results' do | options |
150+
{
151+
href: latest_verification_for_pact_url(represented, options.fetch(:base_url))
152+
}
153+
end
154+
149155
link :'pb:triggered-webhooks' do | options |
150156
{
151157
title: "Webhooks triggered by the publication of this pact",

lib/pact_broker/api/decorators/verification_decorator.rb

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class TagDecorator < BaseDecorator
1515
property :execution_date, as: :verificationDate
1616
property :build_url, as: :buildUrl
1717
property :test_results, as: :testResults
18-
collection :provider_version_tags, as: :tags, embedded: true, extend: TagDecorator
1918

2019
link :self do | options |
2120
{

lib/pact_broker/api/pact_broker_urls.rb

+8
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ def latest_verifications_for_consumer_version_url version, base_url
157157
"#{base_url}/verification-results/consumer/#{url_encode(version.pacticipant.name)}/version/#{version.number}/latest"
158158
end
159159

160+
def latest_verification_for_pact_url pact, base_url
161+
verification_url_from_params(
162+
provider_name: pact.provider_name,
163+
consumer_name: pact.consumer_name,
164+
pact_version_sha: pact.pact_version_sha,
165+
verification_number: 'latest')
166+
end
167+
160168
def verification_triggered_webhooks_url verification, base_url = ''
161169
"#{verification_url(verification, base_url)}/triggered-webhooks"
162170
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'pact_broker/api/resources/verification'
2+
3+
module PactBroker
4+
module Api
5+
module Resources
6+
class LatestVerificationForPact < Verification
7+
private
8+
9+
def pact
10+
@pact ||= pact_service.find_pact(pact_params)
11+
end
12+
13+
def verification
14+
@verification ||= pact && verification_service.find_latest_for_pact(pact)
15+
end
16+
end
17+
end
18+
end
19+
end

lib/pact_broker/api/resources/latest_verifications_for_consumer_version.rb

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
module PactBroker
88
module Api
99
module Resources
10-
1110
class LatestVerificationsForConsumerVersion < BaseResource
1211

1312
def allowed_methods

lib/pact_broker/api/resources/pact.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def content_types_provided
2828
[["application/hal+json", :to_json],
2929
["application/json", :to_json],
3030
["text/html", :to_html],
31-
["application/vnd.pactbroker.v1+json", :to_extended_json]
31+
["application/vnd.pactbrokerextended.v1+json", :to_extended_json]
3232
]
3333
end
3434

lib/pact_broker/api/resources/verification.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
require 'pact_broker/domain/verification'
44
require 'pact_broker/api/contracts/verification_contract'
55
require 'pact_broker/api/decorators/verification_decorator'
6+
require 'pact_broker/api/decorators/extended_verification_decorator'
67

78
module PactBroker
89
module Api
910
module Resources
1011
class Verification < BaseResource
1112
def content_types_provided
12-
[["application/hal+json", :to_json], ["application/json", :to_json]]
13+
[
14+
["application/hal+json", :to_json],
15+
["application/json", :to_json],
16+
["application/vnd.pactbrokerextended.v1+json", :to_extended_json]
17+
]
1318
end
1419

1520
# Remember to update latest_verification_id_for_pact_version_and_provider_version
@@ -26,6 +31,10 @@ def to_json
2631
decorator_for(verification).to_json(user_options: {base_url: base_url})
2732
end
2833

34+
def to_extended_json
35+
extended_decorator_for(verification).to_json(user_options: {base_url: base_url})
36+
end
37+
2938
private
3039

3140
def verification
@@ -35,6 +44,10 @@ def verification
3544
def decorator_for model
3645
PactBroker::Api::Decorators::VerificationDecorator.new(model)
3746
end
47+
48+
def extended_decorator_for model
49+
PactBroker::Api::Decorators::ExtendedVerificationDecorator.new(model)
50+
end
3851
end
3952
end
4053
end

lib/pact_broker/verifications/repository.rb

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def find consumer_name, provider_name, pact_version_sha, verification_number
5454
.verification_number(verification_number).single_record
5555
end
5656

57+
def find_latest_for_pact(pact)
58+
PactBroker::Pacts::PactPublication.where(id: pact.id).single_record.latest_verification
59+
end
60+
5761
def search_for_latest consumer_name, provider_name
5862
query = LatestVerificationForPactVersion
5963
.select_all_qualified

lib/pact_broker/verifications/service.rb

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def find params
4949
verification_repository.find(params.fetch(:consumer_name), params.fetch(:provider_name), params.fetch(:pact_version_sha), params.fetch(:verification_number))
5050
end
5151

52+
def find_latest_for_pact(pact)
53+
verification_repository.find_latest_for_pact(pact)
54+
end
55+
5256
def find_by_id id
5357
PactBroker::Domain::Verification.find(id: id)
5458
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'spec/support/test_data_builder'
2+
3+
describe "Get latest verification for pact" do
4+
before do
5+
td
6+
.create_consumer("Consumer")
7+
.create_consumer_version("1.2.3")
8+
.create_provider("Another provider")
9+
.create_pact
10+
.create_verification(number: 1, provider_version: "5")
11+
.create_provider("Provider")
12+
.create_pact
13+
.create_verification(number: 1, provider_version: "3")
14+
.create_verification(number: 2, provider_version: "4")
15+
end
16+
17+
let(:last_response_body) { JSON.parse(subject.body, symbolize_names: true) }
18+
let(:content_type) { "application/vnd.pactbrokerextended.v1+json" }
19+
20+
subject { get(path, nil, "HTTP_ACCEPT" => content_type) }
21+
22+
context "by pact version" do
23+
let!(:path) { "/pacts/provider/Provider/consumer/Consumer/pact-version/#{td.pact.pact_version_sha}/verification-results/latest" }
24+
25+
it "returns a 200 OK" do
26+
expect(subject.status).to eq 200
27+
expect(subject.headers['Content-Type']).to include content_type
28+
end
29+
30+
it "returns the verification" do
31+
expect(last_response_body[:providerApplicationVersion]).to eq "4"
32+
end
33+
end
34+
35+
context "by consumer version" do
36+
let(:path) { "/pacts/provider/Provider/consumer/Consumer/version/1.2.3/verification-results/latest" }
37+
38+
it "returns the verification" do
39+
expect(last_response_body[:providerApplicationVersion]).to eq "4"
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)