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

62 lines
1.5 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)
2021-04-29 21:17:54 +05:30
@current_user = user
@params = 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!
2021-01-03 14:25:43 +05:30
file = built_in_template&.file || sample_data_template&.file
2018-03-27 19:54:05 +05:30
2018-05-09 12:01:36 +05:30
override_params = params.dup
2021-01-03 14:25:43 +05:30
if built_in_template
params[:file] = built_in_template.file
elsif sample_data_template
params[:file] = sample_data_template.file
params[:sample_data] = true
end
2018-03-27 19:54:05 +05:30
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!
2021-01-03 14:25:43 +05:30
return true if built_in_template || sample_data_template
2019-12-04 20:38:33 +05:30
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
2021-01-03 14:25:43 +05:30
def sample_data_template
strong_memoize(:sample_data_template) do
Gitlab::SampleDataTemplate.find(template_name)
end
end
2019-12-04 20:38:33 +05:30
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
2021-06-08 01:23:25 +05:30
Projects::CreateFromTemplateService.prepend_mod_with('Projects::CreateFromTemplateService')