debian-mirror-gitlab/lib/gitlab/slash_commands/deploy.rb

60 lines
1.4 KiB
Ruby
Raw Normal View History

2018-12-23 12:14:25 +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
def self.match(text)
/\Adeploy\s+(?<from>\S+.*)\s+to+\s+(?<to>\S+.*)\z/.match(text)
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