2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-26 08:21:53 -04:00
|
|
|
module Projects
|
|
|
|
class CreateFromTemplateService < BaseService
|
2018-07-20 19:25:10 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2019-10-08 20:06:06 -04:00
|
|
|
attr_reader :template_name
|
|
|
|
|
2017-07-26 08:21:53 -04:00
|
|
|
def initialize(user, params)
|
2019-01-15 16:05:36 -05:00
|
|
|
@current_user, @params = user, params.to_h.dup
|
2019-10-08 20:06:06 -04:00
|
|
|
@template_name = @params.delete(:template_name).presence
|
2017-07-26 08:21:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2019-09-16 20:06:11 -04:00
|
|
|
return project unless validate_template!
|
|
|
|
|
|
|
|
file = built_in_template&.file
|
2018-02-12 08:57:25 -05:00
|
|
|
|
2018-04-19 11:24:32 -04:00
|
|
|
override_params = params.dup
|
2018-02-12 08:57:25 -05:00
|
|
|
params[:file] = file
|
|
|
|
|
2018-04-19 11:24:32 -04:00
|
|
|
GitlabProjectsImportService.new(current_user, params, override_params).execute
|
2017-08-01 08:34:11 -04:00
|
|
|
ensure
|
2018-02-12 08:57:25 -05:00
|
|
|
file&.close
|
2017-07-26 08:21:53 -04:00
|
|
|
end
|
2018-07-20 19:25:10 -04:00
|
|
|
|
2019-09-16 20:06:11 -04: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 08:21:53 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Projects::CreateFromTemplateService.prepend_if_ee('EE::Projects::CreateFromTemplateService')
|