Skip to content

Commit 48f9853

Browse files
committed
fix: delete related triggered webhooks when webhook is deleted
Trying to keep the triggered webhooks to keep the status on the index page after a webhook had been deleted just made things too complicated. Also, it was likely that the webhook was deleted because it was failing, so it just made the failure message hang around.
1 parent f991e15 commit 48f9853

File tree

8 files changed

+157
-15
lines changed

8 files changed

+157
-15
lines changed

db/migrations/39_add_triggered_webhooks_fk_to_execution.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# TODO drop_column(:webhook_executions, :provider_id)
1818

1919
# TODO
20-
# alter_table(:triggered_webhooks) do
20+
# alter_table(:webhook_executions) do
2121
# set_column_not_null(:triggered_webhook_id)
2222
# end
2323
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'securerandom'
2+
3+
Sequel.migration do
4+
up do
5+
from(:triggered_webhooks).where(webhook_id: nil).each do | triggered_webhook |
6+
from(:webhook_executions).where(triggered_webhook_id: triggered_webhook[:id]).delete
7+
from(:triggered_webhooks).where(id: triggered_webhook[:id]).delete
8+
end
9+
10+
from(:webhook_executions).where(webhook_id: nil, triggered_webhook_id: nil).delete
11+
end
12+
13+
# TODO
14+
# alter_table(:triggered_webhooks) do
15+
# set_column_not_null(:webhook_id)
16+
# end
17+
18+
down do
19+
end
20+
end

lib/pact_broker/webhooks/execution.rb

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def <=> other
2727
end
2828
end
2929

30+
# For a brief time, the code was released with a direct relationship between
31+
# webhook and execution. Need to make sure any existing data is handled properly.
3032
class DeprecatedExecution < Sequel::Model(:webhook_executions)
3133
associate(:many_to_one, :provider, :class => "PactBroker::Domain::Pacticipant", :key => :provider_id, :primary_key => :id)
3234
associate(:many_to_one, :consumer, :class => "PactBroker::Domain::Pacticipant", :key => :consumer_id, :primary_key => :id)

lib/pact_broker/webhooks/repository.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,11 @@ def delete_executions_by_pacticipant pacticipants
104104
Execution.where(id: execution_ids).delete
105105
end
106106

107-
def unlink_triggered_webhooks_by_webhook_uuid uuid
108-
TriggeredWebhook.where(webhook: Webhook.where(uuid: uuid)).update(webhook_id: nil)
109-
DeprecatedExecution.where(webhook_id: Webhook.where(uuid: uuid).select(:id)).update(webhook_id: nil)
107+
def delete_triggered_webhooks_by_webhook_uuid uuid
108+
triggered_webhook_ids = TriggeredWebhook.where(webhook: Webhook.where(uuid: uuid)).select_for_subquery(:id)
109+
Execution.where(triggered_webhook_id: triggered_webhook_ids).delete
110+
DeprecatedExecution.where(webhook_id: Webhook.where(uuid: uuid).select_for_subquery(:id)).delete
111+
TriggeredWebhook.where(id: triggered_webhook_ids).delete
110112
end
111113

112114
def delete_triggered_webhooks_by_pact_publication_ids pact_publication_ids

lib/pact_broker/webhooks/service.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def self.update_by_uuid uuid, webhook
4040
end
4141

4242
def self.delete_by_uuid uuid
43-
webhook_repository.unlink_triggered_webhooks_by_webhook_uuid uuid
43+
webhook_repository.delete_triggered_webhooks_by_webhook_uuid uuid
4444
webhook_repository.delete_by_uuid uuid
4545
end
4646

lib/pact_broker/webhooks/webhook.rb

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class Webhook < Sequel::Model
1111
associate(:many_to_one, :consumer, :class => "PactBroker::Domain::Pacticipant", :key => :consumer_id, :primary_key => :id)
1212
one_to_many :headers, :class => "PactBroker::Webhooks::WebhookHeader", :reciprocal => :webhook
1313

14+
dataset_module do
15+
include PactBroker::Repositories::Helpers
16+
end
17+
1418
def before_destroy
1519
WebhookHeader.where(webhook_id: id).destroy
1620
end

spec/lib/pact_broker/webhooks/repository_spec.rb

+26-10
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ module Webhooks
354354
end
355355
end
356356

357-
describe "unlink_triggered_webhooks_by_webhook_uuid" do
357+
describe "delete_triggered_webhooks_by_webhook_uuid" do
358358
let(:td) { TestDataBuilder.new }
359359

360360
before do
@@ -365,22 +365,38 @@ module Webhooks
365365
.create_webhook
366366
.create_triggered_webhook
367367
.create_deprecated_webhook_execution
368+
.create_webhook_execution
369+
.create_webhook
370+
.create_triggered_webhook
371+
.create_deprecated_webhook_execution
372+
.create_webhook_execution
373+
end
374+
375+
let(:webhook_id) { Webhook.find(uuid: td.webhook.uuid).id }
376+
subject { Repository.new.delete_triggered_webhooks_by_webhook_uuid td.webhook.uuid }
377+
378+
it "deletes the related triggered webhooks" do
379+
expect { subject }.to change {
380+
TriggeredWebhook.where(id: td.triggered_webhook.id).count
381+
}.from(1).to(0)
368382
end
369383

