2018-07-17 16:50:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-26 12:21:53 +00:00
|
|
|
module Projects
|
|
|
|
class CreateFromTemplateService < BaseService
|
2018-07-20 23:25:10 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2019-10-09 00:06:06 +00:00
|
|
|
attr_reader :template_name
|
|
|
|
|
2017-07-26 12:21:53 +00:00
|
|
|
def initialize(user, params)
|
2019-01-15 21:05:36 +00:00
|
|
|
@current_user, @params = user, params.to_h.dup
|
2019-10-09 00:06:06 +00:00
|
|
|
@template_name = @params.delete(:template_name).presence
|
2017-07-26 12:21:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2019-09-17 00:06:11 +00:00
|
|
|
return project unless validate_template!
|
|
|
|
|
|
|
|
file = built_in_template&.file
|
2018-02-12 13:57:25 +00:00
|
|
|
|
2018-04-19 15:24:32 +00:00
|
|
|
override_params = params.dup
|
2018-02-12 13:57:25 +00:00
|
|
|
params[:file] = file
|
|
|
|
|
2018-04-19 15:24:32 +00:00
|
|
|
GitlabProjectsImportService.new(current_user, params, override_params).execute
|
2017-08-01 12:34:11 +00:00
|
|
|
ensure
|
2018-02-12 13:57:25 +00:00
|
|
|
file&.close
|
2017-07-26 12:21:53 +00:00
|
|
|
end
|
2018-07-20 23:25:10 +00:00
|
|
|
|
2019-09-17 00:06:11 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def validate_template!
|
|
|
|
return true if built_in_template
|
|
|
|
|
|
|
|
project.errors.add(:template_name, _("'%{template_name}' is unknown or invalid" % { template_name: template_name }))
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def built_in_template
|
|
|
|
strong_memoize(:built_in_template) do
|
|
|
|
Gitlab::ProjectTemplate.find(template_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
@project ||= ::Project.new(namespace_id: params[:namespace_id])
|
|
|
|
end
|
2017-07-26 12:21:53 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
Projects::CreateFromTemplateService.prepend_if_ee('EE::Projects::CreateFromTemplateService')
|