diff --git a/lib/pact_broker/api/decorators/triggered_webhook_decorator.rb b/lib/pact_broker/api/decorators/triggered_webhook_decorator.rb index f5f6a7e90..cb4312f4d 100644 --- a/lib/pact_broker/api/decorators/triggered_webhook_decorator.rb +++ b/lib/pact_broker/api/decorators/triggered_webhook_decorator.rb @@ -30,11 +30,13 @@ class TriggeredWebhookDecorator < BaseDecorator end link :'pb:webhook' do | context | - { - href: webhook_url(represented.webhook_uuid, context[:base_url]), - title: "Webhook", - name: represented.request_description - } + if represented.webhook + { + href: webhook_url(represented.webhook_uuid, context[:base_url]), + title: "Webhook", + name: represented.request_description + } + end end end end diff --git a/lib/pact_broker/webhooks/service.rb b/lib/pact_broker/webhooks/service.rb index 18065e159..4bc6733a0 100644 --- a/lib/pact_broker/webhooks/service.rb +++ b/lib/pact_broker/webhooks/service.rb @@ -73,7 +73,6 @@ def update_by_uuid uuid, params end def delete_by_uuid uuid - webhook_repository.delete_triggered_webhooks_by_webhook_uuid uuid webhook_repository.delete_by_uuid uuid end diff --git a/lib/pact_broker/webhooks/triggered_webhook.rb b/lib/pact_broker/webhooks/triggered_webhook.rb index 42d1c5dd8..9e15e9edc 100644 --- a/lib/pact_broker/webhooks/triggered_webhook.rb +++ b/lib/pact_broker/webhooks/triggered_webhook.rb @@ -55,7 +55,8 @@ def not_run associate(:many_to_one, :consumer, :class => "PactBroker::Domain::Pacticipant", :key => :consumer_id, :primary_key => :id) def request_description - webhook.to_domain.request_description + # webhook could be deleted + webhook&.to_domain&.request_description end def execute options diff --git a/lib/pact_broker/webhooks/webhook.rb b/lib/pact_broker/webhooks/webhook.rb index 176f4f4c3..b4439fada 100644 --- a/lib/pact_broker/webhooks/webhook.rb +++ b/lib/pact_broker/webhooks/webhook.rb @@ -17,6 +17,13 @@ class Webhook < Sequel::Model dataset_module do include PactBroker::Repositories::Helpers + # Keep the triggered webhooks after the webhook has been deleted + def delete + require 'pact_broker/webhooks/triggered_webhook' + TriggeredWebhook.where(webhook_id: id).update(webhook_id: nil) + super + end + def for_event_name(event_name) join(:webhook_events, { webhook_id: :id }) .where(Sequel[:webhook_events][:name] => event_name) @@ -96,6 +103,13 @@ def is_for? integration (consumer_id == integration.consumer_id || !consumer_id) && (provider_id == integration.provider_id || !provider_id) end + # Keep the triggered webhooks after the webhook has been deleted + def delete + require 'pact_broker/webhooks/triggered_webhook' + TriggeredWebhook.where(webhook_id: id).update(webhook_id: nil) + super + end + private def self.properties_hash_from_domain webhook diff --git a/spec/features/delete_webhook_spec.rb b/spec/features/delete_webhook_spec.rb index 68b5d28bf..e31928ba6 100644 --- a/spec/features/delete_webhook_spec.rb +++ b/spec/features/delete_webhook_spec.rb @@ -27,4 +27,8 @@ subject expect(last_response.status).to eq 204 end + + it "does not delete the triggered webhooks because these are needed to calculate the webhook status" do + expect { subject }.to_not change { PactBroker::Webhooks::TriggeredWebhook.count } + end end