Class: Gitlab::QuickActions::CommandDefinition

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

Direct Known Subclasses

SubstitutionDefinition

Constant Summary collapse

ParseError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = {}) ⇒ CommandDefinition

Returns a new instance of CommandDefinition.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gitlab/quick_actions/command_definition.rb', line 11

def initialize(name, attributes = {})
  @name = name

  @aliases = attributes[:aliases] || []
  @description = attributes[:description] || ''
  @warning = attributes[:warning] || ''
  @icon = attributes[:icon] || ''
  @explanation = attributes[:explanation] || ''
  @execution_message = attributes[:execution_message] || ''
  @params = attributes[:params] || []
  @condition_block = attributes[:condition_block]
  @parse_params_block = attributes[:parse_params_block]
  @action_block = attributes[:action_block]
  @types = attributes[:types] || []
end

Instance Attribute Details

#action_blockObject

Returns the value of attribute action_block.



8
9
10
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8

def action_block
  @action_block
end

#aliasesObject

Returns the value of attribute aliases.



8
9
10
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8

def aliases
  @aliases
end

#condition_blockObject

Returns the value of attribute condition_block.



8
9
10
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8

def condition_block
  @condition_block
end

#descriptionObject

Returns the value of attribute description.



8
9
10
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8

def description
  @description
end

#execution_messageObject

Returns the value of attribute execution_message.



8
9
10
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8

def execution_message
  @execution_message
end

#explanationObject

Returns the value of attribute explanation.



8
9
10
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8

def explanation
  @explanation
end

#iconObject

Returns the value of attribute icon.



8
9
10
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8

def icon
  @icon
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8

def name
  @name
end

#paramsObject

Returns the value of attribute params.



8
9
10
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8

def params
  @params
end

#parse_params_blockObject

Returns the value of attribute parse_params_block.



8
9
10
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8

def parse_params_block
  @parse_params_block
end

#typesObject

Returns the value of attribute types.



8
9
10
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8

def types
  @types
end

#warningObject

Returns the value of attribute warning.



8
9
10
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8

def warning
  @warning
end

Instance Method Details

#all_namesObject



27
28
29
# File 'lib/gitlab/quick_actions/command_definition.rb', line 27

def all_names
  [name, *aliases]
end

#available?(context) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/gitlab/quick_actions/command_definition.rb', line 35

def available?(context)
  return false unless valid_type?(context)
  return true unless condition_block

  context.instance_exec(&condition_block)
end

#execute(context, arg) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gitlab/quick_actions/command_definition.rb', line 64

def execute(context, arg)
  return if noop?

  count_commands_executed_in(context)

  return unless available?(context)

  execute_block(action_block, context, arg)
rescue ParseError
  # message propagation is handled in `execution_message`.
end

#execute_message(context, arg) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gitlab/quick_actions/command_definition.rb', line 76

def execute_message(context, arg)
  return if noop?
  return _('Could not apply %{name} command.') % { name: name } unless available?(context)

  if execution_message.respond_to?(:call)
    execute_block(execution_message, context, arg)
  else
    execution_message
  end
rescue ParseError => e
  format _('Could not apply %{name} command. %{message}.'), name: name, message: e.message
end

#explain(context, arg) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gitlab/quick_actions/command_definition.rb', line 42

def explain(context, arg)
  return unless available?(context)

  message = if explanation.respond_to?(:call)
              begin
                execute_block(explanation, context, arg)
              rescue ParseError => e
                format(_('Problem with %{name} command: %{message}.'), name: name, message: e.message)
              end
            else
              explanation
            end

  warning_text = if warning.respond_to?(:call)
                   execute_block(warning, context, arg)
                 else
                   warning
                 end

  warning.empty? ? message : "#{message} (#{warning_text})"
end

#noop?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/gitlab/quick_actions/command_definition.rb', line 31

def noop?
  action_block.nil?
end

#to_h(context) ⇒ Object



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
# File 'lib/gitlab/quick_actions/command_definition.rb', line 89

def to_h(context)
  desc = description
  if desc.respond_to?(:call)
    desc = begin
      context.instance_exec(&desc)
    rescue StandardError
      ''
    end
  end

  warn = warning
  if warn.respond_to?(:call)
    warn = begin
      context.instance_exec(&warn)
    rescue StandardError
      ''
    end
  end

  prms = params
  if prms.respond_to?(:call)
    prms = begin
      Array(context.instance_exec(&prms))
    rescue StandardError
      params
    end
  end

  {
    name: name,
    aliases: aliases,
    description: desc,
    warning: warn,
    icon: icon,
    params: prms
  }
end