summaryrefslogtreecommitdiff
path: root/lib/gitlab/quick_actions/extractor.rb
blob: 075ff91700c3519407fceaa60d44f468fe136249 (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
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
      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:
      # ```
      # 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)
        return [content, []] unless content

        content = content.dup

        commands = []

        content.delete!("\r")
        content.gsub!(commands_regex) do
          if $~[:cmd]
            commands << [$~[:cmd], $~[:arg]].reject(&:blank?)
            ''
          else
            $~[0]
          end
        end

        content, commands = perform_substitutions(content, commands)

        [content.strip, commands]
      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|$)/
      def commands_regex
        names = command_names.map(&:to_s)

        @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

              ^\/
              (?<cmd>#{Regexp.union(names)})
              (?:
                [ ]
                (?<arg>[^\n]*)
              )?
              (?:\n|$)
            )
        }mx
      end

      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|
          match_data = substitution.match(content)
          if match_data
            command = [substitution.name.to_s]
            command << match_data[1] unless match_data[1].empty?
            commands << command
          end

          content = substitution.perform_substitution(self, content)
        end

        [content, commands]
      end

      def command_names
        command_definitions.flat_map do |command|
          next if command.noop?

          command.all_names
        end.compact
      end
    end
  end
end