diff --git a/app/services/foreman_openscap/notification_builder.rb b/app/services/foreman_openscap/notification_builder.rb new file mode 100644 index 000000000..f183963a5 --- /dev/null +++ b/app/services/foreman_openscap/notification_builder.rb @@ -0,0 +1,58 @@ +module ForemanOpenscap + class NotificationBuilder < ::UINotifications::RemoteExecutionJobs::BaseJobFinish + def deliver! + ::Notification.create!( + :audience => Notification::AUDIENCE_USER, + :notification_blueprint => blueprint, + :initiator => initiator, + :message => message, + :subject => subject, + :actions => { + :links => links + } + ) + end + + def blueprint + @blueprint ||= NotificationBlueprint.unscoped.find_by(:name => 'openscap_scan_succeeded') + end + + def hosts_count + @hosts_count ||= subject.template_invocations_hosts.size + end + + def message + UINotifications::StringParser.new(blueprint.message, { hosts_count: hosts_count }) + end + + def links + job_links + scap_links + end + + # TODO do this only if there's single host + # TODO also add link to policies dashboards for a given host + def scap_links + UINotifications::URLResolver.new( + subject.template_invocations_hosts.first, + { + :links => [{ + :path_method => :host_path, + :title => _('Scanned Host') + }] + } + ).actions[:links] + end + + def job_links + UINotifications::URLResolver.new( + subject, + { + :links => [{ + :path_method => :job_invocation_path, + :title => _('Job Details') + }] + } + ).actions[:links] + end + end +end diff --git a/db/seeds.d/50-notification_blueprints.rb b/db/seeds.d/50-notification_blueprints.rb new file mode 100644 index 000000000..3843e0ba2 --- /dev/null +++ b/db/seeds.d/50-notification_blueprints.rb @@ -0,0 +1,18 @@ +blueprints = [ + { + group: N_('Jobs'), + name: 'openscap_scan_succeeded', + message: N_("OpenSCAP scan on %{hosts_count} host(s) has finished successfully"), + level: 'success', + actions: + { + links: + [ + path_method: :job_invocation_path, + title: N_('Job Details') + ] + } + } +] + +blueprints.each { |blueprint| UINotifications::Seed.new(blueprint).configure } diff --git a/lib/foreman_openscap/engine.rb b/lib/foreman_openscap/engine.rb index 7fb7b574d..a189216c1 100644 --- a/lib/foreman_openscap/engine.rb +++ b/lib/foreman_openscap/engine.rb @@ -176,6 +176,11 @@ class Engine < ::Rails::Engine options[:host_action_button] = true end + # TODO specific version + if Gem::Version.new(ForemanRemoteExecution::VERSION) >= Gem::Version.new('1.0') + options[:notification_builder] = ForemanOpenscap::NotificationBuilder + end + RemoteExecutionFeature.register(:foreman_openscap_run_scans, N_("Run OpenSCAP scan"), options) end