Skip to content

Commit 648e1c3

Browse files
committed
feat(webhook status): display attempts made and attempts remaining in webhook status resource
1 parent f2d92f3 commit 648e1c3

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

lib/pact_broker/api/decorators/pact_webhooks_status_decorator.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ module Decorators
88

99
class TriggeredWebhookDecorator < BaseDecorator
1010
property :status
11+
property :number_of_attempts_made, as: :attemptsMade
12+
property :number_of_attempts_remaining, as: :attemptsRemaining
1113
property :trigger_type, as: :triggerType
1214

1315
property :created_at, as: :triggeredAt
1416

15-
16-
1717
link :logs do | context |
1818
{
1919
href: triggered_webhook_logs_url(represented, context[:base_url]),

lib/pact_broker/webhooks/triggered_webhook.rb

+16
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ def retrying?
5555
def not_run?
5656
status == STATUS_NOT_RUN
5757
end
58+
59+
def number_of_attempts_made
60+
webhook_executions.size
61+
end
62+
63+
def finished?
64+
success? || failure?
65+
end
66+
67+
def number_of_attempts_remaining
68+
if finished?
69+
0
70+
else
71+
(PactBroker.configuration.webhook_retry_schedule.size + 1) - number_of_attempts_made
72+
end
73+
end
5874
end
5975

6076
TriggeredWebhook.plugin :timestamps, update_on_create: true

spec/lib/pact_broker/api/decorators/pact_webhooks_status_decorator_spec.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module Decorators
1919
webhook_uuid: '4321',
2020
request_description: "GET http://foo",
2121
pact_publication: pact,
22+
number_of_attempts_made: 1,
23+
number_of_attempts_remaining: 2,
2224
created_at: DateTime.new(2017),
2325
updated_at: DateTime.new(2017)
2426
)
@@ -57,7 +59,12 @@ module Decorators
5759
end
5860

5961
it "includes the triggered webhooks properties" do
60-
expect(subject[:_embedded][:triggeredWebhooks].first).to include(status: 'success', triggerType: 'pact_publication')
62+
expect(subject[:_embedded][:triggeredWebhooks].first).to include(
63+
status: 'success',
64+
triggerType: 'pact_publication',
65+
attemptsMade: 1,
66+
attemptsRemaining: 2
67+
)
6168
end
6269

6370
it "includes a link to the consumer" do
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'pact_broker/webhooks/triggered_webhook'
2+
3+
module PactBroker
4+
module Webhooks
5+
describe TriggeredWebhook do
6+
let(:status) { TriggeredWebhook::STATUS_SUCCESS }
7+
8+
subject { TriggeredWebhook.new(status: status) }
9+
10+
describe "remaining_attempts" do
11+
before do
12+
PactBroker.configuration.webhook_retry_schedule = [1, 1, 1]
13+
allow(subject).to receive(:webhook_executions).and_return([double('execution')])
14+
end
15+
16+
its(:number_of_attempts_made) { is_expected.to eq 1 }
17+
18+
context "when its status is retrying" do
19+
let(:status) { TriggeredWebhook::STATUS_RETRYING }
20+
its(:number_of_attempts_remaining) { is_expected.to eq 3 }
21+
end
22+
23+
context "when its status is not_run" do
24+
let(:status) { TriggeredWebhook::STATUS_NOT_RUN }
25+
its(:number_of_attempts_remaining) { is_expected.to eq 3 }
26+
end
27+
28+
context "when its status is success" do
29+
let(:status) { TriggeredWebhook::STATUS_SUCCESS }
30+
its(:number_of_attempts_remaining) { is_expected.to eq 0}
31+
end
32+
33+
context "when its status is failure" do
34+
let(:status) { TriggeredWebhook::STATUS_FAILURE }
35+
its(:number_of_attempts_remaining) { is_expected.to eq 0}
36+
end
37+
end
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)