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

60 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-11-21 07:47:18 -05:00
module Gitlab
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
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]
action = find_action(from, to)
2016-11-21 07:47:18 -05:00
if action.nil?
Gitlab::SlashCommands::Presenters::Deploy
.new(action).action_not_found
2016-11-21 07:47:18 -05:00
else
deployment = action.play(current_user)
Gitlab::SlashCommands::Presenters::Deploy
.new(deployment).present(from, to)
2016-11-21 07:47:18 -05:00
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.starts_environment?
end
if actions.many?
actions.find { |action| action.name == to.to_s }
else
actions.first
end
end
# rubocop: enable CodeReuse/ActiveRecord
2016-11-21 07:47:18 -05:00
end
end
end