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



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

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


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

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


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

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


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

def self.delete_all_webhook_related_objects_by_verification_ids verification_ids
  webhook_repository.delete_triggered_webhooks_by_verification_ids verification_ids
end

.delete_by_uuid(uuid) ⇒ Object



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

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

.errors(webhook, uuid = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pact_broker/webhooks/service.rb', line 40

def self.errors webhook, uuid = nil
  contract = PactBroker::Api::Contracts::WebhookContract.new(webhook)
  contract.validate(webhook.attributes)
  messages = contract.errors.messages

  if uuid && !valid_uuid_format?(uuid)
    messages["uuid"] = [message("errors.validation.invalid_webhook_uuid")]
  end

  OpenStruct.new(messages: messages, empty?: messages.empty?, any?: messages.any?)
end

.execute_triggered_webhook_now(triggered_webhook, webhook_execution_configuration_hash) ⇒ Object



102
103
104
105
106
# File 'lib/pact_broker/webhooks/service.rb', line 102

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



159
160
161
# File 'lib/pact_broker/webhooks/service.rb', line 159

def self.fail_retrying_triggered_webhooks
  webhook_repository.fail_retrying_triggered_webhooks
end

.find_allObject



86
87
88
# File 'lib/pact_broker/webhooks/service.rb', line 86

def self.find_all
  webhook_repository.find_all
end

.find_by_consumer_and_or_provider(consumer, provider) ⇒ Object



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

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



120
121
122
# File 'lib/pact_broker/webhooks/service.rb', line 120

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

.find_by_uuid(uuid) ⇒ Object



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

def self.find_by_uuid uuid
  webhook_repository.find_by_uuid uuid
end

.find_for_pact(pact) ⇒ Object



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

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

.find_latest_triggered_webhooks(consumer, provider) ⇒ Object



155
156
157
# File 'lib/pact_broker/webhooks/service.rb', line 155

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



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

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



163
164
165
# File 'lib/pact_broker/webhooks/service.rb', line 163

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

.find_triggered_webhooks_for_verification(verification) ⇒ Object



167
168
169
# File 'lib/pact_broker/webhooks/service.rb', line 167

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

.next_uuidObject



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

def self.next_uuid
  SecureRandom.urlsafe_base64
end

.parametersObject



171
172
173
174
175
176
177
178
# File 'lib/pact_broker/webhooks/service.rb', line 171

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/pact_broker/webhooks/service.rb', line 135

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, event_name)
      logger.info "Scheduling job for webhook with uuid #{webhook.uuid}"
      logger.debug "Schedule webhook with options #{options}"
      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
      logger.warn("Error scheduling webhook execution for webhook with uuid #{webhook.uuid}", e)
    end
  end
end

.test_execution(webhook, execution_configuration) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pact_broker/webhooks/service.rb', line 90

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



124
125
126
127
128
129
130
131
132
133
# File 'lib/pact_broker/webhooks/service.rb', line 124

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?
    webhook_execution_configuration = options.fetch(:webhook_execution_configuration).with_webhook_context(event_name: event_name)
    run_later(webhooks, pact, verification, event_name, options.merge(webhook_execution_configuration: webhook_execution_configuration))
  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



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

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



108
109
110
# File 'lib/pact_broker/webhooks/service.rb', line 108

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

.valid_uuid_format?(uuid) ⇒ Boolean

Not actually a UUID. Ah well.

Returns:

  • (Boolean)


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

def self.valid_uuid_format?(uuid)
  !!(uuid =~ /^[A-Za-z0-9_\-]{16,}$/)
end