Skip to content

Commit 3dc590c

Browse files
committed
feat(webhook status): delete related triggered webhooks and executions when pact publication is deleted
1 parent 447c4cf commit 3dc590c

File tree

8 files changed

+92
-3
lines changed

8 files changed

+92
-3
lines changed

lib/pact_broker/pacts/service.rb

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def find_by_consumer_version params
3131

3232
def delete params
3333
logger.info "Deleting pact version with params #{params}"
34+
pact = find_pact(params)
35+
webhook_service.delete_all_webhook_related_objects_by_pact_publication_id(pact.id)
3436
pact_repository.delete(params)
3537
end
3638

lib/pact_broker/webhooks/execution.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55

66
module PactBroker
77
module Webhooks
8-
class Execution < Sequel::Model(PactBroker::DB.connection[:webhook_executions].select(Sequel[:webhook_executions][:id], :triggered_webhook_id, :success, :logs))
8+
class Execution < Sequel::Model(
9+
PactBroker::DB.connection[:webhook_executions].select(
10+
Sequel[:webhook_executions][:id],
11+
:triggered_webhook_id,
12+
:success,
13+
:logs,
14+
Sequel[:webhook_executions][:created_at])
15+
)
916

1017
dataset_module do
1118
include PactBroker::Repositories::Helpers

lib/pact_broker/webhooks/repository.rb

+7
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ def unlink_triggered_webhooks_by_webhook_uuid uuid
109109
DeprecatedExecution.where(webhook_id: Webhook.where(uuid: uuid).select(:id)).update(webhook_id: nil)
110110
end
111111

112+
def delete_triggered_webhooks_by_pact_publication_id pact_publication_id
113+
triggered_webhook_ids = TriggeredWebhook.where(pact_publication_id: pact_publication_id).select_for_subquery(:id)
114+
Execution.where(triggered_webhook_id: triggered_webhook_ids).delete
115+
TriggeredWebhook.where(id: triggered_webhook_ids).delete
116+
DeprecatedExecution.where(pact_publication_id: pact_publication_id).delete
117+
end
118+
112119
def find_latest_triggered_webhooks consumer, provider
113120
LatestTriggeredWebhook
114121
.where(consumer: consumer, provider: provider)

lib/pact_broker/webhooks/service.rb

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ def self.delete_all_webhhook_related_objects_by_pacticipant pacticipant
5050
webhook_repository.delete_by_pacticipant pacticipant
5151
end
5252

53+
def self.delete_all_webhook_related_objects_by_pact_publication_id pact_publication_id
54+
webhook_repository.delete_triggered_webhooks_by_pact_publication_id pact_publication_id
55+
end
56+
5357
def self.find_all
5458
webhook_repository.find_all
5559
end

spec/features/delete_pact_spec.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
context "when the pact exists" do
1010
before do
11-
TestDataBuilder.new.create_pact_with_hierarchy("A Consumer", "1.2.3", "A Provider").and_return(:pact)
11+
TestDataBuilder.new
12+
.create_pact_with_hierarchy("A Consumer", "1.2.3", "A Provider")
13+
.create_webhook
14+
.create_triggered_webhook
15+
.create_deprecated_webhook_execution
1216
end
1317

1418
it "deletes the pact" do

spec/lib/pact_broker/pacts/service_spec.rb

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
require 'spec_helper'
22
require 'pact_broker/pacts/service'
3+
require 'pact_broker/pacts/pact_params'
4+
35

46
module PactBroker
57

68
module Pacts
7-
module Service
9+
describe Service do
10+
11+
let(:td) { TestDataBuilder.new }
812

913
describe "find_distinct_pacts_between" do
1014
let(:pact_1) { double('pact 1', json_content: 'content 1')}
@@ -61,6 +65,31 @@ module Service
6165
end
6266
end
6367
end
68+
69+
describe "delete" do
70+
before do
71+
td.create_pact_with_hierarchy
72+
.create_webhook
73+
.create_triggered_webhook
74+
.create_deprecated_webhook_execution
75+
end
76+
77+
let(:params) do
78+
{
79+
consumer_name: td.consumer.name,
80+
provider_name: td.provider.name,
81+
consumer_version_number: td.consumer_version.number
82+
}
83+
end
84+
85+
subject { Service.delete PactParams.new(PactBroker::Pacts::PactParams.new(params)) }
86+
87+
it "deletes the pact" do
88+
expect { subject }.to change {
89+
Pacts::PactPublication.where(id: td.pact.id ).count
90+
}.by(-1)
91+
end
92+
end
6493
end
6594
end
6695
end

spec/lib/pact_broker/webhooks/repository_spec.rb

+34
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,40 @@ module Webhooks
505505
expect(TriggeredWebhook.not_run.count).to eq 1
506506
end
507507
end
508+
509+
describe "delete_triggered_webhooks_by_pact_publication_id" do
510+
before do
511+
td.create_pact_with_hierarchy
512+
.create_webhook
513+
.create_triggered_webhook
514+
.create_webhook_execution
515+
.create_pact_with_hierarchy("A Consumer", "1.2.3", "A Provider")
516+
.create_webhook
517+
.create_triggered_webhook
518+
.create_webhook_execution
519+
.create_deprecated_webhook_execution
520+
end
521+
522+
subject { Repository.new.delete_triggered_webhooks_by_pact_publication_id td.pact.id }
523+
524+
it "deletes the triggered webhook" do
525+
expect { subject }.to change {
526+
TriggeredWebhook.count
527+
}.by(-1)
528+
end
529+
530+
it "deletes the webhook_execution" do
531+
expect { subject }.to change {
532+
Execution.exclude(triggered_webhook_id: nil).count
533+
}.by(-1)
534+
end
535+
536+
it "deletes the deprecated webhook_execution" do
537+
expect { subject }.to change {
538+
Execution.exclude(consumer_id: nil).count
539+
}.by(-1)
540+
end
541+
end
508542
end
509543
end
510544
end

spec/support/test_data_builder.rb

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class TestDataBuilder
3131
attr_reader :pacticipant
3232
attr_reader :consumer
3333
attr_reader :provider
34+
attr_reader :consumer_version
3435
attr_reader :pact
3536
attr_reader :webhook
3637
attr_reader :webhook_execution
@@ -190,6 +191,7 @@ def create_webhook_execution params = {}
190191
def create_deprecated_webhook_execution params = {}
191192
create_webhook_execution params
192193
Sequel::Model.db[:webhook_executions].where(id: webhook_execution.id).update(
194+
triggered_webhook_id: nil,
193195
consumer_id: consumer.id,
194196
provider_id: provider.id,
195197
webhook_id: PactBroker::Webhooks::Webhook.find(uuid: webhook.uuid).id,

0 commit comments

Comments
 (0)