BigW Consortium Gitlab

deploy.rb 1.26 KB
Newer Older
1
module Gitlab
2
  module SlashCommands
3 4
    class Deploy < BaseCommand
      def self.match(text)
5
        /\Adeploy\s+(?<from>\S+.*)\s+to+\s+(?<to>\S+.*)\z/.match(text)
6 7 8 9 10 11
      end

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

12 13 14 15
      def self.available?(project)
        project.builds_enabled?
      end

16 17 18 19 20 21 22 23
      def self.allowed?(project, user)
        can?(user, :create_deployment, project)
      end

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

24
        action = find_action(from, to)
25

26 27 28
        if action.nil?
          Gitlab::SlashCommands::Presenters::Deploy
            .new(action).action_not_found
29
        else
30 31 32 33
          deployment = action.play(current_user)

          Gitlab::SlashCommands::Presenters::Deploy
            .new(deployment).present(from, to)
34 35
        end
      end
36 37 38

      private

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

43 44 45 46 47 48 49 50 51
        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
52
      end
53 54 55
    end
  end
end