2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-26 08:21:53 -04:00
|
|
|
# This service is an adapter used to for the GitLab Import feature, and
|
|
|
|
# creating a project from a template.
|
|
|
|
# The latter will under the hood just import an archive supplied by GitLab.
|
|
|
|
module Projects
|
2017-08-01 08:34:11 -04:00
|
|
|
class GitlabProjectsImportService
|
2018-07-20 19:25:10 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
include Gitlab::TemplateHelper
|
|
|
|
|
2017-07-26 08:21:53 -04:00
|
|
|
attr_reader :current_user, :params
|
|
|
|
|
2018-03-29 07:57:21 -04:00
|
|
|
def initialize(user, import_params, override_params = nil)
|
|
|
|
@current_user, @params, @override_params = user, import_params.dup, override_params
|
2017-07-26 08:21:53 -04:00
|
|
|
end
|
|
|
|
|
2020-05-13 08:07:54 -04:00
|
|
|
def execute
|
|
|
|
prepare_template_environment(template_file)
|
2018-04-06 11:23:49 -04:00
|
|
|
|
2020-05-13 08:07:54 -04:00
|
|
|
prepare_import_params
|
2018-02-12 08:57:25 -05:00
|
|
|
|
2020-05-13 08:07:54 -04:00
|
|
|
::Projects::CreateService.new(current_user, params).execute
|
2017-07-26 08:21:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-04-06 11:23:49 -04:00
|
|
|
def overwrite_project?
|
2018-07-20 19:25:10 -04:00
|
|
|
overwrite? && project_with_same_full_path?
|
2018-04-06 11:23:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def project_with_same_full_path?
|
2020-04-23 14:09:46 -04:00
|
|
|
Project.find_by_full_path(project_path).present?
|
2018-04-06 11:23:49 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-04-06 11:23:49 -04:00
|
|
|
def current_namespace
|
2018-07-20 19:25:10 -04:00
|
|
|
strong_memoize(:current_namespace) do
|
2020-04-23 14:09:46 -04:00
|
|
|
Namespace.find_by(id: params[:namespace_id]) || current_user.namespace
|
2018-07-20 19:25:10 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-07-20 19:25:10 -04:00
|
|
|
|
2020-04-23 14:09:46 -04:00
|
|
|
def project_path
|
|
|
|
"#{current_namespace.full_path}/#{params[:path]}"
|
|
|
|
end
|
|
|
|
|
2018-07-20 19:25:10 -04:00
|
|
|
def overwrite?
|
|
|
|
strong_memoize(:overwrite) do
|
|
|
|
params.delete(:overwrite)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def template_file
|
|
|
|
strong_memoize(:template_file) do
|
|
|
|
params.delete(:file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_import_params
|
|
|
|
data = {}
|
|
|
|
data[:override_params] = @override_params if @override_params
|
|
|
|
|
|
|
|
if overwrite_project?
|
|
|
|
data[:original_path] = params[:path]
|
|
|
|
params[:path] += "-#{tmp_filename}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if template_file
|
2020-10-16 14:09:04 -04:00
|
|
|
data[:sample_data] = params.delete(:sample_data) if params.key?(:sample_data)
|
2018-07-20 19:25:10 -04:00
|
|
|
params[:import_type] = 'gitlab_project'
|
|
|
|
end
|
|
|
|
|
|
|
|
params[:import_data] = { data: data } if data.present?
|
2018-04-06 11:23:49 -04:00
|
|
|
end
|
2017-07-26 08:21:53 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Projects::GitlabProjectsImportService.prepend_if_ee('EE::Projects::GitlabProjectsImportService')
|