Skip to content

Commit

Permalink
feat(webhook status): delete related triggered webhooks and execution…
Browse files Browse the repository at this point in the history
…s when pact publication is deleted
  • Loading branch information
bethesque committed Sep 18, 2017
1 parent 447c4cf commit 3dc590c
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/pact_broker/pacts/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def find_by_consumer_version params

def delete params
logger.info "Deleting pact version with params #{params}"
pact = find_pact(params)
webhook_service.delete_all_webhook_related_objects_by_pact_publication_id(pact.id)
pact_repository.delete(params)
end

Expand Down
9 changes: 8 additions & 1 deletion lib/pact_broker/webhooks/execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@

module PactBroker
module Webhooks
class Execution < Sequel::Model(PactBroker::DB.connection[:webhook_executions].select(Sequel[:webhook_executions][:id], :triggered_webhook_id, :success, :logs))
class Execution < Sequel::Model(
PactBroker::DB.connection[:webhook_executions].select(
Sequel[:webhook_executions][:id],
:triggered_webhook_id,
:success,
:logs,
Sequel[:webhook_executions][:created_at])
)

dataset_module do
include PactBroker::Repositories::Helpers
Expand Down
7 changes: 7 additions & 0 deletions lib/pact_broker/webhooks/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ def unlink_triggered_webhooks_by_webhook_uuid uuid
DeprecatedExecution.where(webhook_id: Webhook.where(uuid: uuid).select(:id)).update(webhook_id: nil)
end

def delete_triggered_webhooks_by_pact_publication_id pact_publication_id
triggered_webhook_ids = TriggeredWebhook.where(pact_publication_id: pact_publication_id).select_for_subquery(:id)
Execution.where(triggered_webhook_id: triggered_webhook_ids).delete
TriggeredWebhook.where(id: triggered_webhook_ids).delete
DeprecatedExecution.where(pact_publication_id: pact_publication_id).delete
end

def find_latest_triggered_webhooks consumer, provider
LatestTriggeredWebhook
.where(consumer: consumer, provider: provider)
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/webhooks/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def self.delete_all_webhhook_related_objects_by_pacticipant pacticipant
webhook_repository.delete_by_pacticipant pacticipant
end

def self.delete_all_webhook_related_objects_by_pact_publication_id pact_publication_id
webhook_repository.delete_triggered_webhooks_by_pact_publication_id pact_publication_id
end

def self.find_all
webhook_repository.find_all
end
Expand Down
6 changes: 5 additions & 1 deletion spec/features/delete_pact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

context "when the pact exists" do
before do
TestDataBuilder.new.create_pact_with_hierarchy("A Consumer", "1.2.3", "A Provider").and_return(:pact)
TestDataBuilder.new
.create_pact_with_hierarchy("A Consumer", "1.2.3", "A Provider")
.create_webhook
.create_triggered_webhook
.create_deprecated_webhook_execution
end

it "deletes the pact" do
Expand Down
31 changes: 30 additions & 1 deletion spec/lib/pact_broker/pacts/service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
require 'spec_helper'
require 'pact_broker/pacts/service'
require 'pact_broker/pacts/pact_params'


module PactBroker

module Pacts
module Service
describe Service do

let(:td) { TestDataBuilder.new }

describe "find_distinct_pacts_between" do
let(:pact_1) { double('pact 1', json_content: 'content 1')}
Expand Down Expand Up @@ -61,6 +65,31 @@ module Service
end
end
end

describe "delete" do
before do
td.create_pact_with_hierarchy
.create_webhook
.create_triggered_webhook
.create_deprecated_webhook_execution
end

let(:params) do
{
consumer_name: td.consumer.name,
provider_name: td.provider.name,
consumer_version_number: td.consumer_version.number
}
end

subject { Service.delete PactParams.new(PactBroker::Pacts::PactParams.new(params)) }

it "deletes the pact" do
expect { subject }.to change {
Pacts::PactPublication.where(id: td.pact.id ).count
}.by(-1)
end
end
end
end
end
34 changes: 34 additions & 0 deletions spec/lib/pact_broker/webhooks/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,40 @@ module Webhooks
expect(TriggeredWebhook.not_run.count).to eq 1
end
end

describe "delete_triggered_webhooks_by_pact_publication_id" do
before do
td.create_pact_with_hierarchy
.create_webhook
.create_triggered_webhook
.create_webhook_execution
.create_pact_with_hierarchy("A Consumer", "1.2.3", "A Provider")
.create_webhook
.create_triggered_webhook
.create_webhook_execution
.create_deprecated_webhook_execution
end

subject { Repository.new.delete_triggered_webhooks_by_pact_publication_id td.pact.id }

it "deletes the triggered webhook" do
expect { subject }.to change {
TriggeredWebhook.count
}.by(-1)
end

it "deletes the webhook_execution" do
expect { subject }.to change {
Execution.exclude(triggered_webhook_id: nil).count
}.by(-1)
end

it "deletes the deprecated webhook_execution" do
expect { subject }.to change {
Execution.exclude(consumer_id: nil).count
}.by(-1)
end
end
end
end
end
2 changes: 2 additions & 0 deletions spec/support/test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class TestDataBuilder
attr_reader :pacticipant
attr_reader :consumer
attr_reader :provider
attr_reader :consumer_version
attr_reader :pact
attr_reader :webhook
attr_reader :webhook_execution
Expand Down Expand Up @@ -190,6 +191,7 @@ def create_webhook_execution params = {}
def create_deprecated_webhook_execution params = {}
create_webhook_execution params
Sequel::Model.db[:webhook_executions].where(id: webhook_execution.id).update(
triggered_webhook_id: nil,
consumer_id: consumer.id,
provider_id: provider.id,
webhook_id: PactBroker::Webhooks::Webhook.find(uuid: webhook.uuid).id,
Expand Down

0 comments on commit 3dc590c

Please sign in to comment.