From 648e1c31e3743a8003aa0491108e1ae61044d6dc Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 6 Sep 2017 14:02:59 +1000 Subject: [PATCH] feat(webhook status): display attempts made and attempts remaining in webhook status resource --- .../pact_webhooks_status_decorator.rb | 4 +- lib/pact_broker/webhooks/triggered_webhook.rb | 16 ++++++++ .../pact_webhooks_status_decorator_spec.rb | 9 ++++- .../webhooks/triggered_webhook_spec.rb | 40 +++++++++++++++++++ 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 spec/lib/pact_broker/webhooks/triggered_webhook_spec.rb diff --git a/lib/pact_broker/api/decorators/pact_webhooks_status_decorator.rb b/lib/pact_broker/api/decorators/pact_webhooks_status_decorator.rb index 00294c424..687de738f 100644 --- a/lib/pact_broker/api/decorators/pact_webhooks_status_decorator.rb +++ b/lib/pact_broker/api/decorators/pact_webhooks_status_decorator.rb @@ -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]), diff --git a/lib/pact_broker/webhooks/triggered_webhook.rb b/lib/pact_broker/webhooks/triggered_webhook.rb index 11c3b60e4..0ad310b4a 100644 --- a/lib/pact_broker/webhooks/triggered_webhook.rb +++ b/lib/pact_broker/webhooks/triggered_webhook.rb @@ -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 diff --git a/spec/lib/pact_broker/api/decorators/pact_webhooks_status_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/pact_webhooks_status_decorator_spec.rb index 7b23b1bea..26f8a08db 100644 --- a/spec/lib/pact_broker/api/decorators/pact_webhooks_status_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/pact_webhooks_status_decorator_spec.rb @@ -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) ) @@ -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 diff --git a/spec/lib/pact_broker/webhooks/triggered_webhook_spec.rb b/spec/lib/pact_broker/webhooks/triggered_webhook_spec.rb new file mode 100644 index 000000000..a51682b16 --- /dev/null +++ b/spec/lib/pact_broker/webhooks/triggered_webhook_spec.rb @@ -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