Skip to content

Commit

Permalink
feat: add event name to triggered webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Sep 22, 2020
1 parent ba94c35 commit ecce16f
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 14 deletions.
5 changes: 5 additions & 0 deletions db/migrations/20200922_add_event_to_triggered_webhook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Sequel.migration do
change do
add_column(:triggered_webhooks, :event_name, String)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TriggeredWebhookDecorator < BaseDecorator
property :number_of_attempts_made, as: :attemptsMade
property :number_of_attempts_remaining, as: :attemptsRemaining
property :trigger_type, as: :triggerType
property :event_name, as: :eventName

property :created_at, as: :triggeredAt

Expand Down
13 changes: 12 additions & 1 deletion lib/pact_broker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ class Configuration
:base_equality_only_on_content_that_affects_verification_results,
:seed_example_data,
:badge_provider_mode,
:warning_error_class_names
:warning_error_class_names,
:base_url,
:log_dir,
:allow_missing_migration_files,
:auto_migrate_db_data,
:use_rack_protection
]

attr_accessor :base_url, :log_dir, :database_connection, :auto_migrate_db, :auto_migrate_db_data, :allow_missing_migration_files, :example_data_seeder, :seed_example_data, :use_hal_browser, :html_pact_renderer, :use_rack_protection
Expand Down Expand Up @@ -129,6 +134,12 @@ def logger= logger
@custom_logger = logger
end

def log_configuration
SAVABLE_SETTING_NAMES.sort.each do | setting |
logger.info "PactBroker.configuration.#{setting}=#{PactBroker.configuration.send(setting).inspect}"
end
end

def self.default_html_pact_render
lambda { |pact, options|
require 'pact_broker/api/renderers/html_pact_renderer'
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/test/test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def create_triggered_webhook params = {}
params.delete(:comment)
trigger_uuid = params[:trigger_uuid] || webhook_service.next_uuid
verification = @webhook.trigger_on_provider_verification_published? ? @verification : nil
@triggered_webhook = webhook_repository.create_triggered_webhook trigger_uuid, @webhook, @pact, verification, PactBroker::Webhooks::Service::RESOURCE_CREATION
@triggered_webhook = webhook_repository.create_triggered_webhook trigger_uuid, @webhook, @pact, verification, PactBroker::Webhooks::Service::RESOURCE_CREATION, @webhook.events.first.name
@triggered_webhook.update(status: params[:status]) if params[:status]
set_created_at_if_set params[:created_at], :triggered_webhooks, {id: @triggered_webhook.id}
self
Expand Down
5 changes: 3 additions & 2 deletions lib/pact_broker/webhooks/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def find_by_consumer_and_provider_and_event_name consumer, provider, event_name
.collect(&:to_domain)
end

def create_triggered_webhook trigger_uuid, webhook, pact, verification, trigger_type
def create_triggered_webhook trigger_uuid, webhook, pact, verification, trigger_type, event_name
db_webhook = Webhook.where(uuid: webhook.uuid).single_record
TriggeredWebhook.create(
status: TriggeredWebhook::STATUS_NOT_RUN,
Expand All @@ -112,7 +112,8 @@ def create_triggered_webhook trigger_uuid, webhook, pact, verification, trigger_
trigger_uuid: trigger_uuid,
trigger_type: trigger_type,
consumer: pact.consumer,
provider: pact.provider
provider: pact.provider,
event_name: event_name
)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/webhooks/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def self.run_later webhooks, pact, verification, event_name, options
trigger_uuid = next_uuid
webhooks.each do | webhook |
begin
triggered_webhook = webhook_repository.create_triggered_webhook(trigger_uuid, webhook, pact, verification, RESOURCE_CREATION)
triggered_webhook = webhook_repository.create_triggered_webhook(trigger_uuid, webhook, pact, verification, RESOURCE_CREATION, event_name)
logger.info "Scheduling job for webhook with uuid #{webhook.uuid}"
logger.debug "Schedule webhook with options #{options}"
job_data = { triggered_webhook: triggered_webhook }.deep_merge(options)
Expand Down
4 changes: 1 addition & 3 deletions lib/pact_broker/webhooks/triggered_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
module PactBroker
module Webhooks
class TriggeredWebhook < Sequel::Model(:triggered_webhooks)
plugin :timestamps, update_on_create: true

TRIGGER_TYPE_RESOURCE_CREATION = 'resource_creation'
TRIGGER_TYPE_USER = 'user'
Expand Down Expand Up @@ -95,9 +96,6 @@ def number_of_attempts_remaining
end
end
end

TriggeredWebhook.plugin :timestamps, update_on_create: true

end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/pact_broker/webhooks/webhook_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module PactBroker
module Webhooks
class WebhookEvent < Sequel::Model
plugin :timestamps, update_on_create: true

CONTRACT_PUBLISHED = 'contract_published'
CONTRACT_CONTENT_CHANGED = 'contract_content_changed'
Expand Down Expand Up @@ -38,8 +39,6 @@ def provider_verification_failed?
name == VERIFICATION_FAILED
end
end

WebhookEvent.plugin :timestamps, update_on_create: true
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module Decorators
number_of_attempts_made: 1,
number_of_attempts_remaining: 2,
created_at: DateTime.new(2017),
updated_at: DateTime.new(2017)
updated_at: DateTime.new(2017),
event_name: 'some_event'
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Decorators
let(:triggered_webhook) do
double('PactBroker::Webhooks::TriggeredWebhook',
trigger_type: PactBroker::Webhooks::TriggeredWebhook::TRIGGER_TYPE_RESOURCE_CREATION,
event_name: 'some_event',
status: status,
failure?: failure,
retrying?: retrying,
Expand Down Expand Up @@ -55,7 +56,8 @@ module Decorators
status: 'success',
triggerType: 'resource_creation',
attemptsMade: 1,
attemptsRemaining: 2
attemptsRemaining: 2,
eventName: 'some_event'
)
end
end
Expand Down
5 changes: 3 additions & 2 deletions spec/lib/pact_broker/webhooks/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ module Webhooks
.create_verification
end

subject { Repository.new.create_triggered_webhook '1234', td.webhook, td.pact, td.verification, 'publication' }
subject { Repository.new.create_triggered_webhook '1234', td.webhook, td.pact, td.verification, 'publication', 'some_event' }

it "creates a TriggeredWebhook" do
expect(subject.webhook_uuid ).to eq td.webhook.uuid
Expand All @@ -395,6 +395,7 @@ module Webhooks
expect(subject.verification).to eq td.verification
expect(subject.trigger_uuid).to eq '1234'
expect(subject.trigger_type).to eq 'publication'
expect(subject.event_name).to eq 'some_event'
end

it "sets the webhook" do
Expand All @@ -418,7 +419,7 @@ module Webhooks
end

context "without a verification" do
subject { Repository.new.create_triggered_webhook '1234', td.webhook, td.pact, nil, 'publication' }
subject { Repository.new.create_triggered_webhook '1234', td.webhook, td.pact, nil, 'publication', 'some_event' }

it "does not set the verification" do
expect(subject.verification).to be nil
Expand Down

0 comments on commit ecce16f

Please sign in to comment.