lib/pact_broker/webhooks/service.rb in pact_broker-2.34.0 vs lib/pact_broker/webhooks/service.rb in pact_broker-2.35.0

- old
+ new

@@ -8,22 +8,27 @@ require 'pact_broker/webhooks/status' require 'pact_broker/webhooks/webhook_event' require 'pact_broker/verifications/placeholder_verification' require 'pact_broker/pacts/placeholder_pact' require 'pact_broker/api/decorators/webhook_decorator' +require 'pact_broker/hash_refinements' +require 'pact_broker/webhooks/execution_configuration' +require 'pact_broker/messages' +require 'pact_broker/webhooks/pact_and_verification_parameters' module PactBroker - module Webhooks class Service + using PactBroker::HashRefinements RESOURCE_CREATION = PactBroker::Webhooks::TriggeredWebhook::TRIGGER_TYPE_RESOURCE_CREATION USER = PactBroker::Webhooks::TriggeredWebhook::TRIGGER_TYPE_USER extend Repositories extend Services include Logging + extend PactBroker::Messages def self.next_uuid SecureRandom.urlsafe_base64 end @@ -65,39 +70,24 @@ def self.find_all webhook_repository.find_all end - def self.test_execution webhook, options - logging_options = options[:logging_options].merge( - failure_log_message: "Webhook execution failed", - ) - merged_options = options.merge(logging_options: logging_options) + def self.test_execution webhook, execution_configuration + merged_options = execution_configuration.with_failure_log_message("Webhook execution failed").to_hash + verification = nil if webhook.trigger_on_provider_verification_published? verification = verification_service.search_for_latest(webhook.consumer_name, webhook.provider_name) || PactBroker::Verifications::PlaceholderVerification.new end pact = pact_service.search_for_latest_pact(consumer_name: webhook.consumer_name, provider_name: webhook.provider_name) || PactBroker::Pacts::PlaceholderPact.new webhook.execute(pact, verification, merged_options) end - # # TODO delete? - # def self.execute_webhook_now webhook, pact, verification = nil - # triggered_webhook = webhook_repository.create_triggered_webhook(next_uuid, webhook, pact, verification, USER) - # logging_options = { failure_log_message: "Webhook execution failed"} - # webhook_execution_result = execute_triggered_webhook_now triggered_webhook, logging_options - # if webhook_execution_result.success? - # webhook_repository.update_triggered_webhook_status triggered_webhook, TriggeredWebhook::STATUS_SUCCESS - # else - # webhook_repository.update_triggered_webhook_status triggered_webhook, TriggeredWebhook::STATUS_FAILURE - # end - # webhook_execution_result - # end - - def self.execute_triggered_webhook_now triggered_webhook, webhook_options - webhook_execution_result = triggered_webhook.execute webhook_options + def self.execute_triggered_webhook_now triggered_webhook, webhook_execution_configuration_hash + webhook_execution_result = triggered_webhook.execute webhook_execution_configuration_hash webhook_repository.create_execution triggered_webhook, webhook_execution_result webhook_execution_result end def self.update_triggered_webhook_status triggered_webhook, status @@ -130,16 +120,11 @@ trigger_uuid = next_uuid webhooks.each do | webhook | begin triggered_webhook = webhook_repository.create_triggered_webhook(trigger_uuid, webhook, pact, verification, RESOURCE_CREATION) logger.info "Scheduling job for webhook with uuid #{webhook.uuid}" - job_data = { - triggered_webhook: triggered_webhook, - webhook_context: options.fetch(:webhook_context), - logging_options: options.fetch(:logging_options), - database_connector: options.fetch(:database_connector) - } + job_data = { triggered_webhook: triggered_webhook }.deep_merge(options) # Delay slightly to make sure the request transaction has finished before we execute the webhook Job.perform_in(5, job_data) rescue StandardError => e log_error e end @@ -162,9 +147,18 @@ webhook_repository.find_triggered_webhooks_for_pact(pact) end def self.find_triggered_webhooks_for_verification verification webhook_repository.find_triggered_webhooks_for_verification(verification) + end + + def self.parameters + PactAndVerificationParameters::ALL.collect do | parameter | + OpenStruct.new( + name: parameter, + description: message("messages.webhooks.parameters.#{parameter}") + ) + end end private # Dirty hack to maintain existing password or Authorization header if it is submitted with value ****