2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
module Gitlab
|
2017-09-10 17:25:29 +05:30
|
|
|
module QuickActions
|
2016-09-13 17:45:13 +05:30
|
|
|
class CommandDefinition
|
2019-10-12 21:52:04 +05:30
|
|
|
attr_accessor :name, :aliases, :description, :explanation, :execution_message,
|
2020-03-09 13:42:32 +05:30
|
|
|
:params, :condition_block, :parse_params_block, :action_block, :warning, :icon, :types
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
def initialize(name, attributes = {})
|
|
|
|
@name = name
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
@aliases = attributes[:aliases] || []
|
|
|
|
@description = attributes[:description] || ''
|
2019-02-15 15:39:39 +05:30
|
|
|
@warning = attributes[:warning] || ''
|
2020-03-09 13:42:32 +05:30
|
|
|
@icon = attributes[:icon] || ''
|
2017-08-17 22:00:37 +05:30
|
|
|
@explanation = attributes[:explanation] || ''
|
2019-10-12 21:52:04 +05:30
|
|
|
@execution_message = attributes[:execution_message] || ''
|
2017-08-17 22:00:37 +05:30
|
|
|
@params = attributes[:params] || []
|
2016-09-13 17:45:13 +05:30
|
|
|
@condition_block = attributes[:condition_block]
|
2017-08-17 22:00:37 +05:30
|
|
|
@parse_params_block = attributes[:parse_params_block]
|
|
|
|
@action_block = attributes[:action_block]
|
2019-07-07 11:18:12 +05:30
|
|
|
@types = attributes[:types] || []
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def all_names
|
|
|
|
[name, *aliases]
|
|
|
|
end
|
|
|
|
|
|
|
|
def noop?
|
|
|
|
action_block.nil?
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def available?(context)
|
2019-07-07 11:18:12 +05:30
|
|
|
return false unless valid_type?(context)
|
2016-09-13 17:45:13 +05:30
|
|
|
return true unless condition_block
|
|
|
|
|
|
|
|
context.instance_exec(&condition_block)
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def explain(context, arg)
|
|
|
|
return unless available?(context)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
message = if explanation.respond_to?(:call)
|
|
|
|
execute_block(explanation, context, arg)
|
|
|
|
else
|
|
|
|
explanation
|
|
|
|
end
|
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
warning_text = if warning.respond_to?(:call)
|
|
|
|
execute_block(warning, context, arg)
|
|
|
|
else
|
|
|
|
warning
|
|
|
|
end
|
|
|
|
|
|
|
|
warning.empty? ? message : "#{message} (#{warning_text})"
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def execute(context, arg)
|
2019-10-12 21:52:04 +05:30
|
|
|
return unless executable?(context)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
count_commands_executed_in(context)
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
execute_block(action_block, context, arg)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def execute_message(context, arg)
|
|
|
|
return unless executable?(context)
|
|
|
|
|
|
|
|
if execution_message.respond_to?(:call)
|
|
|
|
execute_block(execution_message, context, arg)
|
|
|
|
else
|
|
|
|
execution_message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def to_h(context)
|
2016-09-13 17:45:13 +05:30
|
|
|
desc = description
|
|
|
|
if desc.respond_to?(:call)
|
|
|
|
desc = context.instance_exec(&desc) rescue ''
|
|
|
|
end
|
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
warn = warning
|
|
|
|
if warn.respond_to?(:call)
|
|
|
|
warn = context.instance_exec(&warn) rescue ''
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
prms = params
|
|
|
|
if prms.respond_to?(:call)
|
|
|
|
prms = Array(context.instance_exec(&prms)) rescue params
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
{
|
|
|
|
name: name,
|
|
|
|
aliases: aliases,
|
|
|
|
description: desc,
|
2020-03-09 13:42:32 +05:30
|
|
|
warning: warn,
|
|
|
|
icon: icon,
|
2017-08-17 22:00:37 +05:30
|
|
|
params: prms
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def executable?(context)
|
|
|
|
!noop? && available?(context)
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def count_commands_executed_in(context)
|
|
|
|
return unless context.respond_to?(:commands_executed_count=)
|
|
|
|
|
|
|
|
context.commands_executed_count ||= 0
|
|
|
|
context.commands_executed_count += 1
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def execute_block(block, context, arg)
|
|
|
|
if arg.present?
|
|
|
|
parsed = parse_params(arg, context)
|
|
|
|
context.instance_exec(parsed, &block)
|
|
|
|
elsif block.arity == 0
|
|
|
|
context.instance_exec(&block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_params(arg, context)
|
|
|
|
return arg unless parse_params_block
|
|
|
|
|
|
|
|
context.instance_exec(arg, &parse_params_block)
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
def valid_type?(context)
|
|
|
|
types.blank? || types.any? { |type| context.quick_action_target.is_a?(type) }
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|