2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-21 07:47:18 -05:00
|
|
|
module Gitlab
|
2017-05-31 01:50:53 -04:00
|
|
|
module SlashCommands
|
2016-11-21 07:47:18 -05:00
|
|
|
class Deploy < BaseCommand
|
|
|
|
def self.match(text)
|
2016-11-30 09:51:48 -05:00
|
|
|
/\Adeploy\s+(?<from>\S+.*)\s+to+\s+(?<to>\S+.*)\z/.match(text)
|
2016-11-21 07:47:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.help_message
|
|
|
|
'deploy <environment> to <target-environment>'
|
|
|
|
end
|
|
|
|
|
2016-11-21 11:26:35 -05:00
|
|
|
def self.available?(project)
|
|
|
|
project.builds_enabled?
|
|
|
|
end
|
|
|
|
|
2016-11-21 07:47:18 -05:00
|
|
|
def self.allowed?(project, user)
|
|
|
|
can?(user, :create_deployment, project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(match)
|
|
|
|
from = match[:from]
|
|
|
|
to = match[:to]
|
|
|
|
|
2017-07-28 04:56:50 -04:00
|
|
|
action = find_action(from, to)
|
2016-11-21 07:47:18 -05:00
|
|
|
|
2017-07-28 04:56:50 -04:00
|
|
|
if action.nil?
|
|
|
|
Gitlab::SlashCommands::Presenters::Deploy
|
|
|
|
.new(action).action_not_found
|
2016-11-21 07:47:18 -05:00
|
|
|
else
|
2017-07-28 04:56:50 -04:00
|
|
|
deployment = action.play(current_user)
|
|
|
|
|
|
|
|
Gitlab::SlashCommands::Presenters::Deploy
|
|
|
|
.new(deployment).present(from, to)
|
2016-11-21 07:47:18 -05:00
|
|
|
end
|
|
|
|
end
|
2016-11-21 11:26:35 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-07-28 04:56:50 -04:00
|
|
|
def find_action(from, to)
|
2016-11-21 11:26:35 -05:00
|
|
|
environment = project.environments.find_by(name: from)
|
2017-07-28 04:56:50 -04:00
|
|
|
return unless environment
|
2016-11-21 11:26:35 -05:00
|
|
|
|
2017-07-28 04:56:50 -04:00
|
|
|
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
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-11-21 07:47:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|