Class: Cyclid::API::Plugins::Slack
- Defined in:
- app/cyclid/plugins/action/slack.rb
Overview
Slack notification plugin
Class Method Summary collapse
-
.config? ⇒ Boolean
This plugin has configuration data.
-
.config_schema ⇒ Object
Config schema for the Slack plugin.
-
.default_config ⇒ Object
Default configuration for the Slack plugin.
-
.update_config(current, new) ⇒ Object
Update the plugin configuration.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Slack
constructor
A new instance of Slack.
-
#perform(log) ⇒ Object
Send a Slack notification to the configured endpoint; the message is rendered via.
Methods inherited from Action
Methods inherited from Base
get_config, human_name, register_plugin, set_config
Constructor Details
#initialize(args = {}) ⇒ Slack
Returns a new instance of Slack.
26 27 28 29 30 31 32 33 34 35 |
# File 'app/cyclid/plugins/action/slack.rb', line 26 def initialize(args = {}) args.symbolize_keys! raise 'a slack action requires a subject' unless args.include? :subject @subject = args[:subject] @url = args[:url] if args.include? :url @color = args[:color] || 'good' @message = args[:message] if args.include? :message end |
Class Method Details
.config? ⇒ Boolean
This plugin has configuration data
103 104 105 |
# File 'app/cyclid/plugins/action/slack.rb', line 103 def config? true end |
.config_schema ⇒ Object
Config schema for the Slack plugin
121 122 123 124 125 126 127 128 129 |
# File 'app/cyclid/plugins/action/slack.rb', line 121 def config_schema schema = [] schema << { name: 'webhook_url', type: 'string', description: 'Slack incoming webhook URL for your team', default: nil } return schema end |
.default_config ⇒ Object
Default configuration for the Slack plugin
113 114 115 116 117 118 |
# File 'app/cyclid/plugins/action/slack.rb', line 113 def default_config config = {} config['webhook_url'] = nil return config end |
.update_config(current, new) ⇒ Object
Update the plugin configuration
108 109 110 |
# File 'app/cyclid/plugins/action/slack.rb', line 108 def update_config(current, new) current.merge! new end |
Instance Method Details
#perform(log) ⇒ Object
Send a Slack notification to the configured endpoint; the message is rendered via. an ERB template which inserts additional information from the context and is attached as a Slack message note
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/cyclid/plugins/action/slack.rb', line 40 def perform(log) begin plugin_data = self.class.get_config(@ctx[:organization]) Cyclid.logger.debug "using plugin config #{plugin_data}" config = plugin_data['config'] subject = @subject ** @ctx url = @url || config['webhook_url'] raise 'no webhook URL given' if url.nil? url = url ** @ctx Cyclid.logger.debug "sending notification to #{url}" = @message ** @ctx if @message # Create a binding for the template bind = binding bind.local_variable_set(:ctx, @ctx) # Generate the context information from a templete template_path = File.(File.join(__FILE__, '..', 'slack', 'note.erb')) template = ERB.new(File.read(template_path), nil, '%<>-') context_text = template.result(bind) # Create a "note" and send it as part of the message fields = if @message [{ title: 'Message', value: }] else [] end fields << { title: 'Information', value: context_text, short: false } note = { fallback: || subject, color: @color, fields: fields } # Send the notification to the Slack webhook notifier = ::Slack::Notifier.new url notifier.username = 'Cyclid' res = notifier.ping subject, attachments: [note] rc = res.code success = rc == '200' rescue StandardError => ex log.write "#{ex.}\n" success = false rc = 0 end [success, rc] end |