Skip to content

Commit

Permalink
feat(webhook status): delete webhook objects related to previous revi…
Browse files Browse the repository at this point in the history
…sions of a pact when deleting a pact publication
  • Loading branch information
bethesque committed Sep 18, 2017
1 parent 3dc590c commit a053623
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
8 changes: 8 additions & 0 deletions lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ def find_pact consumer_name, consumer_version, provider_name, pact_version_sha =
query.limit(1).collect(&:to_domain_with_content)[0]
end

def find_all_revisions consumer_name, consumer_version, provider_name
AllPactPublications
.consumer(consumer_name)
.provider(provider_name)
.consumer_version_number(consumer_version)
.order(:consumer_version_order, :revision_number).collect(&:to_domain_with_content)
end

def find_previous_pact pact
LatestPactPublicationsByConsumerVersion
.eager(:tags)
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/pacts/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +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)
pacts = pact_repository.find_all_revisions(params[:consumer_name], params[:consumer_version_number], params[:provider_name])
webhook_service.delete_all_webhook_related_objects_by_pact_publication_ids(pacts.collect(&:id))
pact_repository.delete(params)
end

Expand Down
6 changes: 3 additions & 3 deletions lib/pact_broker/webhooks/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ 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)
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
TriggeredWebhook.where(id: triggered_webhook_ids).delete
DeprecatedExecution.where(pact_publication_id: pact_publication_id).delete
DeprecatedExecution.where(pact_publication_id: pact_publication_ids).delete
end

def find_latest_triggered_webhooks consumer, provider
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/webhooks/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ 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
def self.delete_all_webhook_related_objects_by_pact_publication_ids pact_publication_ids
webhook_repository.delete_triggered_webhooks_by_pact_publication_ids pact_publication_ids
end

def self.find_all
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/pact_broker/pacts/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,27 @@ module Pacts
end
end

describe "find_all_revisions" do
before do
TestDataBuilder.new
.create_pact_with_hierarchy("foo", "3.0.0", "bar")
.revise_pact
.create_pact_with_hierarchy(consumer_name, "1.2.3", provider_name)
.revise_pact
.create_consumer_version("4.5.6")
.create_pact
end

subject { Repository.new.find_all_revisions consumer_name, "1.2.3", provider_name }

it "returns all the revisions for the given pact version" do
expect(subject.size).to eq 2
expect(subject.first.consumer_name).to eq consumer_name
expect(subject.first.revision_number).to eq 1
expect(subject.last.revision_number).to eq 2
end
end

describe "find_previous_pact" do
before do
TestDataBuilder.new
Expand Down
2 changes: 2 additions & 0 deletions spec/lib/pact_broker/pacts/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ module Pacts
td.create_pact_with_hierarchy
.create_webhook
.create_triggered_webhook
.create_webhook_execution
.create_deprecated_webhook_execution
.revise_pact
end

let(:params) do
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/webhooks/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ module Webhooks
.create_deprecated_webhook_execution
end

subject { Repository.new.delete_triggered_webhooks_by_pact_publication_id td.pact.id }
subject { Repository.new.delete_triggered_webhooks_by_pact_publication_ids [td.pact.id] }

it "deletes the triggered webhook" do
expect { subject }.to change {
Expand Down

0 comments on commit a053623

Please sign in to comment.