Skip to content

Commit

Permalink
feat(webhooks): allow webhooks to be triggered for verification succe…
Browse files Browse the repository at this point in the history
…sses and failures

Closes: #314
  • Loading branch information
bethesque committed Dec 5, 2019
1 parent a7c2518 commit 6735da3
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 9 deletions.
6 changes: 5 additions & 1 deletion lib/pact_broker/doc/views/webhooks.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ To specify an XML body, you will need to use a correctly escaped string (or use

`contract_content_changed:` triggered when the content of the contract, or tags applied to the contract have changed since the previous publication. If `base_equality_only_on_content_that_affects_verification_results` is set to `true` in the configuration (the default), any changes to whitespace, ordering of keys, or the ordering of the `interactions` or `messages` will be ignored, and will not trigger this event. It is recommended to trigger a provider verification build for this event.

`provider_verification_published:` triggered whenever a provider publishes a verification.
`provider_verification_published:` triggered whenever a provider publishes a verification result.

`provider_verification_succeeded:` triggered whenever a provider publishes a successful verification result.

`provider_verification_failed:` triggered whenever a provider publishes a failed verification result.

### Dynamic variable substitution

Expand Down
8 changes: 8 additions & 0 deletions lib/pact_broker/domain/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ def trigger_on_provider_verification_published?
events.any?(&:provider_verification_published?)
end

def trigger_on_provider_verification_succeeded?
events.any?(&:provider_verification_succeeded?)
end

def trigger_on_provider_verification_failed?
events.any?(&:provider_verification_failed?)
end

private

def execute_request(webhook_request)
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/verifications/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def create next_verification_number, params, pact, webhook_options
execution_configuration = webhook_options[:webhook_execution_configuration]
.with_webhook_context(provider_version_tags: verification.provider_version_tag_names)

webhook_service.trigger_webhooks(pact,
webhook_trigger_service.trigger_webhooks_for_verification_results_publication(
pact,
verification,
PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED,
webhook_options.deep_merge(webhook_execution_configuration: execution_configuration)
)
verification
Expand Down
25 changes: 25 additions & 0 deletions lib/pact_broker/webhooks/trigger_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ def trigger_webhooks_for_updated_pact(existing_pact, updated_pact, webhook_optio
end
end

def trigger_webhooks_for_verification_results_publication(pact, verification, webhook_options)
if verification.success
webhook_service.trigger_webhooks(
pact,
verification,
PactBroker::Webhooks::WebhookEvent::VERIFICATION_SUCCEEDED,
webhook_options
)
else
webhook_service.trigger_webhooks(
pact,
verification,
PactBroker::Webhooks::WebhookEvent::VERIFICATION_FAILED,
webhook_options
)
end

webhook_service.trigger_webhooks(
pact,
verification,
PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED,
webhook_options
)
end

private

def pact_is_new_or_newly_tagged_or_pact_has_changed_since_previous_version? pact
Expand Down
13 changes: 10 additions & 3 deletions lib/pact_broker/webhooks/webhook_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class WebhookEvent < Sequel::Model
CONTRACT_PUBLISHED = 'contract_published'
CONTRACT_CONTENT_CHANGED = 'contract_content_changed'
VERIFICATION_PUBLISHED = 'provider_verification_published'
VERIFICATION_SUCCEEDED = 'provider_verification_succeeded'
VERIFICATION_FAILED = 'provider_verification_failed'
DEFAULT_EVENT_NAME = CONTRACT_CONTENT_CHANGED
#CONTRACT_VERIFIABLE_CONTENT_CHANGED = 'contract_verifiable_content_changed'
#VERIFICATION_STATUS_CHANGED = 'verification_status_changed'

EVENT_NAMES = [CONTRACT_PUBLISHED, CONTRACT_CONTENT_CHANGED, VERIFICATION_PUBLISHED]
EVENT_NAMES = [CONTRACT_PUBLISHED, CONTRACT_CONTENT_CHANGED, VERIFICATION_PUBLISHED, VERIFICATION_FAILED]

dataset_module do
include PactBroker::Repositories::Helpers
Expand All @@ -30,6 +30,13 @@ def provider_verification_published?
name == VERIFICATION_PUBLISHED
end

def provider_verification_succeeded?
name == VERIFICATION_SUCCEEDED
end

def provider_verification_failed?
name == VERIFICATION_FAILED
end
end

WebhookEvent.plugin :timestamps, update_on_create: true
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/pact_broker/verifications/service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'pact_broker/verifications/service'
require 'pact_broker/verifications/repository'
require 'pact_broker/webhooks/execution_configuration'
require 'pact_broker/webhooks/trigger_service'

module PactBroker

Expand All @@ -16,7 +17,7 @@ module Verifications

describe "#create" do
before do
allow(PactBroker::Webhooks::Service).to receive(:trigger_webhooks)
allow(PactBroker::Webhooks::TriggerService).to receive(:trigger_webhooks_for_verification_results_publication)
allow(webhook_execution_configuration).to receive(:with_webhook_context).and_return(webhook_execution_configuration)
end

Expand Down Expand Up @@ -61,10 +62,9 @@ module Verifications

it "invokes the webhooks for the verification" do
verification = create_verification
expect(PactBroker::Webhooks::Service).to have_received(:trigger_webhooks).with(
expect(PactBroker::Webhooks::TriggerService).to have_received(:trigger_webhooks_for_verification_results_publication).with(
pact,
verification,
PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED,
options
)
end
Expand Down
33 changes: 33 additions & 0 deletions spec/lib/pact_broker/webhooks/trigger_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,39 @@ module Webhooks
include_examples "not triggering a contract_content_changed event"
end
end

describe "#trigger_webhooks_for_verification_results_publication" do
let(:verification) { double("verification", success: success) }
let(:success) { true }

subject { TriggerService.trigger_webhooks_for_verification_results_publication(pact, verification, webhook_options) }

context "when the verification is successful" do
it "triggers a provider_verification_succeeded webhook" do
expect(webhook_service).to receive(:trigger_webhooks).with(pact, verification, PactBroker::Webhooks::WebhookEvent::VERIFICATION_SUCCEEDED, webhook_options)
subject
end

it "triggers a provider_verification_published webhook" do
expect(webhook_service).to receive(:trigger_webhooks).with(pact, verification, PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED, webhook_options)
subject
end
end

context "when the verification is not successful" do
let(:success) { false }

it "triggers a provider_verification_failed webhook" do
expect(webhook_service).to receive(:trigger_webhooks).with(pact, verification, PactBroker::Webhooks::WebhookEvent::VERIFICATION_FAILED, webhook_options)
subject
end

it "triggeres a provider_verification_published webhook" do
expect(webhook_service).to receive(:trigger_webhooks).with(pact, verification, PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED, webhook_options)
subject
end
end
end
end
end
end

0 comments on commit 6735da3

Please sign in to comment.