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
|
|
|
# This class takes an array of commands that should be extracted from a
|
|
|
|
# given text.
|
|
|
|
#
|
|
|
|
# ```
|
2017-09-10 17:25:29 +05:30
|
|
|
# extractor = Gitlab::QuickActions::Extractor.new([:open, :assign, :labels])
|
2016-09-13 17:45:13 +05:30
|
|
|
# ```
|
|
|
|
class Extractor
|
|
|
|
attr_reader :command_definitions
|
|
|
|
|
|
|
|
def initialize(command_definitions)
|
|
|
|
@command_definitions = command_definitions
|
|
|
|
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-09-10 17:25:29 +05:30
|
|
|
# extractor = Gitlab::QuickActions::Extractor.new([:open, :assign, :labels])
|
2016-09-13 17:45:13 +05:30
|
|
|
# msg = %(hello\n/labels ~foo ~"bar baz"\nworld)
|
|
|
|
# commands = extractor.extract_commands(msg) #=> [['labels', '~foo ~"bar baz"']]
|
|
|
|
# msg #=> "hello\nworld"
|
|
|
|
# ```
|
2018-12-13 13:39:08 +05:30
|
|
|
def extract_commands(content, only: nil)
|
2016-09-13 17:45:13 +05:30
|
|
|
return [content, []] unless content
|
|
|
|
|
|
|
|
content = content.dup
|
|
|
|
|
|
|
|
commands = []
|
|
|
|
|
|
|
|
content.delete!("\r")
|
2018-12-13 13:39:08 +05:30
|
|
|
content.gsub!(commands_regex(only: only)) do
|
2016-09-13 17:45:13 +05:30
|
|
|
if $~[:cmd]
|
2018-11-08 19:23:39 +05:30
|
|
|
commands << [$~[:cmd].downcase, $~[:arg]].reject(&:blank?)
|
2016-09-13 17:45:13 +05:30
|
|
|
''
|
|
|
|
else
|
|
|
|
$~[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
content, commands = perform_substitutions(content, commands)
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
[content.rstrip, commands]
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
#
|
|
|
|
# /^\/(?<cmd>close|reopen|...)(?:( |$))(?<arg>[^\/\n]*)(?:\n|$)/
|
2018-12-13 13:39:08 +05:30
|
|
|
def commands_regex(only:)
|
|
|
|
names = command_names(limit_to_commands: only).map(&:to_s)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
@commands_regex ||= %r{
|
|
|
|
(?<code>
|
|
|
|
# Code blocks:
|
|
|
|
# ```
|
|
|
|
# Anything, including `/cmd arg` which are ignored by this filter
|
|
|
|
# ```
|
|
|
|
|
|
|
|
^```
|
|
|
|
.+?
|
|
|
|
\n```$
|
|
|
|
)
|
|
|
|
|
|
|
|
|
(?<html>
|
|
|
|
# HTML block:
|
|
|
|
# <tag>
|
|
|
|
# Anything, including `/cmd arg` which are ignored by this filter
|
|
|
|
# </tag>
|
|
|
|
|
|
|
|
^<[^>]+?>\n
|
|
|
|
.+?
|
|
|
|
\n<\/[^>]+?>$
|
|
|
|
)
|
|
|
|
|
|
|
|
|
(?<html>
|
|
|
|
# Quote block:
|
|
|
|
# >>>
|
|
|
|
# Anything, including `/cmd arg` which are ignored by this filter
|
|
|
|
# >>>
|
|
|
|
|
|
|
|
^>>>
|
|
|
|
.+?
|
|
|
|
\n>>>$
|
|
|
|
)
|
|
|
|
|
|
|
|
|
(?:
|
|
|
|
# Command not in a blockquote, blockcode, or HTML tag:
|
|
|
|
# /close
|
|
|
|
|
|
|
|
^\/
|
2018-11-08 19:23:39 +05:30
|
|
|
(?<cmd>#{Regexp.new(Regexp.union(names).source, Regexp::IGNORECASE)})
|
2016-09-13 17:45:13 +05:30
|
|
|
(?:
|
|
|
|
[ ]
|
2017-08-17 22:00:37 +05:30
|
|
|
(?<arg>[^\n]*)
|
2016-09-13 17:45:13 +05:30
|
|
|
)?
|
2019-12-21 20:55:43 +05:30
|
|
|
(?:\s*\n|$)
|
2016-09-13 17:45:13 +05:30
|
|
|
)
|
2018-11-08 19:23:39 +05:30
|
|
|
}mix
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
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-11-08 19:23:39 +05:30
|
|
|
match_data = substitution.match(content.downcase)
|
2017-09-10 17:25:29 +05:30
|
|
|
if match_data
|
|
|
|
command = [substitution.name.to_s]
|
|
|
|
command << match_data[1] unless match_data[1].empty?
|
|
|
|
commands << command
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
content = substitution.perform_substitution(self, content)
|
|
|
|
end
|
|
|
|
|
|
|
|
[content, commands]
|
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def command_names(limit_to_commands:)
|
2016-09-13 17:45:13 +05:30
|
|
|
command_definitions.flat_map do |command|
|
|
|
|
next if command.noop?
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
if limit_to_commands && (command.all_names & limit_to_commands).empty?
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
command.all_names
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|