Class: PactBroker::Webhooks::Service

Inherits:
Object
  • Object
show all
Extended by:
Repositories
Includes:
Logging
Defined in:
lib/pact_broker/webhooks/service.rb

Constant Summary collapse

RESOURCE_CREATION =
PactBroker::Webhooks::TriggeredWebhook::TRIGGER_TYPE_RESOURCE_CREATION
USER =
PactBroker::Webhooks::TriggeredWebhook::TRIGGER_TYPE_USER

Constants included from Logging

Logging::LOG_DIR, Logging::LOG_FILE_NAME

Class Method Summary collapse

Methods included from Repositories

label_repository, matrix_repository, pact_repository, pacticipant_repository, tag_repository, verification_repository, version_repository, webhook_repository

Methods included from Logging

included, #log_error, #logger, #logger=

Class Method Details

.create(uuid, webhook, consumer, provider) ⇒ Object



31
32
33
# File 'lib/pact_broker/webhooks/service.rb', line 31

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


48
49
50
51
52
# File 'lib/pact_broker/webhooks/service.rb', line 48

def self.delete_all_webhhook_related_objects_by_pacticipant pacticipant
  webhook_repository.delete_executions_by_pacticipant pacticipant
  webhook_repository.delete_triggered_webhooks_by_pacticipant pacticipant
  webhook_repository.delete_by_pacticipant pacticipant
end


54
55
56
# File 'lib/pact_broker/webhooks/service.rb', line 54

def self.delete_all_webhook_related_objects_by_pact_publication_ids pact_publication_ids
  webhook_repository.delete_triggered_webhooks_by_pact_publication_ids pact_publication_ids
end

.delete_by_uuid(uuid) ⇒ Object



43
44
45
46
# File 'lib/pact_broker/webhooks/service.rb', line 43

def self.delete_by_uuid uuid
  webhook_repository.delete_triggered_webhooks_by_webhook_uuid uuid
  webhook_repository.delete_by_uuid uuid
end

.errors(webhook) ⇒ Object



25
26
27
28
29
# File 'lib/pact_broker/webhooks/service.rb', line 25

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

.execute_triggered_webhook_now(triggered_webhook, options) ⇒ Object



73
74
75
76
77
# File 'lib/pact_broker/webhooks/service.rb', line 73

def self.execute_triggered_webhook_now triggered_webhook, options
  webhook_execution_result = triggered_webhook.execute options
  webhook_repository.create_execution triggered_webhook, webhook_execution_result
  webhook_execution_result
end

.execute_webhook_now(webhook, pact) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/pact_broker/webhooks/service.rb', line 62

def self.execute_webhook_now webhook, pact
  triggered_webhook = webhook_repository.create_triggered_webhook(next_uuid, webhook, pact, USER)
  webhook_execution_result = execute_triggered_webhook_now triggered_webhook, failure_log_message: "Webhook execution failed"
  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

.execute_webhooks(pact, event_name) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/pact_broker/webhooks/service.rb', line 87

def self.execute_webhooks pact, event_name
  webhooks = webhook_repository.find_by_consumer_and_provider_and_event_name pact.consumer, pact.provider, event_name

  if webhooks.any?
    run_later(webhooks, pact, event_name)
  else
    logger.debug "No webhook found for consumer \"#{pact.consumer.name}\" and provider \"#{pact.provider.name}\""
  end
end

.fail_retrying_triggered_webhooksObject



115
116
117
# File 'lib/pact_broker/webhooks/service.rb', line 115

def self.fail_retrying_triggered_webhooks
  webhook_repository.fail_retrying_triggered_webhooks
end

.find_allObject



58
59
60
# File 'lib/pact_broker/webhooks/service.rb', line 58

def self.find_all
  webhook_repository.find_all
end

.find_by_consumer_and_provider(consumer, provider) ⇒ Object



83
84
85
# File 'lib/pact_broker/webhooks/service.rb', line 83

def self.find_by_consumer_and_provider consumer, provider
  webhook_repository.find_by_consumer_and_provider consumer, provider
end

.find_by_uuid(uuid) ⇒ Object



35
36
37
# File 'lib/pact_broker/webhooks/service.rb', line 35

def self.find_by_uuid uuid
  webhook_repository.find_by_uuid uuid
end

.find_latest_triggered_webhooks(consumer, provider) ⇒ Object



111
112
113
# File 'lib/pact_broker/webhooks/service.rb', line 111

def self.find_latest_triggered_webhooks consumer, provider
  webhook_repository.find_latest_triggered_webhooks consumer, provider
end

.next_uuidObject



21
22
23
# File 'lib/pact_broker/webhooks/service.rb', line 21

def self.next_uuid
  SecureRandom.urlsafe_base64
end

.run_later(webhooks, pact, event_name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pact_broker/webhooks/service.rb', line 97

def self.run_later webhooks, pact, event_name
  trigger_uuid = next_uuid
  webhooks.each do | webhook |
    begin
      triggered_webhook = webhook_repository.create_triggered_webhook(trigger_uuid, webhook, pact, RESOURCE_CREATION)
      logger.info "Scheduling job for #{webhook.description} with uuid #{webhook.uuid}"
      # Bit of a dodgey hack to make sure the request transaction has finished before we execute the webhook
      Job.perform_in(5, triggered_webhook: triggered_webhook)
    rescue StandardError => e
      log_error e
    end
  end
end

.update_by_uuid(uuid, webhook) ⇒ Object



39
40
41
# File 'lib/pact_broker/webhooks/service.rb', line 39

def self.update_by_uuid uuid, webhook
  webhook_repository.update_by_uuid uuid, webhook
end

.update_triggered_webhook_status(triggered_webhook, status) ⇒ Object



79
80
81
# File 'lib/pact_broker/webhooks/service.rb', line 79

def self.update_triggered_webhook_status triggered_webhook, status
  webhook_repository.update_triggered_webhook_status triggered_webhook, status
end