2016-06-30 15:34:19 +00:00
|
|
|
module Gitlab
|
2017-05-31 05:50:53 +00:00
|
|
|
module QuickActions
|
2016-06-30 15:34:19 +00:00
|
|
|
# This class takes an array of commands that should be extracted from a
|
|
|
|
# given text.
|
|
|
|
#
|
|
|
|
# ```
|
2017-05-31 05:50:53 +00:00
|
|
|
# extractor = Gitlab::QuickActions::Extractor.new([:open, :assign, :labels])
|
2016-06-30 15:34:19 +00:00
|
|
|
# ```
|
|
|
|
class Extractor
|
2016-08-13 01:17:18 +00:00
|
|
|
attr_reader :command_definitions
|
2016-06-30 15:34:19 +00:00
|
|
|
|
2016-08-13 01:17:18 +00:00
|
|
|
def initialize(command_definitions)
|
|
|
|
@command_definitions = command_definitions
|
2016-06-30 15:34:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Extracts commands from content and return an array of commands.
|
|
|
|
# The array looks like the following:
|
|
|
|
# [
|
|
|
|
# ['command1'],
|
|
|
|
# ['command3', 'arg1 arg2'],
|
|
|
|
# ]
|
|
|
|
# The command and the arguments are stripped.
|
|
|
|
# The original command text is removed from the given `content`.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# ```
|
2017-05-31 05:50:53 +00:00
|
|
|
# extractor = Gitlab::QuickActions::Extractor.new([:open, :assign, :labels])
|
2016-06-30 15:34:19 +00:00
|
|
|
# msg = %(hello\n/labels ~foo ~"bar baz"\nworld)
|
2016-08-13 01:17:18 +00:00
|
|
|
# commands = extractor.extract_commands(msg) #=> [['labels', '~foo ~"bar baz"']]
|
2016-06-30 15:34:19 +00:00
|
|
|
# msg #=> "hello\nworld"
|
|
|
|
# ```
|
2018-11-07 12:33:42 +00:00
|
|
|
def extract_commands(content, only: nil)
|
2016-08-13 16:58:51 +00:00
|
|
|
return [content, []] unless content
|
2016-06-30 15:34:19 +00:00
|
|
|
|
2016-08-13 01:17:18 +00:00
|
|
|
content = content.dup
|
|
|
|
|
2016-06-30 15:34:19 +00:00
|
|
|
commands = []
|
|
|
|
|
2016-08-09 20:47:29 +00:00
|
|
|
content.delete!("\r")
|
2018-11-07 12:33:42 +00:00
|
|
|
content.gsub!(commands_regex(only: only)) do
|
2016-08-09 20:47:29 +00:00
|
|
|
if $~[:cmd]
|
2018-06-13 10:23:57 +00:00
|
|
|
commands << [$~[:cmd].downcase, $~[:arg]].reject(&:blank?)
|
2016-08-09 20:47:29 +00:00
|
|
|
''
|
|
|
|
else
|
|
|
|
$~[0]
|
|
|
|
end
|
2016-06-30 15:34:19 +00:00
|
|
|
end
|
|
|
|
|
2017-07-25 03:11:22 +00:00
|
|
|
content, commands = perform_substitutions(content, commands)
|
|
|
|
|
2016-08-13 01:17:18 +00:00
|
|
|
[content.strip, commands]
|
2016-06-30 15:34:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-08-18 19:21:52 +00:00
|
|
|
|
2016-06-30 15:34:19 +00:00
|
|
|
# Builds a regular expression to match known commands.
|
|
|
|
# First match group captures the command name and
|
|
|
|
# second match group captures its arguments.
|
|
|
|
#
|
|
|
|
# It looks something like:
|
|
|
|
#
|
2016-08-18 19:21:52 +00:00
|
|
|
# /^\/(?<cmd>close|reopen|...)(?:( |$))(?<arg>[^\/\n]*)(?:\n|$)/
|
2018-11-07 12:33:42 +00:00
|
|
|
def commands_regex(only:)
|
|
|
|
names = command_names(limit_to_commands: only).map(&:to_s)
|
2016-08-13 01:17:18 +00:00
|
|
|
|
2016-08-09 20:47:29 +00:00
|
|
|
@commands_regex ||= %r{
|
|
|
|
(?<code>
|
|
|
|
# Code blocks:
|
|
|
|
# ```
|
2016-08-18 19:21:52 +00:00
|
|
|
# Anything, including `/cmd arg` which are ignored by this filter
|
2016-08-09 20:47:29 +00:00
|
|
|
# ```
|
|
|
|
|
|
|
|
^```
|
|
|
|
.+?
|
|
|
|
\n```$
|
|
|
|
)
|
|
|
|
|
|
|
|
|
(?<html>
|
|
|
|
# HTML block:
|
|
|
|
# <tag>
|
2016-08-18 19:21:52 +00:00
|
|
|
# Anything, including `/cmd arg` which are ignored by this filter
|
2016-08-09 20:47:29 +00:00
|
|
|
# </tag>
|
|
|
|
|
|
|
|
^<[^>]+?>\n
|
|
|
|
.+?
|
|
|
|
\n<\/[^>]+?>$
|
|
|
|
)
|
|
|
|
|
|
|
|
|
(?<html>
|
|
|
|
# Quote block:
|
|
|
|
# >>>
|
2016-08-18 19:21:52 +00:00
|
|
|
# Anything, including `/cmd arg` which are ignored by this filter
|
2016-08-09 20:47:29 +00:00
|
|
|
# >>>
|
|
|
|
|
|
|
|
^>>>
|
|
|
|
.+?
|
|
|
|
\n>>>$
|
|
|
|
)
|
|
|
|
|
|
|
|
|
(?:
|
|
|
|
# Command not in a blockquote, blockcode, or HTML tag:
|
|
|
|
# /close
|
|
|
|
|
2016-08-13 16:58:51 +00:00
|
|
|
^\/
|
2018-06-13 10:23:57 +00:00
|
|
|
(?<cmd>#{Regexp.new(Regexp.union(names).source, Regexp::IGNORECASE)})
|
2016-08-13 16:58:51 +00:00
|
|
|
(?:
|
|
|
|
[ ]
|
2017-02-17 09:25:34 +00:00
|
|
|
(?<arg>[^\n]*)
|
2016-08-13 16:58:51 +00:00
|
|
|
)?
|
|
|
|
(?:\n|$)
|
2016-08-09 20:47:29 +00:00
|
|
|
)
|
2018-06-13 10:23:57 +00:00
|
|
|
}mix
|
2016-06-30 15:34:19 +00:00
|
|
|
end
|
2016-08-17 23:58:44 +00:00
|
|
|
|
2017-07-25 03:11:22 +00:00
|
|
|
def perform_substitutions(content, commands)
|
|
|
|
return unless content
|
|
|
|
|
|
|
|
substitution_definitions = self.command_definitions.select do |definition|
|
|
|
|
definition.is_a?(Gitlab::QuickActions::SubstitutionDefinition)
|
|
|
|
end
|
|
|
|
|
|
|
|
substitution_definitions.each do |substitution|
|
2018-06-13 10:23:57 +00:00
|
|
|
match_data = substitution.match(content.downcase)
|
2017-07-25 03:11:22 +00:00
|
|
|
if match_data
|
|
|
|
command = [substitution.name.to_s]
|
|
|
|
command << match_data[1] unless match_data[1].empty?
|
|
|
|
commands << command
|
|
|
|
end
|
2018-01-11 16:34:01 +00:00
|
|
|
|
2017-07-25 03:11:22 +00:00
|
|
|
content = substitution.perform_substitution(self, content)
|
|
|
|
end
|
|
|
|
|
|
|
|
[content, commands]
|
|
|
|
end
|
|
|
|
|
2018-11-07 12:33:42 +00:00
|
|
|
def command_names(limit_to_commands:)
|
2016-08-17 23:58:44 +00:00
|
|
|
command_definitions.flat_map do |command|
|
|
|
|
next if command.noop?
|
|
|
|
|
2018-11-07 12:33:42 +00:00
|
|
|
if limit_to_commands && (command.all_names & limit_to_commands).empty?
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2016-08-17 23:58:44 +00:00
|
|
|
command.all_names
|
|
|
|
end.compact
|
|
|
|
end
|
2016-06-30 15:34:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|