summaryrefslogtreecommitdiff
path: root/lib/gitlab/quick_actions/extractor.rb
blob: 1294e47514512120a229eb807aa14437d4c51654 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# frozen_string_literal: true

module Gitlab
  module QuickActions
    # This class takes an array of commands that should be extracted from a
    # given text.
    #
    # ```
    # extractor = Gitlab::QuickActions::Extractor.new([:open, :assign, :labels])
    # ```
    class Extractor
      CODE_REGEX = %r{
        (?<code>
          # Code blocks:
          # ```
          # Anything, including `/cmd arg` which are ignored by this filter
          # ```

          ^```
          .+?
          \n```$
        )
      }mix.freeze

      INLINE_CODE_REGEX = %r{
        (?<inline_code>
          # Inline code on separate rows:
          # `
          # Anything, including `/cmd arg` which are ignored by this filter
          # `

          `\n*
          .+?
          \n*`
        )
      }mix.freeze

      HTML_BLOCK_REGEX = %r{
        (?<html>
          # HTML block:
          # <tag>
          # Anything, including `/cmd arg` which are ignored by this filter
          # </tag>

          ^<[^>]+?>\n
          .+?
          \n<\/[^>]+?>$
        )
      }mix.freeze

      QUOTE_BLOCK_REGEX = %r{
        (?<html>
          # Quote block:
          # >>>
          # Anything, including `/cmd arg` which are ignored by this filter
          # >>>

          ^>>>
          .+?
          \n>>>$
        )
      }mix.freeze

      EXCLUSION_REGEX = %r{
        #{CODE_REGEX} | #{INLINE_CODE_REGEX} | #{HTML_BLOCK_REGEX} | #{QUOTE_BLOCK_REGEX}
      }mix.freeze

      attr_reader :command_definitions

      def initialize(command_definitions)
        @command_definitions = command_definitions
        @commands_regex = {}
      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:
      # ```
      # extractor = Gitlab::QuickActions::Extractor.new([:open, :assign, :labels])
      # msg = %(hello\n/labels ~foo ~"bar baz"\nworld)
      # commands = extractor.extract_commands(msg) #=> [['labels', '~foo ~"bar baz"']]
      # msg #=> "hello\nworld"
      # ```
      def extract_commands(content, only: nil)
        return [content, []] unless content

        perform_regex(content, only: only)
      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)
        names     = command_names(limit_to_commands: only).map(&:to_s)
        sub_names = substitution_names.map(&:to_s)
        commands  = []
        content   = content.dup
        content.delete!("\r")

        content.gsub!(commands_regex(names: names, sub_names: sub_names)) do
          command, output = if $~[:substitution]
                              process_substitutions($~)
                            else
                              process_commands($~, redact)
                            end

          commands << command
          output
        end

        [content.rstrip, commands.reject(&:empty?)]
      end

      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

      def process_substitutions(matched_text)
        output = matched_text[0]
        command = []

        if matched_text[:substitution]
          cmd = matched_text[:substitution].downcase
          command = [cmd, matched_text[:arg]].reject(&:blank?)

          substitution = substitution_definitions.find { |definition| definition.all_names.include?(cmd.to_sym) }
          output = substitution.perform_substitution(self, output) if substitution
        end

        [command, output]
      end

      # 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|$)/
      def commands_regex(names:, sub_names:)
        @commands_regex[names] ||= %r{
            #{EXCLUSION_REGEX}
          |
            (?:
              # Command not in a blockquote, blockcode, or HTML tag:
              # /close

              ^\/
              (?<cmd>#{Regexp.new(Regexp.union(names).source, Regexp::IGNORECASE)})
              (?:
                [ ]
                (?<arg>[^\n]*)
              )?
              (?:\s*\n|$)
            )
          |
            (?:
              # Substitution not in a blockquote, blockcode, or HTML tag:

              ^\/
              (?<substitution>#{Regexp.new(Regexp.union(sub_names).source, Regexp::IGNORECASE)})
              (?:
                [ ]
                (?<arg>[^\n]*)
              )?
              (?:\s*\n|$)
            )
        }mix
      end

      def command_names(limit_to_commands:)
        command_definitions.flat_map do |command|
          next if command.noop?

          if limit_to_commands && (command.all_names & limit_to_commands).empty?
            next
          end

          command.all_names
        end.compact
      end

      def substitution_names
        substitution_definitions.flat_map { |command| command.all_names }
          .compact
      end

      def substitution_definitions
        @substition_definitions ||= command_definitions.select do |command|
          command.is_a?(Gitlab::QuickActions::SubstitutionDefinition)
        end
      end
    end
  end
end