370-
subject { Repository.new.unlink_triggered_webhooks_by_webhook_uuid td.webhook.uuid }
384+
it "does not delete the unrelated triggered webhooks" do
385+
expect { subject }.to_not change {
386+
TriggeredWebhook.exclude(id: td.triggered_webhook.id).count
387+
}
388+
end
371389

372-
it "sets the webhook id to nil" do
373-
webhook_id = Webhook.find(uuid: td.webhook.uuid).id
390+
it "deletes the related deprecated webhook executions" do
374391
expect { subject }.to change {
375-
TriggeredWebhook.find(id: td.triggered_webhook.id).webhook_id
376-
}.from(webhook_id).to(nil)
392+
DeprecatedExecution.count
393+
}.by(-2)
377394
end
378395

379-
it "sets the webhook id to nil for the deprecated webhook execution field" do
380-
webhook_id = Webhook.find(uuid: td.webhook.uuid).id
396+
it "deletes the related webhook executions" do
381397
expect { subject }.to change {
382-
DeprecatedExecution.find(id: td.webhook_execution.id).webhook_id
383-
}.from(webhook_id).to(nil)
398+
Execution.count
399+
}.by(-2)
384400
end
385401
end
386402

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
describe 'creating triggered webhooks from webhook executions (migrate 36-41)', migration: true do
2+
before do
3+
PactBroker::Database.migrate(41)
4+
end
5+
6+
let(:before_now) { DateTime.new(2016, 1, 1) }
7+
let(:now) { DateTime.new(2018, 2, 2) }
8+
let!(:consumer) { create(:pacticipants, {name: 'Consumer', created_at: now, updated_at: now}) }
9+
let!(:provider) { create(:pacticipants, {name: 'Provider', created_at: now, updated_at: now}) }
10+
let!(:consumer_version) { create(:versions, {number: '1.2.3', order: 1, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) }
11+
let!(:pact_version) { create(:pact_versions, {content: {some: 'json'}.to_json, sha: '1234', consumer_id: consumer[:id], provider_id: provider[:id], created_at: now}) }
12+
let!(:pact_publication) do
13+
create(:pact_publications, {
14+
consumer_version_id: consumer_version[:id],
15+
provider_id: provider[:id],
16+
revision_number: 1,
17+
pact_version_id: pact_version[:id],
18+
created_at: (now - 1)
19+
})
20+
end
21+
let!(:webhook) do
22+
create(:webhooks, {
23+
uuid: '1234',
24+
method: 'GET',
25+
url: 'http://www.example.org',
26+
consumer_id: consumer[:id],
27+
provider_id: provider[:id],
28+
is_json_request_body: false,
29+
created_at: now
30+
})
31+
end
32+
let!(:triggered_webhook) do
33+
create(:triggered_webhooks, {
34+
trigger_uuid: '12345',
35+
trigger_type: 'publication',
36+
pact_publication_id: pact_publication[:id],
37+
webhook_id: webhook[:id],
38+
webhook_uuid: webhook[:uuid],
39+
consumer_id: consumer[:id],
40+
provider_id: provider[:id],
41+
status: 'success',
42+
created_at: now,
43+
updated_at: now
44+
})
45+
end
46+
let!(:webhook_execution) do
47+
create(:webhook_executions, {
48+
triggered_webhook_id: triggered_webhook[:id],
49+
success: true,
50+
logs: 'logs',
51+
created_at: now
52+
})
53+
end
54+
55+
let!(:orphan_triggered_webhook) do
56+
create(:triggered_webhooks, {
57+
trigger_uuid: '12345',
58+
trigger_type: 'publication',
59+
pact_publication_id: pact_publication[:id],
60+
webhook_id: nil,
61+
webhook_uuid: webhook[:uuid],
62+
consumer_id: consumer[:id],
63+
provider_id: provider[:id],
64+
status: 'success',
65+
created_at: now,
66+
updated_at: now
67+
})
68+
end
69+
70+
let!(:orphan_webhook_execution) do
71+
create(:webhook_executions, {
72+
triggered_webhook_id: orphan_triggered_webhook[:id],
73+
success: true,
74+
logs: 'logs',
75+
created_at: now
76+
})
77+
end
78+
79+
let!(:deprecated_orphan_webhook_execution) do
80+
create(:webhook_executions, {
81+
triggered_webhook_id: nil,
82+
webhook_id: nil,
83+
success: true,
84+
logs: 'logs',
85+
created_at: now
86+
})
87+
end
88+
89+
subject { PactBroker::Database.migrate(42) }
90+
91+
it "deletes the orphan triggered webhooks" do
92+
expect { subject }.to change { database[:triggered_webhooks].count }.by(-1)
93+
end
94+
95+
it "deletes the orphan webhook executions" do
96+
expect { subject }.to change { database[:webhook_executions].count }.by(-2)
97+
end
98+
end

0 commit comments

Comments
 (0)