From 49c60c54977bbff7d95494561cf1c8dee15cddb3 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 25 Nov 2022 09:59:00 +1100 Subject: [PATCH] chore: correct policy for 'latest verifications for consumer version' resource --- ...test_verifications_for_consumer_version.rb | 2 +- lib/pact_broker/verifications/repository.rb | 25 +++++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/pact_broker/api/resources/latest_verifications_for_consumer_version.rb b/lib/pact_broker/api/resources/latest_verifications_for_consumer_version.rb index 56efea937..47519422d 100644 --- a/lib/pact_broker/api/resources/latest_verifications_for_consumer_version.rb +++ b/lib/pact_broker/api/resources/latest_verifications_for_consumer_version.rb @@ -27,7 +27,7 @@ def to_json end def policy_name - :'verifications::verifications' + :'pacticipants::pacticipant' end private diff --git a/lib/pact_broker/verifications/repository.rb b/lib/pact_broker/verifications/repository.rb index 1accdaa87..985732ec3 100644 --- a/lib/pact_broker/verifications/repository.rb +++ b/lib/pact_broker/verifications/repository.rb @@ -3,6 +3,7 @@ require "pact_broker/verifications/sequence" require "pact_broker/verifications/latest_verification_id_for_pact_version_and_provider_version" require "pact_broker/verifications/pact_version_provider_tag_successful_verification" +require "pact_broker/repositories/scopes" module PactBroker module Verifications @@ -10,6 +11,7 @@ class Repository include PactBroker::Repositories::Helpers include PactBroker::Repositories + include PactBroker::Repositories::Scopes # Ideally this would just be a sequence, but Sqlite and MySQL don't support sequences # in the way we need to use them ie. determining what the next number will be before we @@ -33,7 +35,7 @@ def create verification, provider_version_number, pact_version end def delete(verification_id) - PactBroker::Domain::Verification.where(id: verification_id).delete + scope_for(PactBroker::Domain::Verification).where(id: verification_id).delete end def update_latest_verification_id verification @@ -62,8 +64,9 @@ def update_pact_version_provider_tag_verifications(verification, tag_names) end end + # policy should be applied in resource def find consumer_name, provider_name, pact_version_sha, verification_number - PactBroker::Domain::Verification + unscoped(PactBroker::Domain::Verification) .select_all_qualified .join(:all_pact_publications, pact_version_id: :pact_version_id) .consumer(consumer_name) @@ -73,19 +76,19 @@ def find consumer_name, provider_name, pact_version_sha, verification_number end def find_latest_for_pact(pact) - PactBroker::Pacts::PactPublication.where(id: pact.id).single_record.latest_verification + scope_for(PactBroker::Pacts::PactPublication).where(id: pact.id).single_record.latest_verification end def find_latest_from_main_branch_for_pact(pact) - PactBroker::Pacts::PactPublication.where(id: pact.id).single_record.latest_main_branch_verification + scope_for(PactBroker::Pacts::PactPublication).where(id: pact.id).single_record.latest_main_branch_verification end def any_verifications?(consumer, provider) - PactBroker::Domain::Verification.where(consumer_id: consumer.id, provider_id: provider.id).any? + scope_for(PactBroker::Domain::Verification).where(consumer_id: consumer.id, provider_id: provider.id).any? end def search_for_latest consumer_name, provider_name - query = PactBroker::Domain::Verification.select_all_qualified + query = scope_for(PactBroker::Domain::Verification).select_all_qualified query = query.for_consumer_name(consumer_name) if consumer_name query = query.for_provider_name(provider_name) if provider_name query.reverse(:execution_date, :id).first @@ -94,7 +97,7 @@ def search_for_latest consumer_name, provider_name def find_latest_verifications_for_consumer_version consumer_name, consumer_version_number # Use remove_verifications_for_overridden_consumer_versions because we don't # want verifications for shadowed revisions as it would be misleading. - PactBroker::Domain::Verification + scope_for(PactBroker::Domain::Verification) .select_all_qualified .remove_verifications_for_overridden_consumer_versions .for_consumer_name_and_consumer_version_number(consumer_name, consumer_version_number) @@ -115,7 +118,7 @@ def find_latest_verification_for consumer_name, provider_name, consumer_version_ Sequel[:lp][:consumer_id] => consumer.id, Sequel[:lp][:provider_id] => provider.id } - query = PactBroker::Domain::Verification + query = scope_for(PactBroker::Domain::Verification) .select_all_qualified .join(:latest_verification_ids_for_pact_versions, { Sequel[:verifications][:id] => Sequel[:lv][:latest_verification_id] }, { table_alias: :lv }) .join(:latest_pact_publication_ids_for_consumer_versions, join_cols, { table_alias: :lp }) @@ -140,7 +143,7 @@ def find_latest_verification_for_tags consumer_name, provider_name, consumer_ver consumer_tag_filter = PactBroker::Repositories::Helpers.name_like(Sequel.qualify(:consumer_tags, :name), consumer_version_tag) provider_tag_filter = PactBroker::Repositories::Helpers.name_like(Sequel.qualify(:provider_tags, :name), provider_version_tag) - query = PactBroker::Domain::Verification + query = scope_for(PactBroker::Domain::Verification) .select_all_qualified .join(:versions, { Sequel[:provider_versions][:id] => Sequel[view_name][:provider_version_id] }, { table_alias: :provider_versions }) .join(:latest_verification_id_for_pact_version_and_provider_version, { Sequel[:lv][:verification_id] => Sequel[view_name][:id] }, { table_alias: :lv }) @@ -160,13 +163,13 @@ def find_latest_verification_for_tags consumer_name, provider_name, consumer_ver end def delete_by_provider_version_id version_id - PactBroker::Domain::Verification.where(provider_version_id: version_id).delete + scope_for(PactBroker::Domain::Verification).where(provider_version_id: version_id).delete end def delete_all_verifications_between(consumer_name, options) consumer = pacticipant_repository.find_by_name!(consumer_name) provider = pacticipant_repository.find_by_name!(options.fetch(:and)) - PactBroker::Domain::Verification.where(provider: provider, consumer: consumer).delete + scope_for(PactBroker::Domain::Verification).where(provider: provider, consumer: consumer).delete end end end