summaryrefslogtreecommitdiff
path: root/lib/gitlab/chat_commands/command.rb
blob: b0d3fdbc48ae321aa3981ee0d568ec0401ed2ae0 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module Gitlab
  module ChatCommands
    class Command < BaseCommand
      COMMANDS = [
        Gitlab::ChatCommands::IssueShow,
        Gitlab::ChatCommands::IssueCreate,
        Gitlab::ChatCommands::IssueSearch,
        Gitlab::ChatCommands::Deploy,
      ].freeze

      def execute
        command, match = match_command

        if command
          if command.allowed?(project, current_user)
            present command.new(project, current_user, params).execute(match)
          else
            access_denied
          end
        else
          help(help_messages)
        end
      end

      private

      def match_command
        match = nil
        service = available_commands.find do |klass|
          match = klass.match(command)
        end

        [service, match]
      end

      def help_messages
        available_commands.map(&:help_message)
      end

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

      def command
        params[:text]
      end

      def help(messages)
        Mattermost::Presenter.help(messages, params[:command])
      end

      def access_denied
        Mattermost::Presenter.access_denied
      end

      def present(resource)
        Mattermost::Presenter.present(resource)
      end
    end
  end
end