Module: PactBroker::Webhooks::TriggerService

Extended by:
Repositories, Services, TriggerService
Includes:
Logging
Included in:
TriggerService
Defined in:
lib/pact_broker/webhooks/trigger_service.rb

Constant Summary collapse

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

Constants included from Services

Services::FACTORIES

Instance Method Summary collapse

Methods included from Repositories

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

Methods included from Services

badge_service, branch_service, certificate_service, contract_service, deployed_version_service, environment_service, get, group_service, index_service, integration_service, label_service, matrix_service, metrics_service, pact_service, pacticipant_service, register_default_services, register_service, released_version_service, tag_service, verification_service, version_service, webhook_service, webhook_trigger_service

Methods included from Logging

included, #log_error, #log_with_tag

Instance Method Details

#create_triggered_webhooks_for_event(pact, verification, event_name, event_context) ⇒ Object

the main entry point



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

def create_triggered_webhooks_for_event pact, verification, event_name, event_context
  webhooks = webhook_repository.find_webhooks_to_trigger(consumer: pact.consumer, provider: pact.provider, event_name: event_name)

  if webhooks.any?
    create_triggered_webhooks_for_webhooks(webhooks, pact, verification, event_name, event_context.merge(event_name: event_name))
  else
    []
  end
end

#execute_triggered_webhook_now(triggered_webhook, webhook_execution_configuration_hash) ⇒ Object



33
34
35
36
37
# File 'lib/pact_broker/webhooks/trigger_service.rb', line 33

def 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

#expand_events_for_required_verifications(event_name, pact, event_contexts) ⇒ Object



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

def expand_events_for_required_verifications(event_name, pact, event_contexts)
  if event_name == PactBroker::Webhooks::WebhookEvent::CONTRACT_REQUIRING_VERIFICATION_PUBLISHED
    required_verifications = verification_service.calculate_required_verifications_for_pact(pact)
    event_contexts.flat_map do | event_context |
      required_verifications.collect do | required_verification |
        event_context.merge(
          provider_version_number: required_verification.provider_version.number,
          provider_version_branch: provider_version_branch_for_required_verification(required_verification, ),
          provider_version_descriptions: required_verification.provider_version_descriptions.uniq
        )
      end
    end
  else
    event_contexts
  end
end

#next_uuidObject



16
17
18
# File 'lib/pact_broker/webhooks/trigger_service.rb', line 16

def next_uuid
  SecureRandom.uuid
end

#provider_version_branch_for_required_verification(required_verification) ⇒ Object



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

def provider_version_branch_for_required_verification(required_verification)
  required_verification.provider_version_selectors.find(&:latest_for_main_branch?)&.resolved_branch_name || required_verification.provider_version.branch_versions.last&.branch_name
end

#schedule_webhooks(triggered_webhooks, options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pact_broker/webhooks/trigger_service.rb', line 50

def schedule_webhooks(triggered_webhooks, options)
  triggered_webhooks.each_with_index do | triggered_webhook, i |
    logger.info "Scheduling job for webhook with uuid #{triggered_webhook.webhook.uuid}, context: #{triggered_webhook.event_context}"
    logger.debug "Schedule webhook with options #{options}"

    job_data = { triggered_webhook: triggered_webhook }.deep_merge(options)
    begin
      # Delay slightly to make sure the request transaction has finished before we execute the webhook
      Job.perform_in(5 + (i * 3), job_data)
    rescue StandardError => e
      logger.warn("Error scheduling webhook execution for webhook with uuid #{triggered_webhook&.webhook&.uuid}", e)
      nil
    end
  end
end

#test_execution(webhook, event_context, execution_configuration) ⇒ Object

TODO support currently deployed



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pact_broker/webhooks/trigger_service.rb', line 21

def test_execution webhook, event_context, 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, event_context.merge(event_name: "test"), merged_options)
end