From ecce16fd4771cd202a4b09394de781d3d301ae32 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 22 Sep 2020 19:10:15 +1000 Subject: [PATCH] feat: add event name to triggered webhook --- .../20200922_add_event_to_triggered_webhook.rb | 5 +++++ .../api/decorators/triggered_webhook_decorator.rb | 1 + lib/pact_broker/configuration.rb | 13 ++++++++++++- lib/pact_broker/test/test_data_builder.rb | 2 +- lib/pact_broker/webhooks/repository.rb | 5 +++-- lib/pact_broker/webhooks/service.rb | 2 +- lib/pact_broker/webhooks/triggered_webhook.rb | 4 +--- lib/pact_broker/webhooks/webhook_event.rb | 3 +-- .../pact_webhooks_status_decorator_spec.rb | 3 ++- .../decorators/triggered_webhook_decorator_spec.rb | 4 +++- spec/lib/pact_broker/webhooks/repository_spec.rb | 5 +++-- 11 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 db/migrations/20200922_add_event_to_triggered_webhook.rb diff --git a/db/migrations/20200922_add_event_to_triggered_webhook.rb b/db/migrations/20200922_add_event_to_triggered_webhook.rb new file mode 100644 index 000000000..6132f252b --- /dev/null +++ b/db/migrations/20200922_add_event_to_triggered_webhook.rb @@ -0,0 +1,5 @@ +Sequel.migration do + change do + add_column(:triggered_webhooks, :event_name, String) + end +end diff --git a/lib/pact_broker/api/decorators/triggered_webhook_decorator.rb b/lib/pact_broker/api/decorators/triggered_webhook_decorator.rb index e2c60d485..f5f6a7e90 100644 --- a/lib/pact_broker/api/decorators/triggered_webhook_decorator.rb +++ b/lib/pact_broker/api/decorators/triggered_webhook_decorator.rb @@ -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 diff --git a/lib/pact_broker/configuration.rb b/lib/pact_broker/configuration.rb index be354ca4b..8777957fa 100644 --- a/lib/pact_broker/configuration.rb +++ b/lib/pact_broker/configuration.rb @@ -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 @@ -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' diff --git a/lib/pact_broker/test/test_data_builder.rb b/lib/pact_broker/test/test_data_builder.rb index 1f088923a..fc6feb866 100644 --- a/lib/pact_broker/test/test_data_builder.rb +++ b/lib/pact_broker/test/test_data_builder.rb @@ -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 diff --git a/lib/pact_broker/webhooks/repository.rb b/lib/pact_broker/webhooks/repository.rb index 057ccf7db..1939e56f6 100644 --- a/lib/pact_broker/webhooks/repository.rb +++ b/lib/pact_broker/webhooks/repository.rb @@ -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, @@ -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 diff --git a/lib/pact_broker/webhooks/service.rb b/lib/pact_broker/webhooks/service.rb index 6fffc8984..160810484 100644 --- a/lib/pact_broker/webhooks/service.rb +++ b/lib/pact_broker/webhooks/service.rb @@ -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) diff --git a/lib/pact_broker/webhooks/triggered_webhook.rb b/lib/pact_broker/webhooks/triggered_webhook.rb index 3caead79e..8de8ad167 100644 --- a/lib/pact_broker/webhooks/triggered_webhook.rb +++ b/lib/pact_broker/webhooks/triggered_webhook.rb @@ -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' @@ -95,9 +96,6 @@ def number_of_attempts_remaining end end end - - TriggeredWebhook.plugin :timestamps, update_on_create: true - end end diff --git a/lib/pact_broker/webhooks/webhook_event.rb b/lib/pact_broker/webhooks/webhook_event.rb index e1c0b2a43..f422f3076 100644 --- a/lib/pact_broker/webhooks/webhook_event.rb +++ b/lib/pact_broker/webhooks/webhook_event.rb @@ -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' @@ -38,8 +39,6 @@ def provider_verification_failed? name == VERIFICATION_FAILED end end - - WebhookEvent.plugin :timestamps, update_on_create: true end end 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 c3f410d4c..8159d77d7 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 @@ -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 diff --git a/spec/lib/pact_broker/api/decorators/triggered_webhook_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/triggered_webhook_decorator_spec.rb index ca6af20dd..3f737c167 100644 --- a/spec/lib/pact_broker/api/decorators/triggered_webhook_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/triggered_webhook_decorator_spec.rb @@ -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, @@ -55,7 +56,8 @@ module Decorators status: 'success', triggerType: 'resource_creation', attemptsMade: 1, - attemptsRemaining: 2 + attemptsRemaining: 2, + eventName: 'some_event' ) end end diff --git a/spec/lib/pact_broker/webhooks/repository_spec.rb b/spec/lib/pact_broker/webhooks/repository_spec.rb index 516ea10a5..32434bfef 100644 --- a/spec/lib/pact_broker/webhooks/repository_spec.rb +++ b/spec/lib/pact_broker/webhooks/repository_spec.rb @@ -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 @@ -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 @@ -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