Skip to content

Commit

Permalink
Fixes #22609 - customizable notifications per feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ares committed Feb 21, 2018
1 parent a221dd8 commit 96f9725
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
13 changes: 12 additions & 1 deletion app/models/job_invocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class JobInvocation < ApplicationRecord
belongs_to :triggering, :class_name => 'ForemanTasks::Triggering'
has_one :recurring_logic, :through => :triggering, :class_name => 'ForemanTasks::RecurringLogic'

belongs_to :remote_execution_feature

scope :with_task, -> { references(:task) }

scoped_search :relation => :recurring_logic, :on => 'id', :rename => 'recurring_logic.id'
Expand Down Expand Up @@ -83,7 +85,16 @@ def notification_recipients_ids
end

def build_notification
UINotifications::RemoteExecutionJobs::BaseJobFinish.new(self)
klass = nil
if self.remote_execution_feature && self.remote_execution_feature.notification_builder.present?
begin
klass = remote_execution_feature.notification_builder.constantize
rescue NameError => e
logger.exception "REX feature defines unknown notification builder class", e
end
end
klass ||= UINotifications::RemoteExecutionJobs::BaseJobFinish
klass.new(self)
end

def status
Expand Down
7 changes: 6 additions & 1 deletion app/models/job_invocation_composer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def params
:targeting => ui_params.fetch(:targeting, {}).merge(:user_id => User.current.id),
:triggering => triggering,
:host_ids => ui_params[:host_ids],
:remote_execution_feature_id => ui_params[:remote_execution_feature_id],
:description_format => job_invocation_base[:description_format],
:concurrency_control => concurrency_control_params,
:execution_timeout_interval => execution_timeout_interval,
Expand Down Expand Up @@ -88,6 +89,7 @@ def params
:targeting => targeting_params,
:triggering => triggering_params,
:description_format => api_params[:description_format],
:remote_execution_feature_id => api_params[:remote_execution_feature_id],
:concurrency_control => concurrency_control_params,
:execution_timeout_interval => api_params[:execution_timeout_interval] || template.execution_timeout_interval,
:template_invocations => template_invocations_params }.with_indifferent_access
Expand Down Expand Up @@ -172,6 +174,7 @@ def params
:description_format => job_invocation.description_format,
:concurrency_control => concurrency_control_params,
:execution_timeout_interval => job_invocation.execution_timeout_interval,
:remote_execution_feature_id => job_invocation.remote_execution_feature_id,
:template_invocations => template_invocations_params }.with_indifferent_access
end

Expand Down Expand Up @@ -227,6 +230,7 @@ def params
:targeting => targeting_params,
:triggering => {},
:concurrency_control => {},
:remote_execution_feature_id => @feature.id,
:template_invocations => template_invocations_params }.with_indifferent_access
end

Expand Down Expand Up @@ -275,7 +279,7 @@ def job_template
end

attr_accessor :params, :job_invocation, :host_ids, :search_query
delegate :job_category, :pattern_template_invocations, :template_invocations, :targeting, :triggering, :to => :job_invocation
delegate :job_category, :remote_execution_feature_id, :pattern_template_invocations, :template_invocations, :targeting, :triggering, :to => :job_invocation

def initialize(params, set_defaults = false)
@params = params
Expand Down Expand Up @@ -307,6 +311,7 @@ def self.for_feature(feature_label, hosts, provided_inputs = {})
def compose
job_invocation.job_category = validate_job_category(params[:job_category])
job_invocation.job_category ||= available_job_categories.first if @set_defaults
job_invocation.remote_execution_feature_id = params[:remote_execution_feature_id]
job_invocation.targeting = build_targeting
job_invocation.triggering = build_triggering
job_invocation.pattern_template_invocations = build_template_invocations
Expand Down
16 changes: 12 additions & 4 deletions app/models/remote_execution_feature.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class RemoteExecutionFeature < ApplicationRecord
VALID_OPTIONS = [:provided_inputs, :description, :host_action_button].freeze
VALID_OPTIONS = [:provided_inputs, :description, :host_action_button, :notification_builder].freeze
validates :label, :name, :presence => true, :uniqueness => true

belongs_to :job_template
Expand Down Expand Up @@ -27,11 +27,19 @@ def self.register(label, name, options = {})
options[:host_action_button] = false unless options.key?(:host_action_button)

feature = self.find_by(label: label)
builder = options[:notification_builder] ? options[:notification_builder].to_s : nil

attributes = { :name => name, :provided_input_names => options[:provided_inputs], :description => options[:description], :host_action_button => options[:host_action_button] }
attributes = { :name => name,
:provided_input_names => options[:provided_inputs],
:description => options[:description],
:host_action_button => options[:host_action_button],
:notification_builder => builder }
# in case DB does not have the attribute created yet but plugin initializer registers the feature, we need to skip this attribute
unless self.attribute_names.include?('host_action_button')
attributes.delete(:host_action_button)
attrs = [ :host_action_button, :notification_builder ]
attrs.each do |attr|
unless self.attribute_names.include?(attr.to_s)
attributes.delete(attr)
end
end

if feature.nil?
Expand Down
1 change: 1 addition & 0 deletions app/views/job_invocations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<%= form_for @composer.job_invocation, :html => {'data-refresh-url' => refresh_job_invocations_path, :id => 'job_invocation_form'} do |f| %>
<%= selectable_f f, :job_category, @composer.available_job_categories, {}, :label => _('Job category') %>
<%= f.hidden_field(:job_category, :value => @composer.remote_execution_feature_id) %>
<% selected_templates_per_provider = {} %>
<% @composer.displayed_provider_types.each do |provider_type| %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddNotificationBuilderToRemoteExecutionFeature < ActiveRecord::Migration[4.2]
def change
add_column :remote_execution_features, :notification_builder, :string
end
end
6 changes: 6 additions & 0 deletions db/migrate/20180202123215_add_feature_id_to_job_invocation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddFeatureIdToJobInvocation < ActiveRecord::Migration[4.2]
def change
add_column :job_invocations, :remote_execution_feature_id, :integer, :index => true
add_foreign_key :job_invocations, :remote_execution_features, :column => :remote_execution_feature_id
end
end

0 comments on commit 96f9725

Please sign in to comment.