2018-07-17 16:50:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-26 12:21:53 +00: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 12:34:11 +00:00
|
|
|
class GitlabProjectsImportService
|
2018-07-20 23:25:10 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
include Gitlab::TemplateHelper
|
|
|
|
|
2017-07-26 12:21:53 +00:00
|
|
|
attr_reader :current_user, :params
|
|
|
|
|
2018-03-29 11:57:21 +00:00
|
|
|
def initialize(user, import_params, override_params = nil)
|
|
|
|
@current_user, @params, @override_params = user, import_params.dup, override_params
|
2017-07-26 12:21:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2018-07-26 10:55:21 +00:00
|
|
|
prepare_template_environment(template_file)
|
2018-04-06 15:23:49 +00:00
|
|
|
|
2018-07-20 23:25:10 +00:00
|
|
|
prepare_import_params
|
2018-02-12 13:57:25 +00:00
|
|
|
|
|
|
|
::Projects::CreateService.new(current_user, params).execute
|
2017-07-26 12:21:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-04-06 15:23:49 +00:00
|
|
|
def overwrite_project?
|
2018-07-20 23:25:10 +00:00
|
|
|
overwrite? && project_with_same_full_path?
|
2018-04-06 15:23:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def project_with_same_full_path?
|
|
|
|
Project.find_by_full_path("#{current_namespace.full_path}/#{params[:path]}").present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_namespace
|
2018-07-20 23:25:10 +00:00
|
|
|
strong_memoize(:current_namespace) do
|
|
|
|
Namespace.find_by(id: params[:namespace_id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
params[:import_type] = 'gitlab_project'
|
|
|
|
end
|
|
|
|
|
|
|
|
params[:import_data] = { data: data } if data.present?
|
2018-04-06 15:23:49 +00:00
|
|
|
end
|
2017-07-26 12:21:53 +00:00
|
|
|
end
|
|
|
|
end
|