This repository has been archived by the owner on Jan 29, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from dhollinger/slack_as_plugin
ChatOps Plugin Support
- Loading branch information
Showing
8 changed files
with
270 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'plugins/chatops/slack' | ||
|
||
class PuppetWebhook | ||
# Chatops object for sending webhook notifications to chatops tools | ||
class Chatops | ||
def initialize(service, url, channel, user, options = {}) | ||
@service = service | ||
@url = url | ||
@channel = channel | ||
@user = user | ||
@args = options | ||
end | ||
|
||
def notify(message) | ||
case @service | ||
when 'slack' | ||
LOGGER.info("Sending Slack webhook message to #{@url}") | ||
Chatops::Slack.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 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'slack-notifier' | ||
|
||
class PuppetWebhook | ||
class Chatops | ||
# Sets up Slack object that will send notifications to Slack via a webhook. | ||
class Slack | ||
def initialize(channel, url, user, message, options = {}) | ||
@channel = channel | ||
@url = url | ||
@user = user | ||
@message = message | ||
@options = options | ||
end | ||
|
||
def notify | ||
notifier = ::Slack::Notifier.new @url, http_options: @options[:http_options] | ||
|
||
target = if @message[:branch] | ||
@message[:branch] | ||
elsif @message[:module] | ||
@message[:module] | ||
end | ||
|
||
msg = format_message(target) | ||
|
||
notifier.post text: msg[:fallback], | ||
channel: @channel, | ||
username: @user, | ||
icon_emoji: @options[:icon_emoji], | ||
attachments: [msg] | ||
end | ||
|
||
private | ||
|
||
def format_message(target) | ||
message = { | ||
author: 'r10k for Puppet', | ||
title: "r10k deployment of Puppet environment #{target}" | ||
} | ||
|
||
case @message[:status_code] | ||
when 202 | ||
message.merge!( | ||
color: 'good', | ||
text: "Successfully started deployment of #{target}", | ||
fallback: "Successfully started deployment of #{target}" | ||
) | ||
when 500 | ||
message.merge!( | ||
color: 'bad', | ||
text: "Failed to deploy #{target}", | ||
fallback: "Failed to deploy #{target}" | ||
) | ||
end | ||
|
||
message | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.