From 77265c304aa3c592e9fe37fa785c6c9f5a22bca5 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 2 Sep 2019 09:54:29 +1000 Subject: [PATCH] fix: delete triggered webhook data when deleting pacticipant version fixes: https://github.com/pact-foundation/pact_broker/issues/300 --- lib/pact_broker/versions/service.rb | 1 + lib/pact_broker/webhooks/repository.rb | 13 +++++++ spec/lib/pact_broker/versions/service_spec.rb | 3 ++ .../pact_broker/webhooks/repository_spec.rb | 35 +++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/lib/pact_broker/versions/service.rb b/lib/pact_broker/versions/service.rb index 634b5410c..e615d2f5d 100644 --- a/lib/pact_broker/versions/service.rb +++ b/lib/pact_broker/versions/service.rb @@ -21,6 +21,7 @@ def self.find_by_pacticipant_name_and_latest_tag(pacticipant_name, tag) def self.delete version tag_repository.delete_by_version_id version.id + webhook_repository.delete_triggered_webhooks_by_version_id version.id pact_repository.delete_by_version_id version.id verification_repository.delete_by_provider_version_id version.id version_repository.delete_by_id version.id diff --git a/lib/pact_broker/webhooks/repository.rb b/lib/pact_broker/webhooks/repository.rb index 577be9749..7dbb7d179 100644 --- a/lib/pact_broker/webhooks/repository.rb +++ b/lib/pact_broker/webhooks/repository.rb @@ -162,6 +162,19 @@ def delete_triggered_webhooks_by_webhook_uuid uuid TriggeredWebhook.where(id: triggered_webhook_ids).delete end + def delete_triggered_webhooks_by_version_id version_id + pact_publication_ids = PactBroker::Pacts::PactPublication.where(consumer_version_id: version_id).select_for_subquery(:id) + pact_triggered_webhook_ids = TriggeredWebhook.where(pact_publication_id: pact_publication_ids).select_for_subquery(:id) + Execution.where(triggered_webhook_id: pact_triggered_webhook_ids).delete + TriggeredWebhook.where(id: pact_triggered_webhook_ids).delete + DeprecatedExecution.where(pact_publication_id: pact_publication_ids).delete + + verification_ids = PactBroker::Domain::Verification.where(provider_version_id: version_id).select_for_subquery(:id) + verification_triggered_webhook_ids = TriggeredWebhook.where(verification_id: verification_ids).select_for_subquery(:id) + Execution.where(triggered_webhook_id: verification_triggered_webhook_ids).delete + TriggeredWebhook.where(id: verification_triggered_webhook_ids).delete + end + def delete_triggered_webhooks_by_pact_publication_ids pact_publication_ids triggered_webhook_ids = TriggeredWebhook.where(pact_publication_id: pact_publication_ids).select_for_subquery(:id) Execution.where(triggered_webhook_id: triggered_webhook_ids).delete diff --git a/spec/lib/pact_broker/versions/service_spec.rb b/spec/lib/pact_broker/versions/service_spec.rb index 8dc06eaa3..f380c3cc2 100644 --- a/spec/lib/pact_broker/versions/service_spec.rb +++ b/spec/lib/pact_broker/versions/service_spec.rb @@ -14,6 +14,9 @@ module Versions .create_consumer_version_tag("prod") .create_pact .create_verification(provider_version: "1.0.0") + .create_webhook + .create_triggered_webhook + .create_webhook_execution .and_return(:consumer_version) end diff --git a/spec/lib/pact_broker/webhooks/repository_spec.rb b/spec/lib/pact_broker/webhooks/repository_spec.rb index becd19190..72583e1ed 100644 --- a/spec/lib/pact_broker/webhooks/repository_spec.rb +++ b/spec/lib/pact_broker/webhooks/repository_spec.rb @@ -730,6 +730,41 @@ module Webhooks }.by(-1) end end + + describe "delete_triggered_webhooks_by_version_id" do + subject { Repository.new.delete_triggered_webhooks_by_version_id(version.id) } + + context "when deleting a triggered webhook by consumer version" do + let!(:version) do + td + .create_pact_with_hierarchy + .create_webhook + .create_triggered_webhook + .create_webhook_execution + .and_return(:consumer_version) + end + + it "deletes the webhooks belonging to the consumer version" do + expect { subject }.to change{ TriggeredWebhook.count }.by (-1) + end + end + + context "when deleting a triggered webhook by provider version" do + let!(:version) do + td + .create_pact_with_hierarchy + .create_verification(provider_version: "1") + .create_provider_webhook(event_names: ['provider_verification_published']) + .create_triggered_webhook + .create_webhook_execution + .and_return(:provider_version) + end + + it "deletes the webhooks belonging to the consumer version" do + expect { subject }.to change{ TriggeredWebhook.count }.by (-1) + end + end + end end end end