diff --git a/lib/pact_broker/api/decorators/extended_pact_decorator.rb b/lib/pact_broker/api/decorators/extended_pact_decorator.rb index 760fc7600..c284a9a23 100644 --- a/lib/pact_broker/api/decorators/extended_pact_decorator.rb +++ b/lib/pact_broker/api/decorators/extended_pact_decorator.rb @@ -3,6 +3,9 @@ module PactBroker module Api module Decorators + # Make a different content type for adding extra information for the UI, as + # some pact parsing tools blow up when there are unexpected keys :| + class ExtendedPactDecorator < PactDecorator class TagDecorator < BaseDecorator property :name diff --git a/lib/pact_broker/api/decorators/verification_decorator.rb b/lib/pact_broker/api/decorators/verification_decorator.rb index 15df3d1a4..5f465ce42 100644 --- a/lib/pact_broker/api/decorators/verification_decorator.rb +++ b/lib/pact_broker/api/decorators/verification_decorator.rb @@ -4,6 +4,10 @@ module PactBroker module Api module Decorators class VerificationDecorator < BaseDecorator + class TagDecorator < BaseDecorator + property :name + property :latest?, as: :latest + end property :provider_name, as: :providerName, writeable: false property :provider_version_number, as: :providerApplicationVersion, writeable: false @@ -11,6 +15,7 @@ class VerificationDecorator < BaseDecorator property :execution_date, as: :verificationDate property :build_url, as: :buildUrl property :test_results, as: :testResults + collection :provider_version_tags, as: :tags, embedded: true, extend: TagDecorator link :self do | options | { diff --git a/lib/pact_broker/api/resources/pact.rb b/lib/pact_broker/api/resources/pact.rb index 6487a8bec..e08bad373 100644 --- a/lib/pact_broker/api/resources/pact.rb +++ b/lib/pact_broker/api/resources/pact.rb @@ -28,7 +28,7 @@ def content_types_provided [["application/hal+json", :to_json], ["application/json", :to_json], ["text/html", :to_html], - ["application/vnd.pactbroker.pact.v1+json", :to_extended_json] + ["application/vnd.pactbroker.v1+json", :to_extended_json] ] end diff --git a/lib/pact_broker/domain/verification.rb b/lib/pact_broker/domain/verification.rb index a2f34403c..043ca4f3a 100644 --- a/lib/pact_broker/domain/verification.rb +++ b/lib/pact_broker/domain/verification.rb @@ -1,9 +1,10 @@ -require 'pact_broker/db' -require 'pact_broker/repositories/helpers' require 'json' +require 'sequel' +require 'pact_broker/repositories/helpers' +require 'pact_broker/tags/tag_with_latest_flag' -module PactBroker +module PactBroker module Domain class Verification < Sequel::Model @@ -12,6 +13,7 @@ class Verification < Sequel::Model associate(:many_to_one, :provider_version, class: "PactBroker::Domain::Version", key: :provider_version_id, primary_key: :id) associate(:many_to_one, :provider, class: "PactBroker::Domain::Pacticipant", key: :provider_id, primary_key: :id) associate(:many_to_one, :consumer, class: "PactBroker::Domain::Pacticipant", key: :consumer_id, primary_key: :id) + associate(:one_to_many, :provider_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :provider_version_id, key: :version_id) plugin :serialization, :json, :test_results def before_create diff --git a/lib/pact_broker/pacts/pact_publication.rb b/lib/pact_broker/pacts/pact_publication.rb index 724826bbb..d66e730cb 100644 --- a/lib/pact_broker/pacts/pact_publication.rb +++ b/lib/pact_broker/pacts/pact_publication.rb @@ -31,6 +31,8 @@ def before_create self.revision_number ||= 1 end + # The names of the tags for which this pact is the latest pact with that tag + # (ie. it is not necessarily the pact for the latest consumer version with the given tag) def head_tag_names LatestTaggedPactPublications.where(id: id).select(:tag_name).collect{|t| t[:tag_name]} end diff --git a/spec/lib/pact_broker/api/decorators/verification_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/verification_decorator_spec.rb index f05245e6e..9da68c6e8 100644 --- a/spec/lib/pact_broker/api/decorators/verification_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/verification_decorator_spec.rb @@ -19,7 +19,8 @@ module Decorators build_url: 'http://build-url', pact_version_sha: '1234', latest_pact_publication: pact_publication, - execution_date: DateTime.now) + execution_date: DateTime.now, + provider_version_tags: provider_version_tags) end let(:pact_publication) do @@ -31,6 +32,8 @@ module Decorators ) end + let(:provider_version_tags) { [instance_double(PactBroker::Tags::TagWithLatestFlag, name: 'prod', latest?: true)] } + let(:options) { { user_options: { base_url: 'http://example.org' } } } let(:json) { VerificationDecorator.new(verification).to_json(options) } @@ -64,6 +67,10 @@ module Decorators it "includes a link to the triggered webhooks" do expect(subject[:_links][:'pb:triggered-webhooks'][:href]).to eq "http://triggered-webhooks" end + + it "includes the provider version tags" do + expect(subject[:_embedded][:tags].first).to eq name: 'prod', latest: true + end end end end