Class: Patches::Notifier

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

Class Method Summary collapse

Class Method Details

.append_tenant_message(message) ⇒ Object



30
31
32
33
# File 'lib/patches/notifier.rb', line 30

def append_tenant_message(message)
  message = message + " for tenant: #{Apartment::Tenant.current}" if defined?(Apartment)
  message
end

.environment_prefixObject



26
27
28
# File 'lib/patches/notifier.rb', line 26

def environment_prefix
  "[#{Rails.env.upcase}] " if defined?(Rails)
end

.failure_message(patch_path, error) ⇒ Object



20
21
22
23
24
# File 'lib/patches/notifier.rb', line 20

def failure_message(patch_path, error)
  details = "#{Pathname.new(patch_path).basename} failed with error: #{error}"
  message = "#{environment_prefix}Error applying patch: #{details}"
  append_tenant_message(message)
end

.notify_failure(patch_path, error) ⇒ Object



10
11
12
13
# File 'lib/patches/notifier.rb', line 10

def notify_failure(patch_path, error)
  send_hipchat_message(failure_message(patch_path, error), color: 'red')
  send_slack_message(failure_message(patch_path, error), 'danger')
end

.notify_success(patches) ⇒ Object



5
6
7
8
# File 'lib/patches/notifier.rb', line 5

def notify_success(patches)
  send_hipchat_message(success_message(patches), color: 'green')
  send_slack_message(success_message(patches), 'good')
end

.send_hipchat_message(message, options) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/patches/notifier.rb', line 35

def send_hipchat_message(message, options)
  return unless defined?(HipChat) && config.use_hipchat

  client = HipChat::Client.new(config.hipchat_api_token, config.hipchat_init_options)
  room = client[config.hipchat_room]
  room.send(config.hipchat_user, message, options)
end

.send_slack_message(message, color) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/patches/notifier.rb', line 43

def send_slack_message(message, color)
  return unless defined?(Slack) && config.use_slack

  notifier = Slack::Notifier.new(
    config.slack_webhook_url,
    channel: config.slack_channel,
    username: config.slack_username)

  payload = { attachments: [{ color: color, text: message }] }

  notifier.post payload
end

.success_message(patches) ⇒ Object



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

def success_message(patches)
  message = "#{environment_prefix}#{patches.count} patches succeeded"
  append_tenant_message(message)
end