diff --git a/lib/pact_broker/api/resources/dashboard.rb b/lib/pact_broker/api/resources/dashboard.rb index 19fdf37fd..a10eddb78 100644 --- a/lib/pact_broker/api/resources/dashboard.rb +++ b/lib/pact_broker/api/resources/dashboard.rb @@ -5,9 +5,7 @@ module PactBroker module Api module Resources - class Dashboard < BaseResource - def content_types_provided [ ["application/hal+json", :to_json], @@ -30,7 +28,7 @@ def to_text private def index_items - index_service.find_index_items(identifier_from_path.merge(tags: true)) + index_service.find_index_items(identifier_from_path.merge(tags: true, dashboard: true)) end end end diff --git a/lib/pact_broker/index/service.rb b/lib/pact_broker/index/service.rb index 325ce72eb..28fe31dc6 100644 --- a/lib/pact_broker/index/service.rb +++ b/lib/pact_broker/index/service.rb @@ -35,14 +35,29 @@ def self.find_index_items options = {} end rows = rows.all.group_by(&:pact_publication_id).values.collect{ | rows| Matrix::AggregatedRow.new(rows) } + + rows.sort.collect do | row | + # The concept of "stale" (the pact used to be verified but then it changed and we haven't got + # a new verification result yet) only really make sense if we're trying to summarise + # the latest state of an integration. Once we start showing multiple pacts for each + # integration (ie. the latest for each tag) then each pact version is either verified, + # or it's not verified. + # For backwards compatiblity with the existing UI, don't change the 'stale' concept for the OSS + # UI - just ensure we don't use it for the new dashboard endpoint with the consumer/provider specified. + latest_verification = if options[:dashboard] + row.latest_verification_for_pact_version + else + row.latest_verification_for_pseudo_branch + end + # TODO simplify. Do we really need 3 layers of abstraction? PactBroker::Domain::IndexItem.create( row.consumer, row.provider, row.pact, row.overall_latest?, - row.latest_verification_for_pseudo_branch, + latest_verification, row.webhooks, row.latest_triggered_webhooks, options[:tags] ? row.consumer_head_tag_names : [], diff --git a/lib/pact_broker/matrix/aggregated_row.rb b/lib/pact_broker/matrix/aggregated_row.rb index 48a649141..5c4c1ad37 100644 --- a/lib/pact_broker/matrix/aggregated_row.rb +++ b/lib/pact_broker/matrix/aggregated_row.rb @@ -43,6 +43,14 @@ def latest_verification_for_pseudo_branch end end + def latest_verification_for_pact_version + @latest_verificaton_for_pact_version ||= begin + matrix_rows.collect do | row| + row.verification + end.compact.sort{ |v1, v2| v1.id <=> v2.id }.last + end + end + # The list of tag names for which this pact publication is the most recent with that tag # There could, however, be a later consumer version that does't have a pact (perhaps because it was deleted) # that has the same tag. diff --git a/spec/lib/pact_broker/index/service_spec.rb b/spec/lib/pact_broker/index/service_spec.rb index ee51c1945..9f28e25d9 100644 --- a/spec/lib/pact_broker/index/service_spec.rb +++ b/spec/lib/pact_broker/index/service_spec.rb @@ -233,10 +233,35 @@ module Index let(:options) { { tags: true } } - it "returns the latest of the feat-x and feat-y verifications" do - expect(rows.last.consumer_version_number).to eq "3" - expect(rows.last.tag_names.sort).to eq ["feat-x", "feat-y"] - expect(rows.last.provider_version_number).to eq "2" + context "with tags=true" do + it "returns the tags for the pacts" do + expect(rows.last.tag_names.sort).to eq ["feat-x", "feat-y"] + end + end + + context "with tags=false" do + let(:options) { { tags: false } } + + it "does not return the tags for the pacts" do + expect(rows.last.tag_names.sort).to eq [] + end + end + + context "with dashboard=true" do + let(:options) { { dashboard: true } } + + it "returns the latest verification as nil as the pact version itself has not been verified" do + expect(rows.last.provider_version_number).to be nil + end + end + + context "with dashboard=false" do + let(:options) { { } } + + it "returns the latest of the feat-x and feat-y verifications because we are summarising the entire integration (backwards compat for OSS index)" do + expect(rows.last.consumer_version_number).to eq "4" + expect(rows.last.provider_version_number).to eq "2" + end end end end diff --git a/spec/lib/pact_broker/matrix/aggregated_row_spec.rb b/spec/lib/pact_broker/matrix/aggregated_row_spec.rb index a47d0e7f0..d66f1c229 100644 --- a/spec/lib/pact_broker/matrix/aggregated_row_spec.rb +++ b/spec/lib/pact_broker/matrix/aggregated_row_spec.rb @@ -74,6 +74,27 @@ module Matrix end end end + + describe "latest_verification_for_pact_version" do + let(:row_1) do + instance_double('PactBroker::Matrix::HeadRow', + verification: verification_1) + end + let(:row_2) do + instance_double('PactBroker::Matrix::HeadRow', + verification: verification_2) + end + let(:verification_1) { instance_double('PactBroker::Domain::Verification', id: 2) } + let(:verification_2) { instance_double('PactBroker::Domain::Verification', id: 1) } + let(:rows) { [row_1, row_2] } + let(:aggregated_row) { AggregatedRow.new(rows) } + + subject { aggregated_row.latest_verification_for_pact_version } + + it "returns the verification with the largest id" do + expect(subject.id).to eq 2 + end + end end end end