Class: Cyclid::API::Plugins::Slack

Inherits:
Action show all
Defined in:
app/cyclid/plugins/action/slack.rb

Overview

Slack notification plugin

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Action

human_name, #prepare

Methods inherited from Base

author, get_config, homepage, human_name, license, register_plugin, set_config, version

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

Returns:

  • (Boolean)


111
112
113
# File 'app/cyclid/plugins/action/slack.rb', line 111

def config?
  true
end

.config_schemaObject

Config schema for the Slack plugin



129
130
131
132
133
134
135
136
137
# File 'app/cyclid/plugins/action/slack.rb', line 129

def config_schema
  schema = []
  schema << { name: 'webhook_url',
              type: 'string',
              description: 'Slack incoming webhook URL for your team',
              default: nil }

  return schema
end

.default_configObject

Default configuration for the Slack plugin



121
122
123
124
125
126
# File 'app/cyclid/plugins/action/slack.rb', line 121

def default_config
  config = {}
  config['webhook_url'] = nil

  return config
end

.metadataObject

Plugin metadata



98
99
100
101
102
103
# File 'app/cyclid/plugins/action/slack.rb', line 98

def self.
  super.merge!(version: Cyclid::Api::VERSION,
               license: 'Apache-2.0',
               author: 'Liqwyd Ltd.',
               homepage: 'http://docs.cyclid.io')
end

.update_config(current, new) ⇒ Object

Update the plugin configuration



116
117
118
# File 'app/cyclid/plugins/action/slack.rb', line 116

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_text = @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.expand_path(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: message_text }]
             else
               []
             end
    fields << { title: 'Information',
                value: context_text,
                short: false }

    note = { fallback: message_text || 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.message}\n"
    success = false
    rc = 0
  end

  [success, rc]
end