summaryrefslogtreecommitdiff
path: root/lib/gitlab/slash_commands/command.rb
blob: bb778f37096db2b9df9f3a1cdc85836655371556 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Gitlab
  module SlashCommands
    class Command < BaseCommand
      COMMANDS = [
        Gitlab::SlashCommands::IssueShow,
        Gitlab::SlashCommands::IssueNew,
        Gitlab::SlashCommands::IssueSearch,
        Gitlab::SlashCommands::IssueMove,
        Gitlab::SlashCommands::Deploy
      ].freeze

      def execute
        command, match = match_command

        if command
          if command.allowed?(project, current_user)
            command.new(project, chat_name, params).execute(match)
          else
            Gitlab::SlashCommands::Presenters::Access.new.access_denied
          end
        else
          Gitlab::SlashCommands::Help.new(project, chat_name, params)
            .execute(available_commands, params[:text])
        end
      end

      def match_command
        match = nil
        service =
          available_commands.find do |klass|
            match = klass.match(params[:text])
          end

        [service, match]
      end

      private

      def available_commands
        COMMANDS.select do |klass|
          klass.available?(project)
        end
      end
    end
  end
end