-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add resource to view the triggered webhooks for a pact publication
- Loading branch information
Showing
16 changed files
with
301 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 1 addition & 26 deletions
27
lib/pact_broker/api/decorators/pact_webhooks_status_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
lib/pact_broker/api/decorators/triggered_webhook_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require_relative 'base_decorator' | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
class TriggeredWebhookDecorator < BaseDecorator | ||
property :request_description, as: :name | ||
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]), | ||
title: "Webhook execution logs", | ||
name: represented.request_description | ||
} | ||
end | ||
|
||
link :'pb:webhook' do | context | | ||
{ | ||
href: webhook_url(represented.webhook_uuid, context[:base_url]), | ||
title: "Webhook", | ||
name: represented.request_description | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
lib/pact_broker/api/decorators/triggered_webhooks_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'pact_broker/api/decorators/base_decorator' | ||
require 'pact_broker/api/decorators/triggered_webhook_decorator' | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
class TriggeredWebhooksDecorator < BaseDecorator | ||
collection :entries, as: :triggeredWebhooks, embedded: true, :extend => PactBroker::Api::Decorators::TriggeredWebhookDecorator | ||
|
||
link :self do | options | | ||
{ | ||
title: options.fetch(:resource_title), | ||
href: options.fetch(:resource_url) | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'pact_broker/api/decorators/triggered_webhooks_decorator' | ||
|
||
module PactBroker | ||
module Api | ||
module Resources | ||
class PactTriggeredWebhooks < BaseResource | ||
def allowed_methods | ||
["GET"] | ||
end | ||
|
||
def content_types_provided | ||
[["application/hal+json", :to_json]] | ||
end | ||
|
||
def resource_exists? | ||
!!pact | ||
end | ||
|
||
def to_json | ||
Decorators::TriggeredWebhooksDecorator.new(triggered_webhooks).to_json(decorator_options) | ||
end | ||
|
||
private | ||
|
||
def triggered_webhooks | ||
webhook_service.find_triggered_webhooks_for_pact(pact) | ||
end | ||
|
||
def resource_title | ||
"Webhooks triggered by the publication of the #{pact.name[0].downcase}#{pact.name[1..-1]}" | ||
end | ||
|
||
def decorator_options | ||
{ | ||
user_options: decorator_context.merge(resource_title: resource_title) | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
RSpec.describe "Get triggered webhooks for pact" do | ||
before do | ||
td.create_pact_with_hierarchy | ||
.create_webhook | ||
.create_triggered_webhook | ||
.create_webhook_execution | ||
end | ||
|
||
let(:td) { TestDataBuilder.new } | ||
let(:path) { PactBroker::Api::PactBrokerUrls.pact_triggered_webhooks_url(td.pact) } | ||
let(:json_response_body) { JSON.parse(subject.body) } | ||
|
||
subject { get(path); last_response } | ||
|
||
it { is_expected.to be_a_hal_json_success_response } | ||
|
||
it "contains a list of triggered webhooks" do | ||
expect(json_response_body['_embedded']['triggeredWebhooks'].size).to be 1 | ||
end | ||
end |
64 changes: 64 additions & 0 deletions
64
spec/lib/pact_broker/api/decorators/triggered_webhook_decorator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'pact_broker/api/decorators/triggered_webhook_decorator' | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
describe TriggeredWebhookDecorator do | ||
let(:triggered_webhook) do | ||
double('PactBroker::Webhooks::TriggeredWebhook', | ||
trigger_type: PactBroker::Webhooks::TriggeredWebhook::TRIGGER_TYPE_RESOURCE_CREATION, | ||
status: status, | ||
failure?: failure, | ||
retrying?: retrying, | ||
trigger_uuid: '1234', | ||
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) | ||
) | ||
end | ||
|
||
let(:pact) do | ||
double('pact', | ||
provider: double(name: 'provider'), | ||
consumer: double(name: 'consumer'), | ||
consumer_version_number: '1', | ||
name: 'foo ' | ||
) | ||
end | ||
|
||
let(:failure) { false } | ||
let(:retrying) { false } | ||
let(:status) { PactBroker::Webhooks::TriggeredWebhook::STATUS_SUCCESS } | ||
let(:logs_url) { "http://example.org/webhooks/4321/trigger/1234/logs" } | ||
let(:user_options) { { base_url: "http://example.org" } } | ||
|
||
let(:json) do | ||
TriggeredWebhookDecorator.new(triggered_webhook).to_json(user_options: user_options) | ||
end | ||
|
||
subject { JSON.parse(json, symbolize_names: true) } | ||
|
||
it "includes a link to the logs" do | ||
expect(subject[:_links][:logs][:href]).to eq logs_url | ||
end | ||
|
||
it "includes a link to the webhook" do | ||
expect(subject[:_links][:'pb:webhook'][:href]).to eq "http://example.org/webhooks/4321" | ||
end | ||
|
||
it "includes the triggered webhooks properties" do | ||
expect(subject).to include( | ||
status: 'success', | ||
triggerType: 'resource_creation', | ||
attemptsMade: 1, | ||
attemptsRemaining: 2 | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
28 changes: 28 additions & 0 deletions
28
spec/lib/pact_broker/api/decorators/triggered_webhooks_decorator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'pact_broker/api/decorators/triggered_webhooks_decorator' | ||
require 'pact_broker/webhooks/triggered_webhook' | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
describe TriggeredWebhooksDecorator do | ||
let(:triggered_webhook) do | ||
instance_double(PactBroker::Webhooks::TriggeredWebhook).as_null_object | ||
end | ||
let(:decorator) { TriggeredWebhooksDecorator.new([triggered_webhook]) } | ||
let(:user_options) { { resource_title: "Title", resource_url: "http://url" } } | ||
let(:json) { decorator.to_json(user_options: user_options) } | ||
|
||
subject { JSON.parse(json) } | ||
|
||
it "includes a self relation" do | ||
expect(subject['_links']['self']['title']).to eq "Title" | ||
expect(subject['_links']['self']['href']).to eq "http://url" | ||
end | ||
|
||
it "includes an embedded list of triggered webhooks" do | ||
expect(subject['_embedded']['triggeredWebhooks']).to be_instance_of(Array) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.