Skip to content

Commit

Permalink
feat(webhook status): display attempts made and attempts remaining in…
Browse files Browse the repository at this point in the history
… webhook status resource
  • Loading branch information
bethesque committed Sep 6, 2017
1 parent f2d92f3 commit 648e1c3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ module Decorators

class TriggeredWebhookDecorator < BaseDecorator
property :status
property :number_of_attempts_made, as: :attemptsMade
property :number_of_attempts_remaining, as: :attemptsRemaining
property :trigger_type, as: :triggerType

property :created_at, as: :triggeredAt



link :logs do | context |
{
href: triggered_webhook_logs_url(represented, context[:base_url]),
Expand Down
16 changes: 16 additions & 0 deletions lib/pact_broker/webhooks/triggered_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ def retrying?
def not_run?
status == STATUS_NOT_RUN
end

def number_of_attempts_made
webhook_executions.size
end

def finished?
success? || failure?
end

def number_of_attempts_remaining
if finished?
0
else
(PactBroker.configuration.webhook_retry_schedule.size + 1) - number_of_attempts_made
end
end
end

TriggeredWebhook.plugin :timestamps, update_on_create: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module Decorators
webhook_uuid: '4321',
request_description: "GET http://foo",
pact_publication: pact,
number_of_attempts_made: 1,
number_of_attempts_remaining: 2,
created_at: DateTime.new(2017),
updated_at: DateTime.new(2017)
)
Expand Down Expand Up @@ -57,7 +59,12 @@ module Decorators
end

it "includes the triggered webhooks properties" do
expect(subject[:_embedded][:triggeredWebhooks].first).to include(status: 'success', triggerType: 'pact_publication')
expect(subject[:_embedded][:triggeredWebhooks].first).to include(
status: 'success',
triggerType: 'pact_publication',
attemptsMade: 1,
attemptsRemaining: 2
)
end

it "includes a link to the consumer" do
Expand Down
40 changes: 40 additions & 0 deletions spec/lib/pact_broker/webhooks/triggered_webhook_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'pact_broker/webhooks/triggered_webhook'

module PactBroker
module Webhooks
describe TriggeredWebhook do
let(:status) { TriggeredWebhook::STATUS_SUCCESS }

subject { TriggeredWebhook.new(status: status) }

describe "remaining_attempts" do
before do
PactBroker.configuration.webhook_retry_schedule = [1, 1, 1]
allow(subject).to receive(:webhook_executions).and_return([double('execution')])
end

its(:number_of_attempts_made) { is_expected.to eq 1 }

context "when its status is retrying" do
let(:status) { TriggeredWebhook::STATUS_RETRYING }
its(:number_of_attempts_remaining) { is_expected.to eq 3 }
end

context "when its status is not_run" do
let(:status) { TriggeredWebhook::STATUS_NOT_RUN }
its(:number_of_attempts_remaining) { is_expected.to eq 3 }
end

context "when its status is success" do
let(:status) { TriggeredWebhook::STATUS_SUCCESS }
its(:number_of_attempts_remaining) { is_expected.to eq 0}
end

context "when its status is failure" do
let(:status) { TriggeredWebhook::STATUS_FAILURE }
its(:number_of_attempts_remaining) { is_expected.to eq 0}
end
end
end
end
end

0 comments on commit 648e1c3

Please sign in to comment.