Skip to content

Commit

Permalink
feat(can-i-deploy): make verification results URL reference the appro…
Browse files Browse the repository at this point in the history
…priate consumer version as the pb:pact-version
  • Loading branch information
bethesque committed Oct 1, 2021
1 parent 89a612d commit 145bb37
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 21 deletions.
1 change: 1 addition & 0 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def self.build_api(application_context = PactBroker::ApplicationContext.default_
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"}
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"}
add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "pact-version", :pact_version_sha, "verification-results", :verification_number], Api::Resources::Verification, {resource_name: "verification_result"}
add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "pact-version", :pact_version_sha, "metadata", :metadata, "verification-results", :verification_number], Api::Resources::Verification, {resource_name: "verification_result"}
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"}
add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "latest", "verification-results","latest"], Api::Resources::LatestVerificationForLatestPact, {resource_name: "latest_verification_results_for_latest_pact_publication"}
add ["pacts", "provider", :provider_name, "consumer", :consumer_name, "latest", :tag, "verification-results","latest"], Api::Resources::LatestVerificationForLatestPact, {resource_name: "latest_verification_results_for_latest_tagged_pact_publication"}
Expand Down
4 changes: 3 additions & 1 deletion lib/pact_broker/api/decorators/matrix_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ def pact_hash(line, base_url)

