Class: Gitlab::QuickActions::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/quick_actions/extractor.rb

Overview

This class takes an array of commands that should be extracted from a given text.

“` extractor = Gitlab::QuickActions::Extractor.new([:open, :assign, :labels]) “`

Constant Summary collapse

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
    # `

    `.+?`
  )
}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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_definitions) ⇒ Extractor

Returns a new instance of Extractor.


68
69
70
71
# File 'lib/gitlab/quick_actions/extractor.rb', line 68

def initialize(command_definitions)
  @command_definitions = command_definitions
  @commands_regex = {}
end

Instance Attribute Details

#command_definitionsObject (readonly)

Returns the value of attribute command_definitions.


66
67
68
# File 'lib/gitlab/quick_actions/extractor.rb', line 66

def command_definitions
  @command_definitions
end

Instance Method Details

#extract_commands(content, only: nil) ⇒ Object

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 = %(hellon/labels ~foo ~“bar baz”nworld) commands = extractor.extract_commands(msg) #=> [['labels', '~foo ~“bar baz”']] msg #=> “hellonworld” “`


89
90
91
92
93
# File 'lib/gitlab/quick_actions/extractor.rb', line 89

def extract_commands(content, only: nil)
  return [content, []] unless content

  perform_regex(content, only: only)
end

#redact_commands(content) ⇒ Object

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`


99
100
101
102
103
104
105
# File 'lib/gitlab/quick_actions/extractor.rb', line 99

def redact_commands(content)
  return "" unless content

  content, _ = perform_regex(content, redact: true)

  content
end