Sha256: 0c3443538ac5c4faf781c9f1bd731e7e3d8bb640f43a4448984af9ea0fd797d1

Contents?: true

Size: 1.83 KB

Versions: 11

Compression:

Stored size: 1.83 KB

Contents

require 'pact_broker/repositories'
require 'pact_broker/logging'
require 'base64'

module PactBroker

  module Webhooks
    class Service

      extend Repositories
      include Logging

      def self.next_uuid
        SecureRandom.urlsafe_base64
      end

      def self.errors webhook
        contract = PactBroker::Api::Contracts::WebhookContract.new(webhook)
        contract.validate(webhook.attributes)
        contract.errors
      end

      def self.create uuid, webhook, consumer, provider
        webhook_repository.create uuid, webhook, consumer, provider
      end

      def self.find_by_uuid uuid
        webhook_repository.find_by_uuid uuid
      end

      def self.delete_by_uuid uuid
        webhook_repository.delete_by_uuid uuid
      end

      def self.delete_by_pacticipant pacticipant
        webhook_repository.delete_by_pacticipant pacticipant
      end

      def self.find_all
        webhook_repository.find_all
      end

      def self.execute_webhook_now webhook
        webhook.execute
      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
        webhooks = webhook_repository.find_by_consumer_and_provider pact.consumer, pact.provider
        if webhooks.any?
          run_later(webhooks)
        else
          logger.debug "No webhook found for consumer \"#{pact.consumer.name}\" and provider \"#{pact.provider.name}\""
        end
      end

      # TODO background job?
      def self.run_later webhooks
        Thread.new do
          webhooks.each do | webhook |
            begin
              webhook.execute
            rescue StandardError => e
              # Exceptions are already logged, no need to log again.
            end
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
pact_broker-2.0.0.beta.6 lib/pact_broker/webhooks/service.rb
pact_broker-2.0.0.beta.5 lib/pact_broker/webhooks/service.rb
pact_broker-1.18.0.beta.1 lib/pact_broker/webhooks/service.rb
pact_broker-1.17.2 lib/pact_broker/webhooks/service.rb
pact_broker-1.17.1 lib/pact_broker/webhooks/service.rb
pact_broker-2.0.0.beta.4 lib/pact_broker/webhooks/service.rb
pact_broker-2.0.0.beta.3 lib/pact_broker/webhooks/service.rb
pact_broker-2.0.0.beta.2 lib/pact_broker/webhooks/service.rb
pact_broker-2.0.0.beta.1 lib/pact_broker/webhooks/service.rb
pact_broker-1.17.0 lib/pact_broker/webhooks/service.rb
pact_broker-1.16.0 lib/pact_broker/webhooks/service.rb