diff --git a/lib/pact_broker/index/service.rb b/lib/pact_broker/index/service.rb index 27d4e3b58..08c60f0a2 100644 --- a/lib/pact_broker/index/service.rb +++ b/lib/pact_broker/index/service.rb @@ -54,12 +54,23 @@ def self.find_index_items options = {} tag_names = row.consumer_version_tags.collect(&:name) end + overall_latest = overall_latest_publication_ids.include?(row.pact_publication_id) + latest_verification = if overall_latest + verification_repository.find_latest_verification_for row.consumer_name, row.provider_name + else + tag_names.collect do | tag_name | + verification_repository.find_latest_verification_for row.consumer_name, row.provider_name, tag_name + end.compact.sort do | v1, v2 | + v1.provider_version.order <=> v2.provider_version.order + end.last + end + index_items << PactBroker::Domain::IndexItem.create( row.consumer, row.provider, row.pact, - overall_latest_publication_ids.include?(row.pact_publication_id), - row.latest_verification, + overall_latest, + latest_verification, row.webhooks, row.latest_triggered_webhooks, row.consumer_head_tag_names, diff --git a/spec/lib/pact_broker/index/service_spec.rb b/spec/lib/pact_broker/index/service_spec.rb index 8a05027ad..ee51c1945 100644 --- a/spec/lib/pact_broker/index/service_spec.rb +++ b/spec/lib/pact_broker/index/service_spec.rb @@ -188,6 +188,57 @@ module Index end end end + + context "when a pact with a tag has been verified, and then a new changed version has been published with the same tag" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .create_consumer_version_tag("feat-x") + .comment("latest verification for feat-x tag") + .create_verification(provider_version: "1") + .comment("latest feat-x version") + .create_consumer_version("2") + .create_consumer_version_tag("feat-x") + .comment("latest overall version") + .create_consumer_version("3") + .create_pact + .comment("latest overall verification") + .create_verification(provider_version: "2") + + end + + let(:options) { { tags: true } } + + it "returns the latest feat-x verification for the latest feat-x pact" do + expect(rows.last.tag_names).to eq ["feat-x"] + expect(rows.last.provider_version_number).to eq "1" + end + end + + context "when a pact with two tags has been verified, and then a new changed version has been published with two tags" do + before do + td.create_pact_with_hierarchy("Foo", "1", "Bar") + .create_consumer_version_tag("feat-x") + .create_verification(provider_version: "1", comment: "latest feat-x verif") + .create_consumer_version("2") + .create_consumer_version_tag("feat-y") + .create_pact + .create_verification(provider_version: "2", comment: "latest feat-y verif") + .create_consumer_version("3") + .create_consumer_version_tag("feat-x") + .create_consumer_version_tag("feat-y") + .create_pact + .create_consumer_version("4") + .create_pact + end + + 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" + end + end end end end diff --git a/spec/support/test_data_builder.rb b/spec/support/test_data_builder.rb index b6731c939..c505e2e9b 100644 --- a/spec/support/test_data_builder.rb +++ b/spec/support/test_data_builder.rb @@ -44,6 +44,10 @@ def initialize(params = {}) @auto_refresh_matrix = params.fetch(:auto_refresh_matrix, true) end + def comment *args + self + end + def refresh_matrix if auto_refresh_matrix params = {} @@ -124,23 +128,27 @@ def create_tag_with_hierarchy pacticipant_name, pacticipant_version, tag_name PactBroker::Domain::Tag.create(name: tag_name, version: version) end - def create_pacticipant pacticipant_name + def create_pacticipant pacticipant_name, params = {} + params.delete(:comment) @pacticipant = PactBroker::Domain::Pacticipant.create(:name => pacticipant_name) self end - def create_consumer consumer_name = "Consumer #{model_counter}" + def create_consumer consumer_name = "Consumer #{model_counter}", params = {} + params.delete(:comment) create_pacticipant consumer_name @consumer = @pacticipant self end - def use_consumer consumer_name + def use_consumer consumer_name, params = {} + params.delete(:comment) @consumer = PactBroker::Domain::Pacticipant.find(:name => consumer_name) self end - def create_provider provider_name = "Provider #{model_counter}" + def create_provider provider_name = "Provider #{model_counter}", params = {} + params.delete(:comment) create_pacticipant provider_name @provider = @pacticipant self @@ -151,17 +159,20 @@ def use_provider provider_name self end - def create_version version_number = "1.0.#{model_counter}" + def create_version version_number = "1.0.#{model_counter}", params = {} + params.delete(:comment) @version = PactBroker::Domain::Version.create(:number => version_number, :pacticipant => @pacticipant) self end - def create_consumer_version version_number = "1.0.#{model_counter}" + def create_consumer_version version_number = "1.0.#{model_counter}", params = {} + params.delete(:comment) @consumer_version = PactBroker::Domain::Version.create(:number => version_number, :pacticipant => @consumer) self end - def create_provider_version version_number = "1.0.#{model_counter}" + def create_provider_version version_number = "1.0.#{model_counter}", params = {} + params.delete(:comment) @version = PactBroker::Domain::Version.create(:number => version_number, :pacticipant => @provider) @provider_version = @version self @@ -177,17 +188,20 @@ def use_provider_version version_number self end - def create_tag tag_name + def create_tag tag_name, params = {} + params.delete(:comment) @tag = PactBroker::Domain::Tag.create(name: tag_name, version: @version) self end - def create_consumer_version_tag tag_name + def create_consumer_version_tag tag_name, params = {} + params.delete(:comment) @tag = PactBroker::Domain::Tag.create(name: tag_name, version: @consumer_version) self end - def create_provider_version_tag tag_name + def create_provider_version_tag tag_name, params = {} + params.delete(:comment) @tag = PactBroker::Domain::Tag.create(name: tag_name, version: @provider_version) self end @@ -198,6 +212,7 @@ def create_label label_name end def create_pact params = {} + params.delete(:comment) @pact = PactBroker::Pacts::Repository.new.create({version_id: @consumer_version.id, consumer_id: @consumer.id, provider_id: @provider.id, json_content: params[:json_content] || default_json_content}) set_created_at_if_set params[:created_at], :pact_publications, {id: @pact.id} set_created_at_if_set params[:created_at], :pact_versions, {sha: @pact.pact_version_sha} @@ -207,6 +222,7 @@ def create_pact params = {} end def republish_same_pact params = {} + params.delete(:comment) last_pact_version = PactBroker::Pacts::PactVersion.order(:id).last create_pact json_content: last_pact_version.content self @@ -220,6 +236,7 @@ def revise_pact json_content = nil end def create_webhook params = {} + params.delete(:comment) uuid = params[:uuid] || PactBroker::Webhooks::Service.next_uuid event_params = params[:events] || [{ name: PactBroker::Webhooks::WebhookEvent::DEFAULT_EVENT_NAME }] events = event_params.collect{ |e| PactBroker::Webhooks::WebhookEvent.new(e) } @@ -230,6 +247,7 @@ def create_webhook params = {} end def create_triggered_webhook params = {} + params.delete(:comment) trigger_uuid = params[:trigger_uuid] || webhook_service.next_uuid @triggered_webhook = webhook_repository.create_triggered_webhook trigger_uuid, @webhook, @pact, PactBroker::Webhooks::Service::RESOURCE_CREATION @triggered_webhook.update(status: params[:status]) if params[:status] @@ -238,6 +256,7 @@ def create_triggered_webhook params = {} end def create_webhook_execution params = {} + params.delete(:comment) logs = params[:logs] || "logs" webhook_execution_result = PactBroker::Domain::WebhookExecutionResult.new(OpenStruct.new(code: "200"), logs, nil) @webhook_execution = PactBroker::Webhooks::Repository.new.create_execution @triggered_webhook, webhook_execution_result @@ -248,6 +267,7 @@ def create_webhook_execution params = {} end def create_deprecated_webhook_execution params = {} + params.delete(:comment) create_webhook_execution params Sequel::Model.db[:webhook_executions].where(id: webhook_execution.id).update( triggered_webhook_id: nil, @@ -260,6 +280,7 @@ def create_deprecated_webhook_execution params = {} end def create_verification parameters = {} + parameters.delete(:comment) tag_names = [parameters.delete(:tag_names), parameters.delete(:tag_name)].flatten.compact provider_version_number = parameters[:provider_version] || '4.5.6' default_parameters = {success: true, number: 1, test_results: {some: 'results'}} @@ -278,6 +299,7 @@ def create_verification parameters = {} end def create_certificate options = {path: 'spec/fixtures/single-certificate.pem'} + options.delete(:comment) PactBroker::Certificates::Certificate.create(uuid: SecureRandom.urlsafe_base64, content: File.read(options[:path])) self end