Class: Harbor::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/harbor/notifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Notifier

Returns a new instance of Notifier.



9
10
11
12
13
14
# File 'lib/harbor/notifier.rb', line 9

def initialize(config)
  @hooks = config.setting("notifications", {})
  @slack_url = @hooks["slack_webhook_url"]
  @webhook_urls = Array(@hooks["webhook_urls"])
  @notify_on = Array(@hooks.fetch("notify_on", %w[deploy_success deploy_failed rollback_success rollback_failed auto_rollback]))
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/harbor/notifier.rb', line 16

def enabled?
  @slack_url || @webhook_urls.any?
end

#notify(event:, project:, destination: nil, message:, details: {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/harbor/notifier.rb', line 20

def notify(event:, project:, destination: nil, message:, details: {})
  return unless enabled?
  return unless @notify_on.include?(event)

  payload = {
    event: event,
    project: project,
    destination: destination,
    message: message,
    timestamp: Time.now.utc.iso8601,
    **details
  }

  send_slack(event, project, destination, message, details) if @slack_url
  send_webhooks(payload) if @webhook_urls.any?
rescue => e
  $stderr.puts "[harbor-notify] Error sending notification: #{e.message}"
end