Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] - add custom notification for scan jobs #326

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions app/services/foreman_openscap/notification_builder.rb
Original file line number Diff line number Diff line change
@@ -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,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

:title => _('Scanned Host')
}]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent the right brace the same as the start of the line where the left brace is.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent the right brace the same as the start of the line where the left brace is.

}
).actions[:links]
end

def job_links
UINotifications::URLResolver.new(
subject,
{
:links => [{
:path_method => :job_invocation_path,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

:title => _('Job Details')
}]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent the right brace the same as the start of the line where the left brace is.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent the right brace the same as the start of the line where the left brace is.

}
).actions[:links]
end
end
end
18 changes: 18 additions & 0 deletions db/seeds.d/50-notification_blueprints.rb
Original file line number Diff line number Diff line change
@@ -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 }
5 changes: 5 additions & 0 deletions lib/foreman_openscap/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down