Skip to content

Commit 6735da3

Browse files
committed
feat(webhooks): allow webhooks to be triggered for verification successes and failures
Closes: pact-foundation#314
1 parent a7c2518 commit 6735da3

File tree

7 files changed

+86
-9
lines changed

7 files changed

+86
-9
lines changed

lib/pact_broker/doc/views/webhooks.markdown

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ To specify an XML body, you will need to use a correctly escaped string (or use
8080

8181
`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.
8282

83-
`provider_verification_published:` triggered whenever a provider publishes a verification.
83+
`provider_verification_published:` triggered whenever a provider publishes a verification result.
84+
85+
`provider_verification_succeeded:` triggered whenever a provider publishes a successful verification result.
86+
87+
`provider_verification_failed:` triggered whenever a provider publishes a failed verification result.
8488

8589
### Dynamic variable substitution
8690

lib/pact_broker/domain/webhook.rb

+8
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ def trigger_on_provider_verification_published?
8585
events.any?(&:provider_verification_published?)
8686
end
8787

88+
def trigger_on_provider_verification_succeeded?
89+
events.any?(&:provider_verification_succeeded?)
90+
end
91+
92+
def trigger_on_provider_verification_failed?
93+
events.any?(&:provider_verification_failed?)
94+
end
95+
8896
private
8997

9098
def execute_request(webhook_request)

lib/pact_broker/verifications/service.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def create next_verification_number, params, pact, webhook_options
3131
execution_configuration = webhook_options[:webhook_execution_configuration]
3232
.with_webhook_context(provider_version_tags: verification.provider_version_tag_names)
3333

34-
webhook_service.trigger_webhooks(pact,
34+
webhook_trigger_service.trigger_webhooks_for_verification_results_publication(
35+
pact,
3536
verification,
36-
PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED,
3737
webhook_options.deep_merge(webhook_execution_configuration: execution_configuration)
3838
)
3939
verification

lib/pact_broker/webhooks/trigger_service.rb

+25
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ def trigger_webhooks_for_updated_pact(existing_pact, updated_pact, webhook_optio
2929
end
3030
end
3131

32+
def trigger_webhooks_for_verification_results_publication(pact, verification, webhook_options)
33+
if verification.success
34+
webhook_service.trigger_webhooks(
35+
pact,
36+
verification,
37+
PactBroker::Webhooks::WebhookEvent::VERIFICATION_SUCCEEDED,
38+
webhook_options
39+
)
40+
else
41+
webhook_service.trigger_webhooks(
42+
pact,
43+
verification,
44+
PactBroker::Webhooks::WebhookEvent::VERIFICATION_FAILED,
45+
webhook_options
46+
)
47+
end
48+
49+
webhook_service.trigger_webhooks(
50+
pact,
51+
verification,
52+
PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED,
53+
webhook_options
54+
)
55+
end
56+
3257
private
3358

3459
def pact_is_new_or_newly_tagged_or_pact_has_changed_since_previous_version? pact

lib/pact_broker/webhooks/webhook_event.rb

+10-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ class WebhookEvent < Sequel::Model
88
CONTRACT_PUBLISHED = 'contract_published'
99
CONTRACT_CONTENT_CHANGED = 'contract_content_changed'
1010
VERIFICATION_PUBLISHED = 'provider_verification_published'
11+
VERIFICATION_SUCCEEDED = 'provider_verification_succeeded'
12+
VERIFICATION_FAILED = 'provider_verification_failed'
1113
DEFAULT_EVENT_NAME = CONTRACT_CONTENT_CHANGED
12-
#CONTRACT_VERIFIABLE_CONTENT_CHANGED = 'contract_verifiable_content_changed'
13-
#VERIFICATION_STATUS_CHANGED = 'verification_status_changed'
1414

15-
EVENT_NAMES = [CONTRACT_PUBLISHED, CONTRACT_CONTENT_CHANGED, VERIFICATION_PUBLISHED]
15+
EVENT_NAMES = [CONTRACT_PUBLISHED, CONTRACT_CONTENT_CHANGED, VERIFICATION_PUBLISHED, VERIFICATION_FAILED]
1616

1717
dataset_module do
1818
include PactBroker::Repositories::Helpers
@@ -30,6 +30,13 @@ def provider_verification_published?
3030
name == VERIFICATION_PUBLISHED
3131
end
3232

33+
def provider_verification_succeeded?
34+
name == VERIFICATION_SUCCEEDED
35+
end
36+
37+
def provider_verification_failed?
38+
name == VERIFICATION_FAILED
39+
end
3340
end
3441

3542
WebhookEvent.plugin :timestamps, update_on_create: true

spec/lib/pact_broker/verifications/service_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'pact_broker/verifications/service'
22
require 'pact_broker/verifications/repository'
33
require 'pact_broker/webhooks/execution_configuration'
4+
require 'pact_broker/webhooks/trigger_service'
45

56
module PactBroker
67

@@ -16,7 +17,7 @@ module Verifications
1617

1718
describe "#create" do
1819
before do
19-
allow(PactBroker::Webhooks::Service).to receive(:trigger_webhooks)
20+
allow(PactBroker::Webhooks::TriggerService).to receive(:trigger_webhooks_for_verification_results_publication)
2021
allow(webhook_execution_configuration).to receive(:with_webhook_context).and_return(webhook_execution_configuration)
2122
end
2223

@@ -61,10 +62,9 @@ module Verifications
6162

6263
it "invokes the webhooks for the verification" do
6364
verification = create_verification
64-
expect(PactBroker::Webhooks::Service).to have_received(:trigger_webhooks).with(
65+
expect(PactBroker::Webhooks::TriggerService).to have_received(:trigger_webhooks_for_verification_results_publication).with(
6566
pact,
6667
verification,
67-
PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED,
6868
options
6969
)
7070
end

spec/lib/pact_broker/webhooks/trigger_service_spec.rb

+33
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,39 @@ module Webhooks
136136
include_examples "not triggering a contract_content_changed event"
137137
end
138138
end
139+
140+
describe "#trigger_webhooks_for_verification_results_publication" do
141+
let(:verification) { double("verification", success: success) }
142+
let(:success) { true }
143+
144+
subject { TriggerService.trigger_webhooks_for_verification_results_publication(pact, verification, webhook_options) }
145+
146+
context "when the verification is successful" do
147+
it "triggers a provider_verification_succeeded webhook" do
148+
expect(webhook_service).to receive(:trigger_webhooks).with(pact, verification, PactBroker::Webhooks::WebhookEvent::VERIFICATION_SUCCEEDED, webhook_options)
149+
subject
150+
end
151+
152+
it "triggers a provider_verification_published webhook" do
153+
expect(webhook_service).to receive(:trigger_webhooks).with(pact, verification, PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED, webhook_options)
154+
subject
155+
end
156+
end
157+
158+
context "when the verification is not successful" do
159+
let(:success) { false }
160+
161+
it "triggers a provider_verification_failed webhook" do
162+
expect(webhook_service).to receive(:trigger_webhooks).with(pact, verification, PactBroker::Webhooks::WebhookEvent::VERIFICATION_FAILED, webhook_options)
163+
subject
164+
end
165+
166+
it "triggeres a provider_verification_published webhook" do
167+
expect(webhook_service).to receive(:trigger_webhooks).with(pact, verification, PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED, webhook_options)
168+
subject
169+
end
170+
end
171+
end
139172
end
140173
end
141174
end

0 commit comments

Comments
 (0)