From a26a1f78a71a03f5de7bbeee11a87d57be6a5343 Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Sun, 15 Apr 2018 08:58:25 +0200 Subject: massage into rubocop compliance --- lib/oxidized/hook/ciscosparkdiff.rb | 66 ++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 34 deletions(-) (limited to 'lib/oxidized/hook') diff --git a/lib/oxidized/hook/ciscosparkdiff.rb b/lib/oxidized/hook/ciscosparkdiff.rb index 12094e6..d43ff81 100644 --- a/lib/oxidized/hook/ciscosparkdiff.rb +++ b/lib/oxidized/hook/ciscosparkdiff.rb @@ -11,41 +11,39 @@ class CiscoSparkDiff < Oxidized::Hook end def run_hook(ctx) - if ctx.node - if ctx.event.to_s == "post_store" - log "Connecting to Cisco Spark" - CiscoSpark.configure do |config| - config.api_key = cfg.accesskey - config.proxy = cfg.proxy if cfg.has_key?('proxy') - end - space = cfg.space - client = CiscoSpark::Room.new(id: space) - client.fetch - log "Connected" - 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}" - log "Posting diff as snippet to #{cfg.space}" - message = CiscoSpark::Message.new(text: 'Device ' + title + ' modified:' + "\n" + diff[:patch].lines.to_a[4..-1].join) - room = CiscoSpark::Room.new(id: space) - room.send_message(message) - end - 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.space}" - 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 Cisco Spark" + CiscoSpark.configure do |config| + config.api_key = cfg.accesskey + config.proxy = cfg.proxy if cfg.has_key?('proxy') + end + space = cfg.space + client = CiscoSpark::Room.new(id: space) + client.fetch + log "Connected" + 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 + log "Posting diff as snippet to #{cfg.space}" + message = CiscoSpark::Message.new(text: 'Device ' + title + ' modified:' + "\n" + diff[:patch].lines.to_a[4..-1].join) + room = CiscoSpark::Room.new(id: space) + room.send_message(message) + end + 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.space}" + client.chat_postMessage(channel: cfg.channel, text: msg, as_user: true) + end + log "Finished" end end -- cgit v1.2.1 From cc5c846b3b88cafd8c01c645e6ada3bab714ae91 Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Sun, 15 Apr 2018 13:08:53 +0200 Subject: restructure xmppdiff.rb to comply with rubocop --- lib/oxidized/hook/xmppdiff.rb | 86 +++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 44 deletions(-) (limited to 'lib/oxidized/hook') diff --git a/lib/oxidized/hook/xmppdiff.rb b/lib/oxidized/hook/xmppdiff.rb index 396d1b3..52cc0e0 100644 --- a/lib/oxidized/hook/xmppdiff.rb +++ b/lib/oxidized/hook/xmppdiff.rb @@ -7,54 +7,52 @@ class XMPPDiff < Oxidized::Hook raise KeyError, 'hook.password is required' unless cfg.has_key?('password') raise KeyError, 'hook.channel is required' unless cfg.has_key?('channel') raise KeyError, 'hook.nick is required' unless cfg.has_key?('nick') - end + end def run_hook(ctx) - if ctx.node - if ctx.event.to_s == "post_store" - begin - Timeout::timeout(15) do - gitoutput = ctx.node.output.new - diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil - - interesting = diff[:patch].lines.to_a[4..-1].any? { |line| - ["+", "-"].include?(line[0]) and not ["#", "!"].include?(line[1]) - } - interesting &&= diff[:patch].lines.to_a[5..-1].any? { |line| line[0] == '-' } - interesting &&= diff[:patch].lines.to_a[5..-1].any? { |line| line[0] == '+' } - - if interesting - log "Connecting to XMPP" - client = Jabber::Client.new(Jabber::JID.new(cfg.jid)) - client.connect - sleep 1 - client.auth(cfg.password) - sleep 1 - - log "Connected" - - m = Jabber::MUC::SimpleMUCClient.new(client) - m.join(cfg.channel + "/" + cfg.nick) - - log "Joined" - - 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}" - - m.say(title + "\n\n" + diff[:patch].lines.to_a[4..-1].join) - - sleep 1 - - client.close - - log "Finished" - - end - end - rescue Timeout::Error - log "timed out" + return unless ctx.node + return unless ctx.event.to_s == "post_store" + begin + Timeout.timeout(15) do + gitoutput = ctx.node.output.new + diff = gitoutput.get_diff ctx.node, ctx.node.group, ctx.commitref, nil + + interesting = diff[:patch].lines.to_a[4..-1].any? do |line| + ["+", "-"].include?(line[0]) and not ["#", "!"].include?(line[1]) + end + interesting &&= diff[:patch].lines.to_a[5..-1].any? { |line| line[0] == '-' } + interesting &&= diff[:patch].lines.to_a[5..-1].any? { |line| line[0] == '+' } + + if interesting + log "Connecting to XMPP" + client = Jabber::Client.new(Jabber::JID.new(cfg.jid)) + client.connect + sleep 1 + client.auth(cfg.password) + sleep 1 + + log "Connected" + + m = Jabber::MUC::SimpleMUCClient.new(client) + m.join(cfg.channel + "/" + cfg.nick) + + log "Joined" + + title = "#{ctx.node.name} #{ctx.node.group} #{ctx.node.model.class.name.to_s.downcase}" + log "Posting diff as snippet to #{cfg.channel}" + + m.say(title + "\n\n" + diff[:patch].lines.to_a[4..-1].join) + + sleep 1 + + client.close + + log "Finished" + end end + rescue Timeout::Error + log "timed out" end end end -- cgit v1.2.1 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 From 1b66348be97d4603566a262756d6ad5a2d21bd58 Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Mon, 16 Apr 2018 21:03:35 +0200 Subject: restructure awssns.rb to comply with rubocop --- lib/oxidized/hook/awssns.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/oxidized/hook') diff --git a/lib/oxidized/hook/awssns.rb b/lib/oxidized/hook/awssns.rb index dbc2d47..82c118e 100644 --- a/lib/oxidized/hook/awssns.rb +++ b/lib/oxidized/hook/awssns.rb @@ -19,9 +19,9 @@ class AwsSns < Oxidized::Hook :node => ctx.node.name.to_s ) end - topic.publish({ + topic.publish( message: message.to_json - }) + ) end end -- cgit v1.2.1 From 21e3d6490496573f25ef77fe8172766ac7d1a736 Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Sat, 21 Apr 2018 13:27:05 +0200 Subject: the great makeover - standardize layout, alignment, indentation --- lib/oxidized/hook/awssns.rb | 1 - lib/oxidized/hook/exec.rb | 5 ++--- lib/oxidized/hook/githubrepo.rb | 13 ++++++------- lib/oxidized/hook/slackdiff.rb | 6 +++--- lib/oxidized/hook/xmppdiff.rb | 18 +++++++++--------- 5 files changed, 20 insertions(+), 23 deletions(-) (limited to 'lib/oxidized/hook') diff --git a/lib/oxidized/hook/awssns.rb b/lib/oxidized/hook/awssns.rb index 82c118e..183cd2c 100644 --- a/lib/oxidized/hook/awssns.rb +++ b/lib/oxidized/hook/awssns.rb @@ -23,5 +23,4 @@ class AwsSns < Oxidized::Hook message: message.to_json ) end - end diff --git a/lib/oxidized/hook/exec.rb b/lib/oxidized/hook/exec.rb index 3f984c2..8a32412 100644 --- a/lib/oxidized/hook/exec.rb +++ b/lib/oxidized/hook/exec.rb @@ -23,10 +23,9 @@ class Exec < Oxidized::Hook @cmd = cfg.cmd raise "invalid cmd value" unless @cmd.is_a?(String) || @cmd.is_a?(Array) end - rescue RuntimeError => e raise ArgumentError, - "#{self.class.name}: configuration invalid: #{e.message}" + "#{self.class.name}: configuration invalid: #{e.message}" end def run_hook ctx @@ -45,7 +44,7 @@ class Exec < Oxidized::Hook def run_cmd! env pid, status = nil, nil Timeout.timeout(@timeout) do - pid = spawn env, @cmd , :unsetenv_others => true + pid = spawn env, @cmd, :unsetenv_others => true pid, status = wait2 pid unless status.exitstatus.zero? msg = "#{@cmd.inspect} failed with exit value #{status.exitstatus}" diff --git a/lib/oxidized/hook/githubrepo.rb b/lib/oxidized/hook/githubrepo.rb index 4cae4e6..e077d5d 100644 --- a/lib/oxidized/hook/githubrepo.rb +++ b/lib/oxidized/hook/githubrepo.rb @@ -35,21 +35,20 @@ class GithubRepo < Oxidized::Hook end Rugged::Commit.create(repo, { - parents: [repo.head.target, their_branch.target], - tree: merge_index.write_tree(repo), - message: "Merge remote-tracking branch '#{their_branch.name}'", - update_ref: "HEAD" - }) + parents: [repo.head.target, their_branch.target], + tree: merge_index.write_tree(repo), + message: "Merge remote-tracking branch '#{their_branch.name}'", + update_ref: "HEAD" + }) end private def credentials Proc.new do |url, username_from_url, allowed_types| - if cfg.has_key?('username') git_user = cfg.username - else + else git_user = username_from_url ? username_from_url : 'git' end diff --git a/lib/oxidized/hook/slackdiff.rb b/lib/oxidized/hook/slackdiff.rb index e271d5f..2c5ec14 100644 --- a/lib/oxidized/hook/slackdiff.rb +++ b/lib/oxidized/hook/slackdiff.rb @@ -14,8 +14,8 @@ class SlackDiff < Oxidized::Hook 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') + config.token = cfg.token + config.proxy = cfg.proxy if cfg.has_key?('proxy') end client = Slack::Client.new client.auth_test @@ -46,7 +46,7 @@ class SlackDiff < Oxidized::Hook 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) + client.chat_postMessage(channel: cfg.channel, text: msg, as_user: true) end log "Finished" end diff --git a/lib/oxidized/hook/xmppdiff.rb b/lib/oxidized/hook/xmppdiff.rb index 52cc0e0..6acb172 100644 --- a/lib/oxidized/hook/xmppdiff.rb +++ b/lib/oxidized/hook/xmppdiff.rb @@ -30,25 +30,25 @@ class XMPPDiff < Oxidized::Hook sleep 1 client.auth(cfg.password) sleep 1 - + log "Connected" - + m = Jabber::MUC::SimpleMUCClient.new(client) m.join(cfg.channel + "/" + cfg.nick) - + log "Joined" - + title = "#{ctx.node.name} #{ctx.node.group} #{ctx.node.model.class.name.to_s.downcase}" log "Posting diff as snippet to #{cfg.channel}" - + m.say(title + "\n\n" + diff[:patch].lines.to_a[4..-1].join) - + sleep 1 - + client.close - + log "Finished" - + end end rescue Timeout::Error -- cgit v1.2.1 From 5e31f4bc028f0e0bd6aade771fe10a3cc6d2a5fa Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Wed, 25 Apr 2018 18:43:23 +0200 Subject: fine tune rubocop to yttis exacting specifications --- lib/oxidized/hook/slackdiff.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/oxidized/hook') diff --git a/lib/oxidized/hook/slackdiff.rb b/lib/oxidized/hook/slackdiff.rb index 2c5ec14..baaf291 100644 --- a/lib/oxidized/hook/slackdiff.rb +++ b/lib/oxidized/hook/slackdiff.rb @@ -43,7 +43,7 @@ class SlackDiff < Oxidized::Hook # 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) + 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) -- cgit v1.2.1 From 83a00943d7333fafdfe352173713a22a5ac89f6f Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Fri, 27 Apr 2018 12:39:07 +0200 Subject: bring ciscosparkdiff.rb back into compliance with rubocop --- lib/oxidized/hook/ciscosparkdiff.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/oxidized/hook') diff --git a/lib/oxidized/hook/ciscosparkdiff.rb b/lib/oxidized/hook/ciscosparkdiff.rb index d43ff81..e45d7c6 100644 --- a/lib/oxidized/hook/ciscosparkdiff.rb +++ b/lib/oxidized/hook/ciscosparkdiff.rb @@ -15,8 +15,8 @@ class CiscoSparkDiff < Oxidized::Hook return unless ctx.event.to_s == "post_store" log "Connecting to Cisco Spark" CiscoSpark.configure do |config| - config.api_key = cfg.accesskey - config.proxy = cfg.proxy if cfg.has_key?('proxy') + config.api_key = cfg.accesskey + config.proxy = cfg.proxy if cfg.has_key?('proxy') end space = cfg.space client = CiscoSpark::Room.new(id: space) @@ -39,10 +39,10 @@ class CiscoSparkDiff < Oxidized::Hook end 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) + 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.space}" - client.chat_postMessage(channel: cfg.channel, text: msg, as_user: true) + client.chat_postMessage(channel: cfg.channel, text: msg, as_user: true) end log "Finished" end -- cgit v1.2.1