From 6735da326e6dd7b91904c07c6d6c2cae4580c066 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 5 Dec 2019 14:11:29 +1100 Subject: [PATCH] feat(webhooks): allow webhooks to be triggered for verification successes and failures Closes: https://github.com/pact-foundation/pact_broker/issues/314 --- lib/pact_broker/doc/views/webhooks.markdown | 6 +++- lib/pact_broker/domain/webhook.rb | 8 +++++ lib/pact_broker/verifications/service.rb | 4 +-- lib/pact_broker/webhooks/trigger_service.rb | 25 ++++++++++++++ lib/pact_broker/webhooks/webhook_event.rb | 13 ++++++-- .../pact_broker/verifications/service_spec.rb | 6 ++-- .../webhooks/trigger_service_spec.rb | 33 +++++++++++++++++++ 7 files changed, 86 insertions(+), 9 deletions(-) diff --git a/lib/pact_broker/doc/views/webhooks.markdown b/lib/pact_broker/doc/views/webhooks.markdown index 7a991d313..18d0f256e 100644 --- a/lib/pact_broker/doc/views/webhooks.markdown +++ b/lib/pact_broker/doc/views/webhooks.markdown @@ -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 diff --git a/lib/pact_broker/domain/webhook.rb b/lib/pact_broker/domain/webhook.rb index dca15f784..8c8c9176b 100644 --- a/lib/pact_broker/domain/webhook.rb +++ b/lib/pact_broker/domain/webhook.rb @@ -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) diff --git a/lib/pact_broker/verifications/service.rb b/lib/pact_broker/verifications/service.rb index 0dedf4e84..9fce09fbc 100644 --- a/lib/pact_broker/verifications/service.rb +++ b/lib/pact_broker/verifications/service.rb @@ -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 diff --git a/lib/pact_broker/webhooks/trigger_service.rb b/lib/pact_broker/webhooks/trigger_service.rb index c16c9f9bf..cdbdcd1ae 100644 --- a/lib/pact_broker/webhooks/trigger_service.rb +++ b/lib/pact_broker/webhooks/trigger_service.rb @@ -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 diff --git a/lib/pact_broker/webhooks/webhook_event.rb b/lib/pact_broker/webhooks/webhook_event.rb index 661adc923..d9fb1f688 100644 --- a/lib/pact_broker/webhooks/webhook_event.rb +++ b/lib/pact_broker/webhooks/webhook_event.rb @@ -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 @@ -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 diff --git a/spec/lib/pact_broker/verifications/service_spec.rb b/spec/lib/pact_broker/verifications/service_spec.rb index be3586698..3d631dfd2 100644 --- a/spec/lib/pact_broker/verifications/service_spec.rb +++ b/spec/lib/pact_broker/verifications/service_spec.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/lib/pact_broker/webhooks/trigger_service_spec.rb b/spec/lib/pact_broker/webhooks/trigger_service_spec.rb index 1908545a2..a498838d4 100644 --- a/spec/lib/pact_broker/webhooks/trigger_service_spec.rb +++ b/spec/lib/pact_broker/webhooks/trigger_service_spec.rb @@ -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