2016-11-21 07:47:18 -05:00
|
|
|
module Gitlab
|
|
|
|
module ChatCommands
|
|
|
|
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]
|
|
|
|
|
2016-11-21 11:26:35 -05:00
|
|
|
actions = find_actions(from, to)
|
2016-11-21 07:47:18 -05:00
|
|
|
|
2017-01-10 13:43:58 -05:00
|
|
|
if actions.none?
|
|
|
|
Gitlab::ChatCommands::Presenters::Deploy.new(nil).no_actions
|
|
|
|
elsif actions.one?
|
|
|
|
action = play!(from, to, actions.first)
|
|
|
|
Gitlab::ChatCommands::Presenters::Deploy.new(action).present(from, to)
|
2016-11-21 07:47:18 -05:00
|
|
|
else
|
2017-01-10 13:43:58 -05:00
|
|
|
Gitlab::ChatCommands::Presenters::Deploy.new(actions).too_many_actions
|
2016-11-21 07:47:18 -05:00
|
|
|
end
|
|
|
|
end
|
2016-11-21 11:26:35 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-11-21 13:40:04 -05:00
|
|
|
def play!(from, to, action)
|
2017-01-10 13:43:58 -05:00
|
|
|
action.play(current_user)
|
2016-11-21 13:40:04 -05:00
|
|
|
end
|
|
|
|
|
2016-11-21 11:26:35 -05:00
|
|
|
def find_actions(from, to)
|
|
|
|
environment = project.environments.find_by(name: from)
|
2017-01-10 13:43:58 -05:00
|
|
|
return [] unless environment
|
2016-11-21 11:26:35 -05:00
|
|
|
|
|
|
|
environment.actions_for(to).select(&:starts_environment?)
|
|
|
|
end
|
2016-11-21 07:47:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|