BigW Consortium Gitlab

dsl.rb 2.76 KB
Newer Older
1 2 3 4 5 6
module Gitlab
  module SlashCommands
    module Dsl
      extend ActiveSupport::Concern

      included do
7 8
        cattr_accessor :command_definitions, instance_accessor: false do
          []
9 10
        end

11 12
        cattr_accessor :command_definitions_by_name, instance_accessor: false do
          {}
13 14 15 16
        end
      end

      class_methods do
17 18 19
        # Allows to give a description to the next slash command.
        # This description is shown in the autocomplete menu.
        # It accepts a block that will be evaluated with the context given to
20
        # `CommandDefintion#to_h`.
21 22 23 24 25 26 27 28 29 30 31
        #
        # Example:
        #
        #   desc do
        #     "This is a dynamic description for #{noteable.to_ability_name}"
        #   end
        #   command :command_key do |arguments|
        #     # Awesome code block
        #   end
        def desc(text = '', &block)
          @description = block_given? ? block : text
32 33
        end

34 35 36 37 38 39 40 41 42
        # Allows to define params for the next slash command.
        # These params are shown in the autocomplete menu.
        #
        # Example:
        #
        #   params "~label ~label2"
        #   command :command_key do |arguments|
        #     # Awesome code block
        #   end
43 44 45 46
        def params(*params)
          @params = params
        end

47 48 49
        # Allows to define conditions that must be met in order for the command
        # to be returned by `.command_names` & `.command_definitions`.
        # It accepts a block that will be evaluated with the context given to
50
        # `CommandDefintion#to_h`.
51
        #
52 53
        # Example:
        #
54 55 56
        #   condition do
        #     project.public?
        #   end
57 58 59
        #   command :command_key do |arguments|
        #     # Awesome code block
        #   end
60
        def condition(&block)
61
          @condition_block = block
62 63 64 65 66
        end

        # Registers a new command which is recognizeable from body of email or
        # comment.
        # It accepts aliases and takes a block.
67
        #
68 69 70 71 72
        # Example:
        #
        #   command :my_command, :alias_for_my_command do |arguments|
        #     # Awesome code block
        #   end
73
        def command(*command_names, &block)
74
          name, *aliases = command_names
75

76 77 78 79 80 81 82 83
          definition = CommandDefinition.new(
            name,
            aliases:          aliases,
            description:      @description,
            params:           @params,
            condition_block:  @condition_block,
            action_block:     block
          )
84 85 86 87 88 89

          self.command_definitions << definition

          definition.all_names.each do |name|
            self.command_definitions_by_name[name] = definition
          end
90 91 92

          @description = nil
          @params = nil
93
          @condition_block = nil
94 95 96 97 98
        end
      end
    end
  end
end