Welcome to mirror list, hosted at ThFree Co, Russian Federation.

deploy.rb « slash_commands « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 16a4875be9190607f0d23d537fee1e66073b7196 (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
64
65
66
67
68
69
# frozen_string_literal: true

module Gitlab
  module SlashCommands
    class Deploy < BaseCommand
      DEPLOY_REGEX = /\Adeploy\s/.freeze

      def self.match(text)
        return unless text&.match?(DEPLOY_REGEX)

        from, _, to = text.sub(DEPLOY_REGEX, '').rpartition(/\sto+\s/)
        return if from.blank? || to.blank?

        {
          from: from.strip,
          to: to.strip
        }
      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

      # rubocop: disable CodeReuse/ActiveRecord
      def find_action(from, to)
        environment = project.environments.find_by(name: from)
        return unless environment

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

        if actions.many?
          actions.find { |action| action.name == to.to_s }
        else
          actions.first
        end
      end
      # rubocop: enable CodeReuse/ActiveRecord
    end
  end
end