summaryrefslogtreecommitdiff
path: root/lib/oxidized/hook
diff options
context:
space:
mode:
authorNat Morris <nat@nuqe.net>2017-02-16 16:30:00 +0000
committerGitHub <noreply@github.com>2017-02-16 16:30:00 +0000
commit04e1f3e5184a0ae7adce51607c2716a3f0c8c262 (patch)
tree359132d2a1975c08ba64cdc39d5977bcc132a10c /lib/oxidized/hook
parente2d583728543e0b46c21a8fad56674bbda544006 (diff)
slackdiff hook to post colourized diffs to a Slack channel
Diffstat (limited to 'lib/oxidized/hook')
-rw-r--r--lib/oxidized/hook/slackdiff.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/oxidized/hook/slackdiff.rb b/lib/oxidized/hook/slackdiff.rb
new file mode 100644
index 0000000..b37c9c4
--- /dev/null
+++ b/lib/oxidized/hook/slackdiff.rb
@@ -0,0 +1,30 @@
+require 'slack'
+
+class SlackDiff < Oxidized::Hook
+ def validate_cfg!
+ raise KeyError, 'hook.token is required' unless cfg.has_key?('token')
+ raise KeyError, 'hook.channel is required' unless cfg.has_key?('channel')
+ end
+
+ def run_hook(ctx)
+ if ctx.node
+ if ctx.event.to_s == "post_store"
+ log "Connecting to slack"
+ client = Slack::Client.new token: cfg.token
+ client.auth_test
+ log "Connected"
+ gitoutput = ctx.node.output.new
+ diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil
+ title = "#{ctx.node.name.to_s} #{ctx.node.group.to_s} #{ctx.node.model.class.name.to_s.downcase}"
+ log "Posting diff as snippet to #{cfg.channel}"
+ client.files_upload(channels: cfg.channel, as_user: true,
+ content: diff[:patch].lines.to_a[4..-1].join,
+ filetype: "diff",
+ title: title,
+ filename: "change"
+ )
+ log "Finished"
+ end
+ end
+ end
+end