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

167 lines
4.9 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
module Dsl
extend ActiveSupport::Concern
included do
cattr_accessor :command_definitions, instance_accessor: false do
[]
end
cattr_accessor :command_definitions_by_name, instance_accessor: false do
{}
end
end
class_methods do
2017-09-10 17:25:29 +05:30
# Allows to give a description to the next quick action.
2016-09-13 17:45:13 +05:30
# This description is shown in the autocomplete menu.
# It accepts a block that will be evaluated with the context given to
# `CommandDefintion#to_h`.
#
# Example:
#
# desc do
2019-05-30 16:15:17 +05:30
# "This is a dynamic description for #{noteable.to_ability_name}"
2016-09-13 17:45:13 +05:30
# end
# command :command_key do |arguments|
# # Awesome code block
# end
def desc(text = '', &block)
@description = block_given? ? block : text
end
2019-02-15 15:39:39 +05:30
def warning(message = '')
@warning = message
end
2017-09-10 17:25:29 +05:30
# Allows to define params for the next quick action.
2016-09-13 17:45:13 +05:30
# These params are shown in the autocomplete menu.
#
# Example:
#
# params "~label ~label2"
# command :command_key do |arguments|
# # Awesome code block
# end
2017-08-17 22:00:37 +05:30
def params(*params, &block)
@params = block_given? ? block : params
end
# 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)
@explanation = block_given? ? block : text
2016-09-13 17:45:13 +05:30
end
# Allows to define conditions that must be met in order for the command
# to be returned by `.command_names` & `.command_definitions`.
2018-03-27 19:54:05 +05:30
# It accepts a block that will be evaluated with the context
# of a QuickActions::InterpretService instance
2016-09-13 17:45:13 +05:30
# Example:
#
# condition do
# project.public?
# end
# command :command_key do |arguments|
# # Awesome code block
# end
def condition(&block)
@condition_block = block
end
2017-08-17 22:00:37 +05:30
# 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-09-13 17:45:13 +05:30
# Registers a new command which is recognizeable from body of email or
# comment.
# It accepts aliases and takes a block.
#
# Example:
#
# command :my_command, :alias_for_my_command do |arguments|
# # Awesome code block
# end
def command(*command_names, &block)
2017-09-10 17:25:29 +05:30
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-09-13 17:45:13 +05:30
name, *aliases = command_names
2017-09-10 17:25:29 +05:30
definition = klass.new(
2016-09-13 17:45:13 +05:30
name,
2017-08-17 22:00:37 +05:30
aliases: aliases,
description: @description,
2019-02-15 15:39:39 +05:30
warning: @warning,
2017-08-17 22:00:37 +05:30
explanation: @explanation,
params: @params,
condition_block: @condition_block,
parse_params_block: @parse_params_block,
2019-05-30 16:15:17 +05:30
action_block: block
2016-09-13 17:45:13 +05:30
)
self.command_definitions << definition
definition.all_names.each do |name|
self.command_definitions_by_name[name] = definition
end
@description = nil
2017-08-17 22:00:37 +05:30
@explanation = nil
2016-09-13 17:45:13 +05:30
@params = nil
@condition_block = nil
2019-02-15 15:39:39 +05:30
@warning = nil
2017-08-17 22:00:37 +05:30
@parse_params_block = nil
end
2016-09-13 17:45:13 +05:30
end
end
end
end