From 3dc590c31c448de1ab38af4cc2fd645376515136 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 18 Sep 2017 11:53:45 +1000 Subject: [PATCH] feat(webhook status): delete related triggered webhooks and executions when pact publication is deleted --- lib/pact_broker/pacts/service.rb | 2 ++ lib/pact_broker/webhooks/execution.rb | 9 ++++- lib/pact_broker/webhooks/repository.rb | 7 ++++ lib/pact_broker/webhooks/service.rb | 4 +++ spec/features/delete_pact_spec.rb | 6 +++- spec/lib/pact_broker/pacts/service_spec.rb | 31 ++++++++++++++++- .../pact_broker/webhooks/repository_spec.rb | 34 +++++++++++++++++++ spec/support/test_data_builder.rb | 2 ++ 8 files changed, 92 insertions(+), 3 deletions(-) diff --git a/lib/pact_broker/pacts/service.rb b/lib/pact_broker/pacts/service.rb index ebad60e4e..aabfa993e 100644 --- a/lib/pact_broker/pacts/service.rb +++ b/lib/pact_broker/pacts/service.rb @@ -31,6 +31,8 @@ def find_by_consumer_version params def delete params logger.info "Deleting pact version with params #{params}" + pact = find_pact(params) + webhook_service.delete_all_webhook_related_objects_by_pact_publication_id(pact.id) pact_repository.delete(params) end diff --git a/lib/pact_broker/webhooks/execution.rb b/lib/pact_broker/webhooks/execution.rb index 7c931d4e7..66cd2f472 100644 --- a/lib/pact_broker/webhooks/execution.rb +++ b/lib/pact_broker/webhooks/execution.rb @@ -5,7 +5,14 @@ module PactBroker module Webhooks - class Execution < Sequel::Model(PactBroker::DB.connection[:webhook_executions].select(Sequel[:webhook_executions][:id], :triggered_webhook_id, :success, :logs)) + class Execution < Sequel::Model( + PactBroker::DB.connection[:webhook_executions].select( + Sequel[:webhook_executions][:id], + :triggered_webhook_id, + :success, + :logs, + Sequel[:webhook_executions][:created_at]) + ) dataset_module do include PactBroker::Repositories::Helpers diff --git a/lib/pact_broker/webhooks/repository.rb b/lib/pact_broker/webhooks/repository.rb index 4853c75bf..a4f010545 100644 --- a/lib/pact_broker/webhooks/repository.rb +++ b/lib/pact_broker/webhooks/repository.rb @@ -109,6 +109,13 @@ def unlink_triggered_webhooks_by_webhook_uuid uuid DeprecatedExecution.where(webhook_id: Webhook.where(uuid: uuid).select(:id)).update(webhook_id: nil) end + def delete_triggered_webhooks_by_pact_publication_id pact_publication_id + triggered_webhook_ids = TriggeredWebhook.where(pact_publication_id: pact_publication_id).select_for_subquery(:id) + Execution.where(triggered_webhook_id: triggered_webhook_ids).delete + TriggeredWebhook.where(id: triggered_webhook_ids).delete + DeprecatedExecution.where(pact_publication_id: pact_publication_id).delete + end + def find_latest_triggered_webhooks consumer, provider LatestTriggeredWebhook .where(consumer: consumer, provider: provider) diff --git a/lib/pact_broker/webhooks/service.rb b/lib/pact_broker/webhooks/service.rb index 5afdb425b..1aabd21d6 100644 --- a/lib/pact_broker/webhooks/service.rb +++ b/lib/pact_broker/webhooks/service.rb @@ -50,6 +50,10 @@ def self.delete_all_webhhook_related_objects_by_pacticipant pacticipant webhook_repository.delete_by_pacticipant pacticipant end + def self.delete_all_webhook_related_objects_by_pact_publication_id pact_publication_id + webhook_repository.delete_triggered_webhooks_by_pact_publication_id pact_publication_id + end + def self.find_all webhook_repository.find_all end diff --git a/spec/features/delete_pact_spec.rb b/spec/features/delete_pact_spec.rb index 1a93eec2e..f43725fae 100644 --- a/spec/features/delete_pact_spec.rb +++ b/spec/features/delete_pact_spec.rb @@ -8,7 +8,11 @@ context "when the pact exists" do before do - TestDataBuilder.new.create_pact_with_hierarchy("A Consumer", "1.2.3", "A Provider").and_return(:pact) + TestDataBuilder.new + .create_pact_with_hierarchy("A Consumer", "1.2.3", "A Provider") + .create_webhook + .create_triggered_webhook + .create_deprecated_webhook_execution end it "deletes the pact" do diff --git a/spec/lib/pact_broker/pacts/service_spec.rb b/spec/lib/pact_broker/pacts/service_spec.rb index c5b2bf98f..0f9b6ddfe 100644 --- a/spec/lib/pact_broker/pacts/service_spec.rb +++ b/spec/lib/pact_broker/pacts/service_spec.rb @@ -1,10 +1,14 @@ require 'spec_helper' require 'pact_broker/pacts/service' +require 'pact_broker/pacts/pact_params' + module PactBroker module Pacts - module Service + describe Service do + + let(:td) { TestDataBuilder.new } describe "find_distinct_pacts_between" do let(:pact_1) { double('pact 1', json_content: 'content 1')} @@ -61,6 +65,31 @@ module Service end end end + + describe "delete" do + before do + td.create_pact_with_hierarchy + .create_webhook + .create_triggered_webhook + .create_deprecated_webhook_execution + end + + let(:params) do + { + consumer_name: td.consumer.name, + provider_name: td.provider.name, + consumer_version_number: td.consumer_version.number + } + end + + subject { Service.delete PactParams.new(PactBroker::Pacts::PactParams.new(params)) } + + it "deletes the pact" do + expect { subject }.to change { + Pacts::PactPublication.where(id: td.pact.id ).count + }.by(-1) + end + end end end end diff --git a/spec/lib/pact_broker/webhooks/repository_spec.rb b/spec/lib/pact_broker/webhooks/repository_spec.rb index aa355bb68..bc69b9853 100644 --- a/spec/lib/pact_broker/webhooks/repository_spec.rb +++ b/spec/lib/pact_broker/webhooks/repository_spec.rb @@ -505,6 +505,40 @@ module Webhooks expect(TriggeredWebhook.not_run.count).to eq 1 end end + + describe "delete_triggered_webhooks_by_pact_publication_id" do + before do + td.create_pact_with_hierarchy + .create_webhook + .create_triggered_webhook + .create_webhook_execution + .create_pact_with_hierarchy("A Consumer", "1.2.3", "A Provider") + .create_webhook + .create_triggered_webhook + .create_webhook_execution + .create_deprecated_webhook_execution + end + + subject { Repository.new.delete_triggered_webhooks_by_pact_publication_id td.pact.id } + + it "deletes the triggered webhook" do + expect { subject }.to change { + TriggeredWebhook.count + }.by(-1) + end + + it "deletes the webhook_execution" do + expect { subject }.to change { + Execution.exclude(triggered_webhook_id: nil).count + }.by(-1) + end + + it "deletes the deprecated webhook_execution" do + expect { subject }.to change { + Execution.exclude(consumer_id: nil).count + }.by(-1) + end + end end end end diff --git a/spec/support/test_data_builder.rb b/spec/support/test_data_builder.rb index 4bd8468a7..ee92e243f 100644 --- a/spec/support/test_data_builder.rb +++ b/spec/support/test_data_builder.rb @@ -31,6 +31,7 @@ class TestDataBuilder attr_reader :pacticipant attr_reader :consumer attr_reader :provider + attr_reader :consumer_version attr_reader :pact attr_reader :webhook attr_reader :webhook_execution @@ -190,6 +191,7 @@ def create_webhook_execution params = {} def create_deprecated_webhook_execution params = {} create_webhook_execution params Sequel::Model.db[:webhook_executions].where(id: webhook_execution.id).update( + triggered_webhook_id: nil, consumer_id: consumer.id, provider_id: provider.id, webhook_id: PactBroker::Webhooks::Webhook.find(uuid: webhook.uuid).id,