class PuppetWebhook::Chatops::Rocketchat

Sets up Rocketchat object that will send notifications to Slack via a webhook.

Public Class Methods

new(channel, url, user, message, options = {}) click to toggle source
# File lib/plugins/chatops/rocketchat.rb, line 7
def initialize(channel, url, user, message, options = {})
  @channel = channel
  @url = url
  @user = user
  @message = message
  @options = options
end

Public Instance Methods

notify() click to toggle source
# File lib/plugins/chatops/rocketchat.rb, line 15
def notify
  notifier = RocketChat::Notifier.new @url, http_options: @options[:http_options]
  notifier.username = @user
  notifier.channel = @channel

  target = if @message[:branch]
             @message[:branch]
           elsif @message[:module]
             @message[:module]
           end

  msg = "r10k deployment of Puppet environment/module #{target} started..."

  attachment = format_attachment(target)

  notifier.ping(msg, icon_emoji: @options[:icon_emoji], attachments: [attachment])
end

Private Instance Methods

format_attachment(target) click to toggle source
# File lib/plugins/chatops/rocketchat.rb, line 35
def format_attachment(target)
  attachment = {
    author: 'r10k for Puppet',
    title: "r10k deployment of Puppet environment #{target}"
  }

  case @message[:status_code]
  when 200
    attachment.merge!(
      color: 'good',
      text: "Successfully started deployment of #{target}",
      fallback: "Successfully started deployment of #{target}"
    )
  when 500
    attachment.merge!(
      color: 'bad',
      text: "Failed to deploy #{target}",
      fallback: "Failed to deploy #{target}"
    )
  end

  attachment
end