def verification_hash(line, base_url)
if !line.success.nil?
url_params = { consumer_name: line.consumer_name,
url_params = {
consumer_name: line.consumer_name,
provider_name: line.provider_name,
pact_version_sha: line.pact_version_sha,
consumer_version_number: line.consumer_version_number,
verification_number: line.verification_number
}
{
Expand Down
8 changes: 5 additions & 3 deletions lib/pact_broker/api/decorators/verification_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ class TagDecorator < BaseDecorator
end

link :self do | options |
pact = options[:pact] || represented.latest_pact_publication
{
title: "Verification result",
name: "Verification result #{represented.number} for #{represented.latest_pact_publication.name}",
name: "Verification result #{represented.number} for #{pact.name}",
href: verification_url(represented, options.fetch(:base_url), )
}
end

link "pb:pact-version" do | options |
pact = options[:pact] || represented.latest_pact_publication
{
title: "Pact",
name: represented.latest_pact_publication.name,
href: pact_url(options.fetch(:base_url), represented.latest_pact_publication)
name: pact.name,
href: pact_version_with_consumer_version_metadata_url(pact, options.fetch(:base_url))
}
end

Expand Down
32 changes: 26 additions & 6 deletions lib/pact_broker/api/pact_broker_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def pact_version_url pact, base_url = ""
"#{pactigration_base_url(base_url, pact)}/pact-version/#{pact.pact_version_sha}"
end

def pact_version_with_consumer_version_metadata_url pact, base_url = ""
"#{pactigration_base_url(base_url, pact)}/pact-version/#{pact.pact_version_sha}/metadata/#{encode_metadata(build_metadata_for_consumer_version_number(pact.consumer_version_number))}"
end

def pact_version_url_with_metadata pact, metadata, base_url = ""
if metadata && metadata.any?
"#{pact_version_url(pact, base_url)}/metadata/#{encode_metadata(metadata)}"
Expand Down Expand Up @@ -166,12 +170,28 @@ def verification_url verification, base_url = ""
end

def verification_url_from_params params, base_url = ""
[ base_url, "pacts",
"provider", url_encode(params.fetch(:provider_name)),
"consumer", url_encode(params.fetch(:consumer_name)),
"pact-version", params.fetch(:pact_version_sha),
"verification-results", params.fetch(:verification_number)
].join("/")
if params[:consumer_version_number]
metadata = encode_metadata(build_metadata_for_consumer_version_number(params[:consumer_version_number]))

[
base_url,
"pacts",
"provider", url_encode(params.fetch(:provider_name)),
"consumer", url_encode(params.fetch(:consumer_name)),
"pact-version", params.fetch(:pact_version_sha),
"metadata", metadata,
"verification-results", params.fetch(:verification_number)
].join("/")
else
[
base_url,
"pacts",
"provider", url_encode(params.fetch(:provider_name)),
"consumer", url_encode(params.fetch(:consumer_name)),
"pact-version", params.fetch(:pact_version_sha),
"verification-results", params.fetch(:verification_number)
].join("/")
end
end

def latest_verifications_for_consumer_version_url version, base_url
Expand Down
7 changes: 5 additions & 2 deletions lib/pact_broker/api/resources/verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
require "pact_broker/api/contracts/verification_contract"
require "pact_broker/api/decorators/verification_decorator"
require "pact_broker/api/decorators/extended_verification_decorator"
require "pact_broker/api/resources/metadata_resource_methods"

module PactBroker
module Api
module Resources
class Verification < BaseResource
include MetadataResourceMethods

def content_types_provided
[
["application/hal+json", :to_json],
Expand All @@ -35,11 +38,11 @@ def resource_exists?
end

def to_json
decorator_for(verification).to_json(decorator_options)
decorator_for(verification).to_json(decorator_options(pact: pact))
end

def to_extended_json
extended_decorator_for(verification).to_json(decorator_options)
extended_decorator_for(verification).to_json(decorator_options(pact: pact))
end

def delete_resource
Expand Down
8 changes: 7 additions & 1 deletion lib/pact_broker/pacts/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Metadata

MAPPINGS = [
[:consumer_version_tags, "cvt"],
[:consumer_version_number, "cvn"], # for old urls
[:consumer_version_number, "cvn"], # for old urls and build_metadata_for_consumer_version_number
[:consumer_version_id, "cv"],
[:wip, "w"],
[:pending, "p"],
Expand Down Expand Up @@ -33,6 +33,12 @@ def build_metadata_for_latest_pact(pact, selection_parameters)
end
end

def build_metadata_for_consumer_version_number(consumer_version_number)
{
"cvn" => consumer_version_number
}
end

# When a pact is published, and a webhook is triggered, this stores
# the current tags and consumer version number in the metadata parameter of the
# pact version URL that is made available in the webhook template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ module Decorators
verifiedAt: "2017-12-31T00:00:00+00:00",
_links: {
self: {
href: "http://example.org/pacts/provider/Provider/consumer/Consumer/pact-version/1234/verification-results/1"
href: "http://example.org/pacts/provider/Provider/consumer/Consumer/pact-version/1234/metadata/Y3ZuPTEuMC4w/verification-results/1"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Decorators
describe VerificationDecorator do
before do
allow_any_instance_of(VerificationDecorator).to receive(:verification_triggered_webhooks_url).and_return("http://triggered-webhooks")
allow_any_instance_of(VerificationDecorator).to receive(:pact_version_with_consumer_version_metadata_url).and_return("http://pact")
end

let(:verification) do
Expand All @@ -18,25 +19,25 @@ module Decorators
test_results: { "arbitrary" => "json" },
build_url: "http://build-url",
pact_version_sha: "1234",
latest_pact_publication: pact_publication,
execution_date: DateTime.now,
provider_version_tags: provider_version_tags,
verified_by_implementation: "Ruby",
verified_by_version: "1234")
end

let(:pact_publication) do
instance_double("PactBroker::Pacts::PactPublication",
let(:pact) do
instance_double("PactBroker::Domain::Pact",
name: "A name",
provider_name: "Provider",
consumer_name: "Consumer",
consumer_version_number: "1.2.3"
consumer_version_number: "1.2.3",
pact_version_sha: "1234",
)
end

let(:provider_version_tags) { [instance_double(PactBroker::Tags::TagWithLatestFlag, name: "prod", latest?: true)] }

let(:options) { { user_options: { base_url: "http://example.org" } } }
let(:options) { { user_options: { base_url: "http://example.org", pact: pact } } }

let(:json) { VerificationDecorator.new(verification).to_json(options) }

Expand All @@ -63,7 +64,7 @@ module Decorators
end

it "includes a link to its pact" do
expect(subject[:_links][:'pb:pact-version'][:href]).to match %r{http://example.org/pacts/}
expect(subject[:_links][:'pb:pact-version'][:href]).to eq "http://pact"
end

it "includes a link to the triggered webhooks" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Decorators
end

let(:provider_version_tags) { [instance_double(PactBroker::Tags::TagWithLatestFlag, name: "prod", latest?: true)] }
let(:pact) { instance_double("PactBroker::Domain::Pact", name: "Some pact", consumer_name: "Foo", provider_name: "Bar", consumer_version_number: "1.2.3") }
let(:pact) { instance_double("PactBroker::Domain::Pact", name: "Some pact", consumer_name: "Foo", provider_name: "Bar", consumer_version_number: "1.2.3", pact_version_sha: "1234") }
let(:is_pending) { true }
let(:options) { {base_url: "http://example.org", consumer_name: "Foo", consumer_version_number: "1.2.3", resource_url: "http://self"} }

Expand Down
18 changes: 18 additions & 0 deletions spec/lib/pact_broker/api/pact_broker_urls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ module Api
end
end

describe "verification_url_from_params" do
context "with consumer_version_number" do
let(:params) do
{
consumer_name: consumer_name,
provider_name: provider_name,
pact_version_sha: "1234",
consumer_version_number: "2",
verification_number: "1"
}
end

subject { PactBrokerUrls.verification_url_from_params(params, base_url)}

it { is_expected.to eq "http://example.org/pacts/provider/Bar%2FBar/consumer/Foo%2FFoo/pact-version/1234/metadata/Y3ZuPTI/verification-results/1" }
end
end

describe "templated_diff_url" do
subject { PactBrokerUrls.templated_diff_url(pact, base_url) }

Expand Down

0 comments on commit 145bb37

Please sign in to comment.