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

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
EXCLUSION_REGEX =
%r{#{INLINE_CODE_REGEX} | #{HTML_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.



40
41
42
43
44
# File 'lib/gitlab/quick_actions/extractor.rb', line 40

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.



38
39
40
# File 'lib/gitlab/quick_actions/extractor.rb', line 38

def command_definitions
  @command_definitions
end

#keep_actionsObject (readonly)

Returns the value of attribute keep_actions.



38
39
40
# File 'lib/gitlab/quick_actions/extractor.rb', line 38

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” “‘



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

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`



77
78
79
80
81
82
83
# File 'lib/gitlab/quick_actions/extractor.rb', line 77

def redact_commands(content)
  return "" unless content

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

  content
end