debian-mirror-gitlab/lib/gitlab/quick_actions/command_definition.rb

163 lines
4.4 KiB
Ruby
Raw Normal View History

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
2022-04-04 11:22:00 +05:30
ParseError = Class.new(StandardError)
2019-10-12 21:52:04 +05:30
attr_accessor :name, :aliases, :description, :explanation, :execution_message,
2020-03-13 15:44:24 +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-13 15:44:24 +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)
2022-04-04 11:22:00 +05:30
begin
execute_block(explanation, context, arg)
rescue ParseError => e
format(_('Problem with %{name} command: %{message}.'), name: name, message: e.message)
end
2019-02-15 15:39:39 +05:30
else
explanation
end
2020-03-13 15:44:24 +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)
2021-04-29 21:17:54 +05:30
return if noop?
2016-09-13 17:45:13 +05:30
2019-07-07 11:18:12 +05:30
count_commands_executed_in(context)
2021-04-29 21:17:54 +05:30
return unless available?(context)
2017-08-17 22:00:37 +05:30
execute_block(action_block, context, arg)
2022-04-04 11:22:00 +05:30
rescue ParseError
# message propagation is handled in `execution_message`.
2016-09-13 17:45:13 +05:30
end
2019-10-12 21:52:04 +05:30
def execute_message(context, arg)
2021-04-29 21:17:54 +05:30
return if noop?
return _('Could not apply %{name} command.') % { name: name } unless available?(context)
2019-10-12 21:52:04 +05:30
if execution_message.respond_to?(:call)
execute_block(execution_message, context, arg)
else
execution_message
end
2022-04-04 11:22:00 +05:30
rescue ParseError => e
format _('Could not apply %{name} command. %{message}.'), name: name, message: e.message
2019-10-12 21:52:04 +05:30
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)
2022-08-27 11:52:29 +05:30
desc = begin
context.instance_exec(&desc)
rescue StandardError
''
end
2016-09-13 17:45:13 +05:30
end
2020-03-13 15:44:24 +05:30
warn = warning
if warn.respond_to?(:call)
2022-08-27 11:52:29 +05:30
warn = begin
context.instance_exec(&warn)
rescue StandardError
''
end
2020-03-13 15:44:24 +05:30
end
2017-08-17 22:00:37 +05:30
prms = params
if prms.respond_to?(:call)
2022-08-27 11:52:29 +05:30
prms = begin
Array(context.instance_exec(&prms))
rescue StandardError
params
end
2017-08-17 22:00:37 +05:30
end
2016-09-13 17:45:13 +05:30
{
name: name,
aliases: aliases,
description: desc,
2020-03-13 15:44:24 +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-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)
2023-04-23 21:23:45 +05:30
types.blank? || types.any? do |type|
if context.quick_action_target.is_a?(WorkItem)
context.quick_action_target.supported_quick_action_commands.include?(name.to_sym)
else
context.quick_action_target.is_a?(type)
end
end
2019-07-07 11:18:12 +05:30
end
2016-09-13 17:45:13 +05:30
end
end
end