lib/pact_broker/webhooks/service.rb in pact_broker-2.22.0 vs lib/pact_broker/webhooks/service.rb in pact_broker-2.23.0

- old
+ new

@@ -1,23 +1,27 @@ require 'pact_broker/repositories' +require 'pact_broker/services' require 'pact_broker/logging' require 'base64' require 'securerandom' require 'pact_broker/webhooks/job' require 'pact_broker/webhooks/triggered_webhook' require 'pact_broker/webhooks/status' require 'pact_broker/webhooks/webhook_event' +require 'pact_broker/verifications/placeholder_verification' +require 'pact_broker/pacts/placeholder_pact' module PactBroker module Webhooks class Service RESOURCE_CREATION = PactBroker::Webhooks::TriggeredWebhook::TRIGGER_TYPE_RESOURCE_CREATION USER = PactBroker::Webhooks::TriggeredWebhook::TRIGGER_TYPE_USER extend Repositories + extend Services include Logging def self.next_uuid SecureRandom.urlsafe_base64 end @@ -57,10 +61,21 @@ def self.find_all webhook_repository.find_all end + def self.test_execution webhook + options = { failure_log_message: "Webhook execution failed", show_response: PactBroker.configuration.show_webhook_response?} + 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, options) + end + def self.execute_webhook_now webhook, pact, verification = nil triggered_webhook = webhook_repository.create_triggered_webhook(next_uuid, webhook, pact, verification, USER) options = { failure_log_message: "Webhook execution failed"} webhook_execution_result = execute_triggered_webhook_now triggered_webhook, options if webhook_execution_result.success? @@ -79,16 +94,24 @@ def self.update_triggered_webhook_status triggered_webhook, status webhook_repository.update_triggered_webhook_status triggered_webhook, status end + def self.find_for_pact pact + webhook_repository.find_for_pact(pact) + end + + def self.find_by_consumer_and_or_provider consumer, provider + webhook_repository.find_by_consumer_and_or_provider(consumer, provider) + end + def self.find_by_consumer_and_provider consumer, provider webhook_repository.find_by_consumer_and_provider consumer, provider end - def self.execute_webhooks pact, verification, event_name - webhooks = webhook_repository.find_by_consumer_and_provider_and_event_name pact.consumer, pact.provider, event_name + def self.trigger_webhooks pact, verification, event_name + webhooks = webhook_repository.find_by_consumer_and_or_provider_and_event_name pact.consumer, pact.provider, event_name if webhooks.any? run_later(webhooks, pact, verification, event_name) else logger.debug "No webhook found for consumer \"#{pact.consumer.name}\" and provider \"#{pact.provider.name}\"" @@ -107,15 +130,27 @@ log_error e end end end + def self.find_latest_triggered_webhooks_for_pact pact + webhook_repository.find_latest_triggered_webhooks_for_pact pact + end + def self.find_latest_triggered_webhooks consumer, provider webhook_repository.find_latest_triggered_webhooks consumer, provider end def self.fail_retrying_triggered_webhooks webhook_repository.fail_retrying_triggered_webhooks + end + + def self.find_triggered_webhooks_for_pact pact + 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 end end end