diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-24 15:09:00 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-24 15:09:00 +0000 |
commit | c282dba898a4cb0645f88579339502a4e3778727 (patch) | |
tree | 94a6457ce4438e085c9ae43bc51a2b5a29787bf2 /lib/gitlab/quick_actions | |
parent | 2c2dd5e36c4ed5f09f488be288882d98f9124d12 (diff) | |
download | gitlab-ce-c282dba898a4cb0645f88579339502a4e3778727.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/quick_actions')
-rw-r--r-- | lib/gitlab/quick_actions/extractor.rb | 53 |
1 files changed, 41 insertions, 12 deletions
diff --git a/lib/gitlab/quick_actions/extractor.rb b/lib/gitlab/quick_actions/extractor.rb index e04d6f250b1..6f87968e286 100644 --- a/lib/gitlab/quick_actions/extractor.rb +++ b/lib/gitlab/quick_actions/extractor.rb @@ -34,26 +34,55 @@ module Gitlab def extract_commands(content, only: nil) return [content, []] unless content - content = content.dup + content, commands = perform_regex(content, only: only) - commands = [] + perform_substitutions(content, commands) + end + # Encloses quick action commands into code span markdown + # avoiding them being executed, for example, when sent via email + # to GitLab service desk. + # Example: /label ~label1 becomes `/label ~label1` + def redact_commands(content) + return "" unless content + + content, _ = perform_regex(content, redact: true) + + content + end + + private + + def perform_regex(content, only: nil, redact: false) + commands = [] + content = content.dup content.delete!("\r") + content.gsub!(commands_regex(only: only)) do - if $~[:cmd] - commands << [$~[:cmd].downcase, $~[:arg]].reject(&:blank?) - '' - else - $~[0] - end + command, output = process_commands($~, redact) + commands << command + output end - content, commands = perform_substitutions(content, commands) - - [content.rstrip, commands] + [content.rstrip, commands.reject(&:empty?)] end - private + def process_commands(matched_text, redact) + output = matched_text[0] + command = [] + + if matched_text[:cmd] + command = [matched_text[:cmd].downcase, matched_text[:arg]].reject(&:blank?) + output = '' + + if redact + output = "`/#{matched_text[:cmd]}#{" " + matched_text[:arg] if matched_text[:arg]}`" + output += "\n" if matched_text[0].include?("\n") + end + end + + [command, output] + end # Builds a regular expression to match known commands. # First match group captures the command name and |