diff --git a/lib/plugins/chatops.rb b/lib/plugins/chatops.rb index f7fbffa..b641ece 100644 --- a/lib/plugins/chatops.rb +++ b/lib/plugins/chatops.rb @@ -21,6 +21,14 @@ def notify(message) message, http_options: @args[:http_options] || {}, icon_emoji: @args[:icon_emoji]).notify + when 'rocketchat' + LOGGER.info("Sending Rocket.Chat webhook message to #{@url}") + Chatops::Rocketchat.new(@channel, + @url, + @user, + message, + http_options: @args[:http_options] || {}, + icon_emoji: @args[:icon_emoji]).notify else LOGGER.error("Service #{@service} is not currently supported") end diff --git a/lib/plugins/chatops/rocketchat.rb b/lib/plugins/chatops/rocketchat.rb new file mode 100644 index 0000000..281e63b --- /dev/null +++ b/lib/plugins/chatops/rocketchat.rb @@ -0,0 +1,60 @@ +require 'rocket-chat-notifier' + +class PuppetWebhook + class Chatops + class Rocketchat + def initialize(channel, url, user, message, options = {}) + @channel = channel + @url = url + @user = user + @message = message + @options = options + end + + 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 + + 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 deployed #{target}", + fallback: "Successfully deployed #{target}", + ) + when 500 + attachment.merge!( + color: "bad", + text: "Failed to deploy #{target}", + fallback: "Failed to deploy #{target}", + ) + end + + attachment + end + + end + end +end