From 140954f78e50bed1aaf3f8fd42f849e3892e55c0 Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Sun, 15 Apr 2018 13:24:05 +0200 Subject: restructure slackdiff.rb to comply with rubocop --- lib/oxidized/hook/slackdiff.rb | 77 ++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 40 deletions(-) (limited to 'lib/oxidized/hook') diff --git a/lib/oxidized/hook/slackdiff.rb b/lib/oxidized/hook/slackdiff.rb index 7cd4465..e271d5f 100644 --- a/lib/oxidized/hook/slackdiff.rb +++ b/lib/oxidized/hook/slackdiff.rb @@ -10,47 +10,44 @@ class SlackDiff < Oxidized::Hook end def run_hook(ctx) - if ctx.node - if ctx.event.to_s == "post_store" - log "Connecting to slack" - Slack.configure do |config| - config.token = cfg.token - config.proxy = cfg.proxy if cfg.has_key?('proxy') - end - client = Slack::Client.new - client.auth_test - log "Connected" - # 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 - 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 - 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" + return unless ctx.node + return unless ctx.event.to_s == "post_store" + log "Connecting to slack" + Slack.configure do |config| + config.token = cfg.token + config.proxy = cfg.proxy if cfg.has_key?('proxy') + end + client = Slack::Client.new + client.auth_test + log "Connected" + # 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 + unless diff == "no diffs" + title = "#{ctx.node.name} #{ctx.node.group} #{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 + log cfg.message + msg = format(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