Class: PactBroker::Webhooks::Service

Inherits:
Object
  • Object
show all
Extended by:
Messages, Repositories, Services
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

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 Services

badge_service, certificate_service, group_service, index_service, integration_service, label_service, matrix_service, metrics_service, pact_service, pacticipant_service, tag_service, verification_service, version_service, webhook_service, webhook_trigger_service

Methods included from Messages

message, potential_duplicate_pacticipant_message, validation_message

Methods included from Logging

included, #log_error

Class Method Details

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



41
42
43
# File 'lib/pact_broker/webhooks/service.rb', line 41

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


61
62
63
64
65
# File 'lib/pact_broker/webhooks/service.rb', line 61

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


67
68
69
# File 'lib/pact_broker/webhooks/service.rb', line 67

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



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

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



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

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

.execute_triggered_webhook_now(triggered_webhook, webhook_execution_configuration_hash) ⇒ Object



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

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

.fail_retrying_triggered_webhooksObject



142
143
144
# File 'lib/pact_broker/webhooks/service.rb', line 142

def self.fail_retrying_triggered_webhooks
  webhook_repository.fail_retrying_triggered_webhooks
end

.find_allObject



71
72
73
# File 'lib/pact_broker/webhooks/service.rb', line 71

def self.find_all
  webhook_repository.find_all
end

.find_by_consumer_and_or_provider(consumer, provider) ⇒ Object



101
102
103
# File 'lib/pact_broker/webhooks/service.rb', line 101

def self.find_by_consumer_and_or_provider consumer, provider
  webhook_repository.find_by_consumer_and_or_provider(consumer, provider)
end

.find_by_consumer_and_provider(consumer, provider) ⇒ Object



105
106
107
# File 'lib/pact_broker/webhooks/service.rb', line 105

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

.find_by_uuid(uuid) ⇒ Object



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

def self.find_by_uuid uuid
  webhook_repository.find_by_uuid uuid
end

.find_for_pact(pact) ⇒ Object



97
98
99
# File 'lib/pact_broker/webhooks/service.rb', line 97

def self.find_for_pact pact
  webhook_repository.find_for_pact(pact)
end

.find_latest_triggered_webhooks(consumer, provider) ⇒ Object



138
139
140
# File 'lib/pact_broker/webhooks/service.rb', line 138

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

.find_latest_triggered_webhooks_for_pact(pact) ⇒ Object



134
135
136
# File 'lib/pact_broker/webhooks/service.rb', line 134

def self.find_latest_triggered_webhooks_for_pact pact
  webhook_repository.find_latest_triggered_webhooks_for_pact pact
end

.find_triggered_webhooks_for_pact(pact) ⇒ Object



146
147
148
# File 'lib/pact_broker/webhooks/service.rb', line 146

def self.find_triggered_webhooks_for_pact pact
  webhook_repository.find_triggered_webhooks_for_pact(pact)
end

.find_triggered_webhooks_for_verification(verification) ⇒ Object



150
151
152
# File 'lib/pact_broker/webhooks/service.rb', line 150

def self.find_triggered_webhooks_for_verification verification
  webhook_repository.find_triggered_webhooks_for_verification(verification)
end

.next_uuidObject



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

def self.next_uuid
  SecureRandom.urlsafe_base64
end

.parametersObject



154
155
156
157
158
159
160
161
# File 'lib/pact_broker/webhooks/service.rb', line 154

def self.parameters
  PactAndVerificationParameters::ALL.collect do | parameter |
    OpenStruct.new(
      name: parameter,
      description: message("messages.webhooks.parameters.#{parameter}")
    )
  end
end

.run_later(webhooks, pact, verification, event_name, options) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/pact_broker/webhooks/service.rb', line 119

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)
      logger.info "Scheduling job for webhook with uuid #{webhook.uuid}"
      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
  end
end

.test_execution(webhook, execution_configuration) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pact_broker/webhooks/service.rb', line 75

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

.trigger_webhooks(pact, verification, event_name, options) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/pact_broker/webhooks/service.rb', line 109

def self.trigger_webhooks pact, verification, event_name, options
  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, options)
  else
    logger.info "No enabled webhooks found for consumer \"#{pact.consumer.name}\" and provider \"#{pact.provider.name}\" and event #{event_name}"
  end
end

.update_by_uuid(uuid, params) ⇒ Object



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

def self.update_by_uuid uuid, params
  webhook = webhook_repository.find_by_uuid(uuid)
  maintain_redacted_params(webhook, params)
  PactBroker::Api::Decorators::WebhookDecorator.new(webhook).from_hash(params)
  webhook_repository.update_by_uuid uuid, webhook
end

.update_triggered_webhook_status(triggered_webhook, status) ⇒ Object



93
94
95
# File 'lib/pact_broker/webhooks/service.rb', line 93

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