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

81 lines
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
# 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
class GitlabProjectsImportService
2018-11-18 11:00:15 +05:30
include Gitlab::Utils::StrongMemoize
include Gitlab::TemplateHelper
2017-09-10 17:25:29 +05:30
attr_reader :current_user, :params
2018-05-09 12:01:36 +05:30
def initialize(user, import_params, override_params = nil)
2021-04-29 21:17:54 +05:30
@current_user = user
@params = import_params.dup
@override_params = override_params
2017-09-10 17:25:29 +05:30
end
def execute
2018-11-18 11:00:15 +05:30
prepare_template_environment(template_file)
2018-05-09 12:01:36 +05:30
2018-11-18 11:00:15 +05:30
prepare_import_params
2018-03-27 19:54:05 +05:30
::Projects::CreateService.new(current_user, params).execute
2017-09-10 17:25:29 +05:30
end
private
2018-05-09 12:01:36 +05:30
def overwrite_project?
2018-11-18 11:00:15 +05:30
overwrite? && project_with_same_full_path?
2018-05-09 12:01:36 +05:30
end
def project_with_same_full_path?
2020-05-24 23:13:21 +05:30
Project.find_by_full_path(project_path).present?
2018-05-09 12:01:36 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-05-09 12:01:36 +05:30
def current_namespace
2018-11-18 11:00:15 +05:30
strong_memoize(:current_namespace) do
2020-05-24 23:13:21 +05:30
Namespace.find_by(id: params[:namespace_id]) || current_user.namespace
2018-11-18 11:00:15 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
2020-05-24 23:13:21 +05:30
def project_path
"#{current_namespace.full_path}/#{params[:path]}"
end
2018-11-18 11:00:15 +05:30
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
2021-01-03 14:25:43 +05:30
data[:sample_data] = params.delete(:sample_data) if params.key?(:sample_data)
2018-11-18 11:00:15 +05:30
params[:import_type] = 'gitlab_project'
end
params[:import_data] = { data: data } if data.present?
2018-05-09 12:01:36 +05:30
end
2017-09-10 17:25:29 +05:30
end
end
2019-12-04 20:38:33 +05:30
Projects::GitlabProjectsImportService.prepend_if_ee('EE::Projects::GitlabProjectsImportService')