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

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

    ^<[^>]+?>\n
    .+?
    \n<\/[^>]+?>$
  )
}mix
QUOTE_BLOCK_REGEX =
%r{
  (?<html>
    # Quote block:
    # >>>
    # Anything, including `/cmd arg` which are ignored by this filter
    # >>>

    ^>>>
    .+?
    \n>>>$
  )
}mix
EXCLUSION_REGEX =
%r{
  #{CODE_REGEX} | #{INLINE_CODE_REGEX} | #{HTML_BLOCK_REGEX} | #{QUOTE_BLOCK_REGEX}
}mix

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_definitions, keep_actions: false) ⇒ Extractor

Returns a new instance of Extractor.



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

def initialize(command_definitions, keep_actions: false)
  @command_definitions = command_definitions
  @commands_regex = {}
  @keep_actions = keep_actions
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

#keep_actionsObject (readonly)

Returns the value of attribute keep_actions.



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

def keep_actions
  @keep_actions
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 original command text and arguments are removed from the given ‘content`, unless `keep_actions` is true.

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”

extractor = Gitlab::QuickActions::Extractor.new([:open, :assign, :labels], keep_actions: true) msg = %(hellon/labels ~foo ~“bar baz”nworld) commands = extractor.extract_commands(msg) #=> [[‘labels’, ‘~foo ~“bar baz”’]] msg #=> “hellon/labels ~foo ~”bar baz“nnworld” “‘



95
96
97
98
99
# File 'lib/gitlab/quick_actions/extractor.rb', line 95

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`



105
106
107
108
109
110
111
# File 'lib/gitlab/quick_actions/extractor.rb', line 105

def redact_commands(content)
  return "" unless content

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

  content
end