Class: PactBroker::Webhooks::EventListener

Inherits:
Object
  • Object
show all
Includes:
Events::Publisher, Logging, Services
Defined in:
lib/pact_broker/webhooks/event_listener.rb

Constant Summary

Constants included from Services

Services::FACTORIES

Instance Method Summary collapse

Methods included from Logging

included, #log_error, #log_with_tag

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

Constructor Details

#initialize(webhook_options) ⇒ EventListener

Returns a new instance of EventListener.



13
14
15
16
17
18
# File 'lib/pact_broker/webhooks/event_listener.rb', line 13

def initialize(webhook_options)
  @webhook_options = webhook_options
  # this has the base URL
  @base_webhook_context = webhook_options[:webhook_execution_configuration].webhook_context
  @detected_events = []
end

Instance Method Details

#contract_content_changed(params) ⇒ Object



28
29
30
31
# File 'lib/pact_broker/webhooks/event_listener.rb', line 28

def contract_content_changed(params)
  main_branch_verification = verification_service.find_latest_from_main_branch_for_pact(params.fetch(:pact))
  handle_event_for_webhook(PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED, { verification: main_branch_verification }.compact.merge(params))
end

#contract_content_unchanged(params) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/pact_broker/webhooks/event_listener.rb', line 33

def contract_content_unchanged(params)
  detected_events << PactBroker::Events::Event.new(
    "contract_content_unchanged",
    params[:event_comment],
    []
  )
  log_detected_event
end

#contract_published(params) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/pact_broker/webhooks/event_listener.rb', line 20

def contract_published(params)
  main_branch_verification = verification_service.find_latest_from_main_branch_for_pact(params.fetch(:pact))
  handle_event_for_webhook(PactBroker::Webhooks::WebhookEvent::CONTRACT_PUBLISHED, { verification: main_branch_verification }.compact.merge(params))
  if verification_service.calculate_required_verifications_for_pact(params.fetch(:pact)).any?
    handle_event_for_webhook(PactBroker::Webhooks::WebhookEvent::CONTRACT_REQUIRING_VERIFICATION_PUBLISHED, params)
  end
end

#log_detected_eventObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/pact_broker/webhooks/event_listener.rb', line 54

def log_detected_event
  event = detected_events.last
  logger.debug "Event detected", payload: { event_name: event.name, event_comment: event.comment }
  if event.triggered_webhooks&.any?
    triggered_webhook_descriptions = event.triggered_webhooks.collect{ |tw| { event_name: event.name, webhook_uuid: tw.webhook_uuid, triggered_webhook_uuid: tw.uuid, webhook_description: tw.webhook.description } }
    logger.info "Triggered webhooks for #{event.name}", payload: { triggered_webhooks: triggered_webhook_descriptions }
  else
    logger.debug "No enabled webhooks found for event #{event.name}"
  end
end

#provider_verification_failed(params) ⇒ Object



50
51
52
# File 'lib/pact_broker/webhooks/event_listener.rb', line 50

def provider_verification_failed(params)
  handle_event_for_webhook(PactBroker::Webhooks::WebhookEvent::VERIFICATION_FAILED, params)
end

#provider_verification_published(params) ⇒ Object



42
43
44
# File 'lib/pact_broker/webhooks/event_listener.rb', line 42

def provider_verification_published(params)
  handle_event_for_webhook(PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED, params)
end

#provider_verification_succeeded(params) ⇒ Object



46
47
48
# File 'lib/pact_broker/webhooks/event_listener.rb', line 46

def provider_verification_succeeded(params)
  handle_event_for_webhook(PactBroker::Webhooks::WebhookEvent::VERIFICATION_SUCCEEDED, params)
end

#schedule_triggered_webhooksObject



65
66
67
# File 'lib/pact_broker/webhooks/event_listener.rb', line 65

def schedule_triggered_webhooks
  webhook_trigger_service.schedule_webhooks(detected_events.flat_map(&:triggered_webhooks), webhook_options)
end