From f0cc32b50257d3d253db7e677741bf1a4c32c247 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 10 Oct 2017 15:26:56 +1100 Subject: [PATCH] feat(matrix): add pact publication and verification dates to response --- .../api/decorators/matrix_decorator.rb | 6 ++-- lib/pact_broker/matrix/repository.rb | 30 +++++++++++-------- .../api/decorators/matrix_decorator_spec.rb | 14 ++++++--- .../lib/pact_broker/matrix/repository_spec.rb | 5 +++- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/lib/pact_broker/api/decorators/matrix_decorator.rb b/lib/pact_broker/api/decorators/matrix_decorator.rb index 8ce51df62..94b0f8aad 100644 --- a/lib/pact_broker/api/decorators/matrix_decorator.rb +++ b/lib/pact_broker/api/decorators/matrix_decorator.rb @@ -75,8 +75,8 @@ def provider_hash(line, provider, base_url) } } - if !line[:provider_version].nil? - hash[:version] = { number: line[:provider_version] } + if !line[:provider_version_number].nil? + hash[:version] = { number: line[:provider_version_number] } end hash @@ -84,6 +84,7 @@ def provider_hash(line, provider, base_url) def pact_hash(line, base_url) { + createdAt: line[:pact_created_at].to_datetime.xmlschema, _links: { self: { href: pact_url_from_params(base_url, line) @@ -96,6 +97,7 @@ def verification_hash(line, base_url) if !line[:success].nil? { success: line[:success], + verifiedAt: line[:verification_executed_at].to_datetime.xmlschema, _links: { self: { href: verification_url(OpenStruct.new(line), base_url) diff --git a/lib/pact_broker/matrix/repository.rb b/lib/pact_broker/matrix/repository.rb index 87937566b..f4eaf02df 100644 --- a/lib/pact_broker/matrix/repository.rb +++ b/lib/pact_broker/matrix/repository.rb @@ -7,12 +7,10 @@ class Repository include PactBroker::Repositories def find pacticipant_1_name, pacticipant_2_name - PactBroker::Pacts::LatestPactPublicationsByConsumerVersion - .left_outer_join(:latest_verifications, pact_version_id: :pact_version_id) - .pacticipants(pacticipant_1_name, pacticipant_2_name) - .reverse(:consumer_version_order) - .all - .collect(&:values) + version_ids = PactBroker::Domain::Version.select(Sequel[:versions][:id]) + .join(:pacticipants, id: :pacticipant_id) + .where(Sequel[:pacticipants][:name] => [pacticipant_1_name, pacticipant_2_name]) + find_for_version_ids(version_ids) end ## @@ -23,19 +21,25 @@ def find_compatible_pacticipant_versions criteria version_repository.find_by_pacticipant_name_and_number(key, value).id end - query = PactBroker::Pacts::LatestPactPublicationsByConsumerVersion - .select_append(:consumer_version_number, :provider_name, :consumer_name, :provider_version_id, :provider_version_number, :success, :execution_date) + find_for_version_ids(version_ids) + .group_by{|line| [line[:consumer_version_number], line[:provider_version_number]]} + .values + .collect(&:last) + .select{ |line | line[:success] } + end + + def find_for_version_ids version_ids + PactBroker::Pacts::LatestPactPublicationsByConsumerVersion + .select_append(:consumer_version_number, :provider_name, :consumer_name, :provider_version_id, :provider_version_number, :success) + .select_append(Sequel[:latest_pact_publications_by_consumer_versions][:created_at].as(:pact_created_at)) .select_append(Sequel[:all_verifications][:number]) .select_append(Sequel[:all_verifications][:id].as(:verification_id)) + .select_append(Sequel[:all_verifications][:execution_date].as(:verification_executed_at)) .left_outer_join(:all_verifications, pact_version_id: :pact_version_id) - .where(provider_version_id: version_ids) + .where(provider_version_id: version_ids).or(provider_version_id: nil) .where(consumer_version_id: version_ids) .order(:execution_date, :verification_id) .collect(&:values) - .group_by{|line| [line[:consumer_version_number], line[:provider_version_number]]} - .values - .collect(&:last) - .select{ |line | line[:success] } end end end diff --git a/spec/lib/pact_broker/api/decorators/matrix_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/matrix_decorator_spec.rb index bc66f0349..24e9d8621 100644 --- a/spec/lib/pact_broker/api/decorators/matrix_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/matrix_decorator_spec.rb @@ -5,17 +5,20 @@ module Api module Decorators describe MatrixPactDecorator do describe "to_json" do + let(:verification_date) { DateTime.new(2017, 12, 31) } + let(:pact_created_at) { DateTime.new(2017, 1, 1) } let(:line_1) do { consumer_name: "Consumer", consumer_version_number: "1.0.0", pact_version_sha: "1234", - provider_version: "4.5.6", + pact_created_at: pact_created_at, + provider_version_number: "4.5.6", provider_name: "Provider", success: true, number: 1, build_url: nil, - execution_date: DateTime.now + verification_executed_at: verification_date } end @@ -24,12 +27,13 @@ module Decorators consumer_name: "Consumer", consumer_version_number: "1.0.0", pact_version_sha: "1234", - provider_version: nil, + pact_created_at: pact_created_at, + provider_version_number: nil, provider_name: "Provider", success: nil, number: nil, build_url: nil, - execution_date: nil + verification_executed_at: nil } end @@ -69,6 +73,7 @@ module Decorators let(:verification_hash) do { success: true, + 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" @@ -79,6 +84,7 @@ module Decorators let(:pact_hash) do { + createdAt: "2017-01-01T00:00:00+00:00", _links: { self: { href: "http://example.org/pacts/provider/Provider/consumer/Consumer/version/1.0.0" diff --git a/spec/lib/pact_broker/matrix/repository_spec.rb b/spec/lib/pact_broker/matrix/repository_spec.rb index 1b5a87c26..fdb936c4d 100644 --- a/spec/lib/pact_broker/matrix/repository_spec.rb +++ b/spec/lib/pact_broker/matrix/repository_spec.rb @@ -14,7 +14,7 @@ module Matrix .create_pact end - subject { Repository.new.find "Consumer", "Provider" } + subject { Repository.new.find("Consumer", "Provider") } it "returns the latest revision of each pact in reverse consumer_version_order" do expect(subject.count).to eq 2 @@ -58,12 +58,15 @@ module Matrix expect(subject.first[:provider_name]).to eq "B" expect(subject.first[:provider_version_number]).to eq "2" expect(subject.first[:number]).to eq 2 + expect(subject.first[:pact_created_at]).to be_datey + expect(subject.first[:verification_executed_at]).to be_datey expect(subject.last[:consumer_name]).to eq "B" expect(subject.last[:consumer_version_number]).to eq "2" expect(subject.last[:provider_name]).to eq "C" expect(subject.last[:provider_version_number]).to eq "2" expect(subject.last[:number]).to eq 1 + expect(subject.last[:pact_created_at]).to be_datey expect(subject.size).to eq 2 end