2016-06-30 15:34:19 +00:00
|
|
|
module Gitlab
|
|
|
|
module SlashCommands
|
|
|
|
module Dsl
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
@command_definitions = []
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2016-08-11 16:51:37 +00:00
|
|
|
# This method is used to generate the autocompletion menu
|
|
|
|
# It returns no-op slash commands (such as `/cc`)
|
2016-08-10 15:51:01 +00:00
|
|
|
def command_definitions(opts = {})
|
|
|
|
@command_definitions.map do |cmd_def|
|
2016-08-11 16:51:37 +00:00
|
|
|
context = OpenStruct.new(opts)
|
|
|
|
next if cmd_def[:cond_block] && !context.instance_exec(&cmd_def[:cond_block])
|
2016-08-10 15:51:01 +00:00
|
|
|
|
|
|
|
cmd_def = cmd_def.dup
|
2016-06-30 15:34:19 +00:00
|
|
|
|
2016-08-10 15:51:01 +00:00
|
|
|
if cmd_def[:description].present? && cmd_def[:description].respond_to?(:call)
|
2016-08-11 16:51:37 +00:00
|
|
|
cmd_def[:description] = context.instance_exec(&cmd_def[:description]) rescue ''
|
2016-08-10 12:12:09 +00:00
|
|
|
end
|
2016-08-10 15:51:01 +00:00
|
|
|
|
|
|
|
cmd_def
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
2016-08-11 16:51:37 +00:00
|
|
|
# This method is used to generate a list of valid commands in the current
|
|
|
|
# context of `opts`.
|
|
|
|
# It excludes no-op slash commands (such as `/cc`).
|
|
|
|
# This list can then be given to `Gitlab::SlashCommands::Extractor`.
|
2016-08-10 15:51:01 +00:00
|
|
|
def command_names(opts = {})
|
|
|
|
command_definitions(opts).flat_map do |command_definition|
|
|
|
|
next if command_definition[:noop]
|
|
|
|
|
|
|
|
[command_definition[:name], *command_definition[:aliases]]
|
2016-08-10 12:12:09 +00:00
|
|
|
end.compact
|
2016-06-30 15:34:19 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 16:51:37 +00:00
|
|
|
# Allows to give a description to the next slash command.
|
|
|
|
# This description is shown in the autocomplete menu.
|
|
|
|
# It accepts a block that will be evaluated with the context given to
|
|
|
|
# `.command_definitions` or `.command_names`.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# desc do
|
|
|
|
# "This is a dynamic description for #{noteable.to_ability_name}"
|
|
|
|
# end
|
|
|
|
# command :command_key do |arguments|
|
|
|
|
# # Awesome code block
|
|
|
|
# end
|
|
|
|
def desc(text = '', &block)
|
|
|
|
@description = block_given? ? block : text
|
2016-06-30 15:34:19 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 16:51:37 +00:00
|
|
|
# Allows to define params for the next slash command.
|
|
|
|
# These params are shown in the autocomplete menu.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# params "~label ~label2"
|
|
|
|
# command :command_key do |arguments|
|
|
|
|
# # Awesome code block
|
|
|
|
# end
|
2016-06-30 15:34:19 +00:00
|
|
|
def params(*params)
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
2016-08-11 16:51:37 +00:00
|
|
|
# Allows to define conditions that must be met in order for the command
|
|
|
|
# to be returned by `.command_names` & `.command_definitions`.
|
|
|
|
# It accepts a block that will be evaluated with the context given to
|
|
|
|
# `.command_definitions`, `.command_names`, and the actual command method.
|
|
|
|
#
|
2016-06-30 15:34:19 +00:00
|
|
|
# Example:
|
|
|
|
#
|
2016-08-11 16:51:37 +00:00
|
|
|
# condition do
|
|
|
|
# project.public?
|
|
|
|
# end
|
2016-06-30 15:34:19 +00:00
|
|
|
# command :command_key do |arguments|
|
|
|
|
# # Awesome code block
|
|
|
|
# end
|
2016-08-11 16:51:37 +00:00
|
|
|
def condition(&block)
|
|
|
|
@cond_block = block
|
|
|
|
end
|
|
|
|
|
|
|
|
# Registers a new command which is recognizeable from body of email or
|
|
|
|
# comment.
|
|
|
|
# It accepts aliases and takes a block.
|
2016-06-30 15:34:19 +00:00
|
|
|
#
|
2016-08-11 16:51:37 +00:00
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# command :my_command, :alias_for_my_command do |arguments|
|
|
|
|
# # Awesome code block
|
|
|
|
# end
|
2016-06-30 15:34:19 +00:00
|
|
|
def command(*command_names, &block)
|
2016-08-11 16:51:37 +00:00
|
|
|
opts = command_names.extract_options!
|
2016-06-30 15:34:19 +00:00
|
|
|
command_name, *aliases = command_names
|
|
|
|
proxy_method_name = "__#{command_name}__"
|
|
|
|
|
2016-08-11 16:51:37 +00:00
|
|
|
if block_given?
|
|
|
|
# This proxy method is needed because calling `return` from inside a
|
|
|
|
# block/proc, causes a `return` from the enclosing method or lambda,
|
|
|
|
# otherwise a LocalJumpError error is raised.
|
|
|
|
define_method(proxy_method_name, &block)
|
2016-06-30 15:34:19 +00:00
|
|
|
|
2016-08-11 16:51:37 +00:00
|
|
|
define_method(command_name) do |*args|
|
|
|
|
return if @cond_block && !instance_exec(&@cond_block)
|
2016-08-10 15:51:01 +00:00
|
|
|
|
2016-08-11 16:51:37 +00:00
|
|
|
proxy_method = method(proxy_method_name)
|
2016-06-30 15:34:19 +00:00
|
|
|
|
2016-08-11 16:51:37 +00:00
|
|
|
if proxy_method.arity == -1 || proxy_method.arity == args.size
|
|
|
|
instance_exec(*args, &proxy_method)
|
|
|
|
end
|
2016-06-30 15:34:19 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 16:51:37 +00:00
|
|
|
private command_name
|
|
|
|
aliases.each do |alias_command|
|
|
|
|
alias_method alias_command, command_name
|
|
|
|
private alias_command
|
|
|
|
end
|
2016-06-30 15:34:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
command_definition = {
|
|
|
|
name: command_name,
|
|
|
|
aliases: aliases,
|
|
|
|
description: @description || '',
|
2016-08-10 15:51:01 +00:00
|
|
|
params: @params || []
|
2016-06-30 15:34:19 +00:00
|
|
|
}
|
2016-08-11 16:51:37 +00:00
|
|
|
command_definition[:noop] = opts[:noop] || false
|
|
|
|
command_definition[:cond_block] = @cond_block
|
2016-06-30 15:34:19 +00:00
|
|
|
@command_definitions << command_definition
|
|
|
|
|
|
|
|
@description = nil
|
|
|
|
@params = nil
|
2016-08-11 16:51:37 +00:00
|
|
|
@cond_block = nil
|
2016-06-30 15:34:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|