2016-06-30 11:34:19 -04:00
|
|
|
module Gitlab
|
|
|
|
module SlashCommands
|
|
|
|
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
|
2016-08-11 12:51:37 -04: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
|
2016-08-16 20:59:55 -04:00
|
|
|
# `CommandDefintion#to_h`.
|
2016-08-11 12:51:37 -04:00
|
|
|
#
|
|
|
|
# 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 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
2016-08-11 12:51:37 -04: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 11:34:19 -04:00
|
|
|
def params(*params)
|
|
|
|
@params = params
|
|
|
|
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`.
|
|
|
|
# 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
|
|
|
#
|
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
|
|
|
|
|
|
|
|
# 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
|
|
|
#
|
2016-08-11 12:51:37 -04:00
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# command :my_command, :alias_for_my_command do |arguments|
|
|
|
|
# # Awesome code block
|
|
|
|
# end
|
2016-06-30 11:34:19 -04:00
|
|
|
def command(*command_names, &block)
|
2016-08-12 05:19:29 -04:00
|
|
|
name, *aliases = command_names
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2016-08-16 20:59:55 -04:00
|
|
|
definition = CommandDefinition.new(
|
|
|
|
name,
|
|
|
|
aliases: aliases,
|
|
|
|
description: @description,
|
|
|
|
params: @params,
|
|
|
|
condition_block: @condition_block,
|
|
|
|
action_block: block
|
|
|
|
)
|
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
|
|
|
|
@params = nil
|
2016-08-12 05:19:29 -04:00
|
|
|
@condition_block = nil
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|