diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000000000000000000000000000000000000..9bec14fc6c785d04e973e94511e6839a1fc8b388 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,9 @@ +--- +AllCops: + NewCops: enable + +Metrics/AbcSize: + Enabled: false + +Style/Documentation: + Enabled: false diff --git a/app/models/concerns/foreman_notification_send/notification_extensions.rb b/app/models/concerns/foreman_notification_send/notification_extensions.rb index a573562253934573ebda252ef13e8228dc06ba72..a6b6254dd3852fa085d4c0e67c02d137649d02c1 100644 --- a/app/models/concerns/foreman_notification_send/notification_extensions.rb +++ b/app/models/concerns/foreman_notification_send/notification_extensions.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module ForemanNotificationSend module NotificationExtensions def self.prepended(base) @@ -8,20 +9,12 @@ module ForemanNotificationSend end def send_notification - if Setting[:notification_send_enable] - sender = SenderBase.create_sender( - backend: :matrix, - hs_url: Setting[:notification_send_target_url], - access_token: Setting[:notification_send_token], - room: Setting[:notification_send_target_room] - ) - sender.send_notification(self) - end + send_notification_fallback if Setting[:notification_send_enable] - #NotificationTarget.select { |target| target.should_send?(self) } - # .each { |target| target.send(self) } - rescue StandardError => ex - Foreman::Logging.exception "Failed to send notification #{self}", ex + # NotificationTarget.select { |target| target.should_send?(self) } + # .each { |target| target.send(self) } + rescue StandardError => e + Foreman::Logging.exception "Failed to send notification #{self}", e end def level_to_symbol @@ -39,18 +32,15 @@ module ForemanNotificationSend def to_html unless actions.empty? - action_str = '' - - (actions[:links] || []).each do |action| + munged = (actions[:links] || []).map do |action| next unless action.key?(:href) && action.key?(:title) url = action[:href] url = Setting[:foreman_url] + url unless action[:external] - action_str += ' | ' unless action_str.empty? - action_str += "<a href=\"#{url}\">#{action[:title]}</a>" + "<a href=\"#{url}\">#{action[:title]}</a>" end - action_str = "<br/>[ #{action_str} ]" + action_str = "<br/>[ #{munged.join ' | '} ]" end "<b>#{notification_blueprint.group}</b>:<br/>#{level_to_symbol} #{message}#{action_str}" @@ -58,20 +48,28 @@ module ForemanNotificationSend def to_markdown unless actions.empty? - action_str = '' - - (actions[:links] || []).each do |action| + munged = (actions[:links] || []).map do |action| next unless action.key?(:href) && action.key?(:title) url = action[:href] url = Setting[:foreman_url] + url unless action[:external] - action_str += ' | ' unless action_str.empty? - action_str += "[#{action[:title]}](#{url})" + "[#{action[:title]}](#{url})" end - action_str = " \n\\[ #{action_str}\\ ]" + action_str = " \n\\[ #{munged.join ' | '}\\ ]" end "**#{notification_blueprint.group}**:\n#{level_to_symbol} #{message}#{action_str}" end + + private + + def send_notification_fallback + SenderBase.create_sender( + backend: :matrix, + hs_url: Setting[:notification_send_target_url], + access_token: Setting[:notification_send_token], + room: Setting[:notification_send_target_room] + ).send_notification(self) + end end end diff --git a/app/models/foreman_notification_send/notification_target.rb b/app/models/foreman_notification_send/notification_target.rb index 01e3c5947c54d6be744caa230bc0e29bc9073681..375682bcf306415239b23cf48fd225bab68ba72a 100644 --- a/app/models/foreman_notification_send/notification_target.rb +++ b/app/models/foreman_notification_send/notification_target.rb @@ -3,7 +3,7 @@ module ForemanNotificationSend class NotificationTarget < ApplicationRecord before_validation do - backend = slugify_backend if backend + # backend = slugify_backend if backend end validate :validate_backend diff --git a/app/services/foreman_notification_send/sender_matrix.rb b/app/services/foreman_notification_send/sender_matrix.rb index 06bb522b860b5bc96b9b1a6eb05f14136a23f0ce..a5e6bf84696cbd07ff9b1593d11c5a13b2d9cc5d 100644 --- a/app/services/foreman_notification_send/sender_matrix.rb +++ b/app/services/foreman_notification_send/sender_matrix.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require 'matrix_sdk/api' module ForemanNotificationSend diff --git a/db/migrate/20200414141644_add_notification_targets.rb b/db/migrate/20200414141644_add_notification_targets.rb index 268bf015b70ee1c81820076142038f9c6ecb7dbf..081d33195da6c52bde8d1cf963a981c4a712d909 100644 --- a/db/migrate/20200414141644_add_notification_targets.rb +++ b/db/migrate/20200414141644_add_notification_targets.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddNotificationTargets < ActiveRecord::Migration[5.1] def change create_table :notification_targets do |t| @@ -11,4 +13,3 @@ class AddNotificationTargets < ActiveRecord::Migration[5.1] end end end - diff --git a/lib/foreman_notification_send.rb b/lib/foreman_notification_send.rb index d56ba1a05644fe5fa05d4f09d71763d612c49328..80cd19abfdaba96402f8d03ac94c2baa924697ee 100644 --- a/lib/foreman_notification_send.rb +++ b/lib/foreman_notification_send.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'foreman_notification_send/engine' module ForemanNotificationSend diff --git a/lib/foreman_notification_send/version.rb b/lib/foreman_notification_send/version.rb index dcf95fb2cd768cf89fdad2c08a4c8ff7750e4e40..71d2953611ef0d2ce82e1980959a2d7186734c95 100644 --- a/lib/foreman_notification_send/version.rb +++ b/lib/foreman_notification_send/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ForemanNotificationSend - VERSION = '0.1.0'.freeze + VERSION = '0.1.0' end