debian-mirror-gitlab/app/services/projects/create_from_template_service.rb

49 lines
1.2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module Projects
class CreateFromTemplateService < BaseService
2018-11-18 11:00:15 +05:30
include Gitlab::Utils::StrongMemoize
2019-12-21 20:55:43 +05:30
attr_reader :template_name
2017-09-10 17:25:29 +05:30
def initialize(user, params)
2019-03-02 22:35:43 +05:30
@current_user, @params = user, params.to_h.dup
2019-12-21 20:55:43 +05:30
@template_name = @params.delete(:template_name).presence
2017-09-10 17:25:29 +05:30
end
def execute
2019-12-04 20:38:33 +05:30
return project unless validate_template!
file = built_in_template&.file
2018-03-27 19:54:05 +05:30
2018-05-09 12:01:36 +05:30
override_params = params.dup
2018-03-27 19:54:05 +05:30
params[:file] = file
2018-05-09 12:01:36 +05:30
GitlabProjectsImportService.new(current_user, params, override_params).execute
2017-09-10 17:25:29 +05:30
ensure
2018-03-27 19:54:05 +05:30
file&.close
2017-09-10 17:25:29 +05:30
end
2018-11-18 11:00:15 +05:30
2019-12-04 20:38:33 +05:30
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-09-10 17:25:29 +05:30
end
end
2019-12-04 20:38:33 +05:30
Projects::CreateFromTemplateService.prepend_if_ee('EE::Projects::CreateFromTemplateService')