2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-30 11:34:19 -04:00
|
|
|
module Gitlab
|
2017-05-31 01:50:53 -04:00
|
|
|
module QuickActions
|
2016-06-30 11:34:19 -04:00
|
|
|
module Dsl
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2016-08-12 21:17:18 -04:00
|
|
|
cattr_accessor :command_definitions, instance_accessor: false do
|
|
|
|
[]
|
2016-08-12 05:19:29 -04:00
|
|
|
end
|
|
|
|
|
2016-08-12 21:17:18 -04:00
|
|
|
cattr_accessor :command_definitions_by_name, instance_accessor: false do
|
|
|
|
{}
|
2016-08-12 05:19:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
2017-05-31 01:50:53 -04:00
|
|
|
# Allows to give a description to the next quick action.
|
2016-08-11 12:51:37 -04:00
|
|
|
# This description is shown in the autocomplete menu.
|
|
|
|
# It accepts a block that will be evaluated with the context given to
|
2016-08-16 20:59:55 -04:00
|
|
|
# `CommandDefintion#to_h`.
|
2016-08-11 12:51:37 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# desc do
|
2019-03-13 06:45:43 -04:00
|
|
|
# "This is a dynamic description for #{quick_action_target.to_ability_name}"
|
2016-08-11 12:51:37 -04:00
|
|
|
# end
|
|
|
|
# command :command_key do |arguments|
|
|
|
|
# # Awesome code block
|
|
|
|
# end
|
|
|
|
def desc(text = '', &block)
|
2022-08-03 08:11:38 -04:00
|
|
|
@description = block || text
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
2019-12-20 19:07:40 -05:00
|
|
|
def warning(text = '', &block)
|
2022-08-03 08:11:38 -04:00
|
|
|
@warning = block || text
|
2019-12-20 19:07:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def icon(string = '')
|
|
|
|
@icon = string
|
2018-10-29 05:05:47 -04:00
|
|
|
end
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
# Allows to define params for the next quick action.
|
2016-08-11 12:51:37 -04:00
|
|
|
# These params are shown in the autocomplete menu.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# params "~label ~label2"
|
|
|
|
# command :command_key do |arguments|
|
|
|
|
# # Awesome code block
|
|
|
|
# end
|
2017-06-06 04:49:36 -04:00
|
|
|
def params(*params, &block)
|
2022-08-03 08:11:38 -04:00
|
|
|
@params = block || params
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
# Allows to give an explanation of what the command will do when
|
|
|
|
# executed. This explanation is shown when rendering the Markdown
|
|
|
|
# preview.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# explanation do |arguments|
|
|
|
|
# "Adds label(s) #{arguments.join(' ')}"
|
|
|
|
# end
|
|
|
|
# command :command_key do |arguments|
|
|
|
|
# # Awesome code block
|
|
|
|
# end
|
|
|
|
def explanation(text = '', &block)
|
2022-08-03 08:11:38 -04:00
|
|
|
@explanation = block || text
|
2016-11-16 06:09:09 -05:00
|
|
|
end
|
|
|
|
|
2019-07-29 18:35:29 -04:00
|
|
|
# Allows to provide a message about quick action execution result, success or failure.
|
|
|
|
# This message is shown after quick action execution and after saving the note.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# execution_message do |arguments|
|
|
|
|
# "Added label(s) #{arguments.join(' ')}"
|
|
|
|
# end
|
|
|
|
# command :command_key do |arguments|
|
|
|
|
# # Awesome code block
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Note: The execution_message won't be executed unless the condition block returns true.
|
|
|
|
# execution_message block is executed always after the command block has run,
|
|
|
|
# for this reason if the condition block doesn't return true after the command block has
|
|
|
|
# run you need to set the @execution_message variable inside the command block instead as
|
|
|
|
# shown in the following example.
|
|
|
|
#
|
|
|
|
# Example using instance variable:
|
|
|
|
#
|
|
|
|
# command :command_key do |arguments|
|
|
|
|
# # Awesome code block
|
|
|
|
# @execution_message[:command_key] = 'command_key executed successfully'
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
def execution_message(text = '', &block)
|
2022-08-03 08:11:38 -04:00
|
|
|
@execution_message = block || text
|
2019-07-29 18:35:29 -04:00
|
|
|
end
|
|
|
|
|
2019-03-13 06:45:43 -04:00
|
|
|
# Allows to define type(s) that must be met in order for the command
|
|
|
|
# to be returned by `.command_names` & `.command_definitions`.
|
|
|
|
#
|
|
|
|
# It is being evaluated before the conditions block is being evaluated
|
|
|
|
#
|
|
|
|
# If no types are passed then any type is allowed as the check is simply skipped.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# types Commit, Issue, MergeRequest
|
|
|
|
# command :command_key do |arguments|
|
|
|
|
# # Awesome code block
|
|
|
|
# end
|
|
|
|
def types(*types_list)
|
|
|
|
@types = types_list
|
|
|
|
end
|
|
|
|
|
2016-08-11 12:51:37 -04:00
|
|
|
# Allows to define conditions that must be met in order for the command
|
|
|
|
# to be returned by `.command_names` & `.command_definitions`.
|
2018-02-23 09:23:09 -05:00
|
|
|
# It accepts a block that will be evaluated with the context
|
|
|
|
# of a QuickActions::InterpretService instance
|
2016-06-30 11:34:19 -04:00
|
|
|
# Example:
|
|
|
|
#
|
2016-08-11 12:51:37 -04:00
|
|
|
# condition do
|
|
|
|
# project.public?
|
|
|
|
# end
|
2016-06-30 11:34:19 -04:00
|
|
|
# command :command_key do |arguments|
|
|
|
|
# # Awesome code block
|
|
|
|
# end
|
2016-08-11 12:51:37 -04:00
|
|
|
def condition(&block)
|
2016-08-12 05:19:29 -04:00
|
|
|
@condition_block = block
|
2016-08-11 12:51:37 -04:00
|
|
|
end
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
# Allows to perform initial parsing of parameters. The result is passed
|
|
|
|
# both to `command` and `explanation` blocks, instead of the raw
|
|
|
|
# parameters.
|
|
|
|
# It accepts a block that will be evaluated with the context given to
|
|
|
|
# `CommandDefintion#to_h`.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# parse_params do |raw|
|
|
|
|
# raw.strip
|
|
|
|
# end
|
|
|
|
# command :command_key do |parsed|
|
|
|
|
# # Awesome code block
|
|
|
|
# end
|
|
|
|
def parse_params(&block)
|
|
|
|
@parse_params_block = block
|
|
|
|
end
|
|
|
|
|
2016-08-11 12:51:37 -04:00
|
|
|
# Registers a new command which is recognizeable from body of email or
|
|
|
|
# comment.
|
|
|
|
# It accepts aliases and takes a block.
|
2016-06-30 11:34:19 -04:00
|
|
|
#
|
2019-07-29 18:35:29 -04:00
|
|
|
# You can also set the @execution_message instance variable, on conflicts with
|
|
|
|
# execution_message method the instance variable has precedence.
|
|
|
|
#
|
2016-08-11 12:51:37 -04:00
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# command :my_command, :alias_for_my_command do |arguments|
|
|
|
|
# # Awesome code block
|
2019-07-29 18:35:29 -04:00
|
|
|
# @updates[:my_command] = 'foo'
|
|
|
|
#
|
|
|
|
# @execution_message[:my_command] = 'my_command executed successfully'
|
2016-08-11 12:51:37 -04:00
|
|
|
# end
|
2016-06-30 11:34:19 -04:00
|
|
|
def command(*command_names, &block)
|
2017-07-24 23:11:22 -04:00
|
|
|
define_command(CommandDefinition, *command_names, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Registers a new substitution which is recognizable from body of email or
|
|
|
|
# comment.
|
|
|
|
# It accepts aliases and takes a block with the formatted content.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# command :my_substitution, :alias_for_my_substitution do |text|
|
|
|
|
# "#{text} MY AWESOME SUBSTITUTION"
|
|
|
|
# end
|
|
|
|
def substitution(*substitution_names, &block)
|
|
|
|
define_command(SubstitutionDefinition, *substitution_names, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def definition_by_name(name)
|
|
|
|
command_definitions_by_name[name.to_sym]
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def define_command(klass, *command_names, &block)
|
2016-08-12 05:19:29 -04:00
|
|
|
name, *aliases = command_names
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2017-07-24 23:11:22 -04:00
|
|
|
definition = klass.new(
|
2016-08-16 20:59:55 -04:00
|
|
|
name,
|
2016-11-16 06:09:09 -05:00
|
|
|
aliases: aliases,
|
|
|
|
description: @description,
|
2018-10-29 05:05:47 -04:00
|
|
|
warning: @warning,
|
2019-12-20 19:07:40 -05:00
|
|
|
icon: @icon,
|
2016-11-16 06:09:09 -05:00
|
|
|
explanation: @explanation,
|
2019-07-29 18:35:29 -04:00
|
|
|
execution_message: @execution_message,
|
2016-11-16 06:09:09 -05:00
|
|
|
params: @params,
|
|
|
|
condition_block: @condition_block,
|
|
|
|
parse_params_block: @parse_params_block,
|
2019-03-13 06:45:43 -04:00
|
|
|
action_block: block,
|
|
|
|
types: @types
|
2016-08-16 20:59:55 -04:00
|
|
|
)
|
2016-08-12 21:17:18 -04:00
|
|
|
|
|
|
|
self.command_definitions << definition
|
|
|
|
|
|
|
|
definition.all_names.each do |name|
|
|
|
|
self.command_definitions_by_name[name] = definition
|
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
|
|
@description = nil
|
2016-11-16 06:09:09 -05:00
|
|
|
@explanation = nil
|
2019-07-29 18:35:29 -04:00
|
|
|
@execution_message = nil
|
2016-06-30 11:34:19 -04:00
|
|
|
@params = nil
|
2016-08-12 05:19:29 -04:00
|
|
|
@condition_block = nil
|
2018-10-29 05:05:47 -04:00
|
|
|
@warning = nil
|
2019-12-20 19:07:40 -05:00
|
|
|
@icon = nil
|
2016-11-16 06:09:09 -05:00
|
|
|
@parse_params_block = nil
|
2019-03-13 06:45:43 -04:00
|
|
|
@types = nil
|
2016-11-16 06:09:09 -05:00
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|