gitlab-org--gitlab-foss/lib/gitlab/chat_commands/deploy.rb

58 lines
1.4 KiB
Ruby
Raw Normal View History

2016-11-21 07:47:18 -05:00
module Gitlab
module ChatCommands
class Deploy < BaseCommand
2016-11-21 13:40:04 -05:00
include Gitlab::Routing.url_helpers
2016-11-21 07:47:18 -05:00
def self.match(text)
/\Adeploy\s+(?<from>.*)\s+to+\s+(?<to>.*)\z/.match(text)
end
def self.help_message
'deploy <environment> to <target-environment>'
end
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]
actions = find_actions(from, to)
return unless actions.present?
2016-11-21 07:47:18 -05:00
if actions.one?
2016-11-21 13:40:04 -05:00
play!(from, to, actions.first)
2016-11-21 07:47:18 -05:00
else
Result.new(:error, 'Too many actions defined')
end
end
private
2016-11-21 13:40:04 -05:00
def play!(from, to, action)
new_action = action.play(current_user)
Result.new(:success, "Deployment from #{from} to #{to} started. Follow the progress: #{url(new_action)}.")
end
def find_actions(from, to)
environment = project.environments.find_by(name: from)
return unless environment
environment.actions_for(to).select(&:starts_environment?)
end
2016-11-21 13:40:04 -05:00
def url(subject)
polymorphic_url(
[ subject.project.namespace.becomes(Namespace), subject.project, subject ])
end
2016-11-21 07:47:18 -05:00
end
end
end