Skip to content

Commit

Permalink
feat: keep the triggered webhooks after the webhook has been deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Apr 29, 2021
1 parent a1ee967 commit a5ab2a3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
12 changes: 7 additions & 5 deletions lib/pact_broker/api/decorators/triggered_webhook_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion lib/pact_broker/webhooks/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/pact_broker/webhooks/triggered_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions lib/pact_broker/webhooks/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/features/delete_webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a5ab2a3

Please sign in to comment.