From aa0e1808f25e5e8b1a76f34c29f2eeb7db75a7c7 Mon Sep 17 00:00:00 2001 From: Nat Morris Date: Thu, 9 Nov 2017 22:14:37 +0000 Subject: feature: Slack hook - optionally disable diff snippets and post custom messages (#763) * continue to diffs by default, but optionally post formatted messages * updated readme * remove static channel name * Update Hooks.md --- lib/oxidized/hook/slackdiff.rb | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'lib/oxidized/hook/slackdiff.rb') diff --git a/lib/oxidized/hook/slackdiff.rb b/lib/oxidized/hook/slackdiff.rb index 61f1743..728e5a7 100644 --- a/lib/oxidized/hook/slackdiff.rb +++ b/lib/oxidized/hook/slackdiff.rb @@ -1,5 +1,8 @@ require 'slack' +# defaults to posting a diff, if messageformat is supplied them a message will be posted too +# diffenable defaults to true + class SlackDiff < Oxidized::Hook def validate_cfg! raise KeyError, 'hook.token is required' unless cfg.has_key?('token') @@ -17,16 +20,33 @@ class SlackDiff < Oxidized::Hook client = Slack::Client.new 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" - ) + # diff snippet - default + diffenable = true + if cfg.has_key?('diff') == true + if cfg.diff == false + diffenable = false + end + end + if diffenable == true + 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" + ) + end + # message custom formatted - optional + if cfg.has_key?('message') == true + log cfg.message + msg = cfg.message % {:node => ctx.node.name.to_s, :group => ctx.node.group.to_s, :commitref => ctx.commitref, :model => ctx.node.model.class.name.to_s.downcase} + log msg + log "Posting message to #{cfg.channel}" + client.chat_postMessage(channel: cfg.channel, text: msg, as_user: true) + end log "Finished" end end -- cgit v1.2.1 From d6456bf490e71c7e3f453497641ff5c7952a018e Mon Sep 17 00:00:00 2001 From: Adam Smith Date: Thu, 25 Jan 2018 14:11:07 -0800 Subject: fix: Don't post diff to Slack if there are no diffs (#1151) --- lib/oxidized/hook/slackdiff.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'lib/oxidized/hook/slackdiff.rb') diff --git a/lib/oxidized/hook/slackdiff.rb b/lib/oxidized/hook/slackdiff.rb index 728e5a7..7cd4465 100644 --- a/lib/oxidized/hook/slackdiff.rb +++ b/lib/oxidized/hook/slackdiff.rb @@ -30,14 +30,16 @@ class SlackDiff < Oxidized::Hook if diffenable == true 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" - ) + unless diff == "no diffs" + 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" + ) + end end # message custom formatted - optional if cfg.has_key?('message') == true -- cgit v1.2.1