2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Gitlab
|
2017-09-10 17:25:29 +05:30
|
|
|
module SlashCommands
|
2017-08-17 22:00:37 +05:30
|
|
|
class Deploy < BaseCommand
|
2021-12-07 22:27:20 +05:30
|
|
|
DEPLOY_REGEX = /\Adeploy\s/.freeze
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.match(text)
|
2021-12-07 22:27:20 +05:30
|
|
|
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
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
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]
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
action = find_action(from, to)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
if action.nil?
|
|
|
|
Gitlab::SlashCommands::Presenters::Deploy
|
|
|
|
.new(action).action_not_found
|
2017-08-17 22:00:37 +05:30
|
|
|
else
|
2017-09-10 17:25:29 +05:30
|
|
|
deployment = action.play(current_user)
|
|
|
|
|
|
|
|
Gitlab::SlashCommands::Presenters::Deploy
|
|
|
|
.new(deployment).present(from, to)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
def find_action(from, to)
|
2017-08-17 22:00:37 +05:30
|
|
|
environment = project.environments.find_by(name: from)
|
2017-09-10 17:25:29 +05:30
|
|
|
return unless environment
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|