summaryrefslogtreecommitdiff
path: root/lib/gitlab/slash_commands/deploy.rb
blob: 93e00ab75a15e2a0d3afea2df9e534c26b5012c9 (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
module Gitlab
  module SlashCommands
    class Deploy < BaseCommand
      def self.match(text)
        /\Adeploy\s+(?<from>\S+.*)\s+to+\s+(?<to>\S+.*)\z/.match(text)
      end

      def self.help_message
        'deploy <environment> to <target-environment>'
      end

      def self.available?(project)
        project.builds_enabled?
      end

      def self.allowed?(project, user)
        can?(user, :create_deployment, project)
      end

      def execute(match)
        from = match[:from]
        to = match[:to]

        action = find_action(from, to)

        if action.nil?
          Gitlab::SlashCommands::Presenters::Deploy
            .new(action).action_not_found
        else
          deployment = action.play(current_user)

          Gitlab::SlashCommands::Presenters::Deploy
            .new(deployment).present(from, to)
        end
      end

      private

      def find_action(from, to)
        environment = project.environments.find_by(name: from)
        return unless environment

        actions = environment.actions_for(to).select do |action|
          action.starts_environment?
        end

        if actions.many?
          actions.find { |action| action.name == to.to_s }
        else
          actions.first
        end
      end
    end
  end
end