Skip to content

Commit

Permalink
fix: delete triggered webhook data when deleting pacticipant version
Browse files Browse the repository at this point in the history
fixes: #300
  • Loading branch information
bethesque committed Sep 2, 2019
1 parent e92bde5 commit 77265c3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/pact_broker/versions/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/pact_broker/webhooks/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/lib/pact_broker/versions/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 35 additions & 0 deletions spec/lib/pact_broker/webhooks/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 77265c3

Please sign in to comment.