2018-11-17 00:37:17 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-15 16:14:28 +00:00
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class Importer
|
2018-04-06 15:23:49 +00:00
|
|
|
include Gitlab::Allowable
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2017-11-15 12:27:37 +00:00
|
|
|
def self.imports_repository?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-06-14 10:47:07 +00:00
|
|
|
def initialize(project)
|
|
|
|
@archive_file = project.import_source
|
|
|
|
@current_user = project.creator
|
2016-06-14 18:32:19 +00:00
|
|
|
@project = project
|
2018-03-05 19:53:40 +00:00
|
|
|
@shared = project.import_export_shared
|
2016-04-15 16:14:28 +00:00
|
|
|
end
|
|
|
|
|
2016-06-14 10:47:07 +00:00
|
|
|
def execute
|
2018-04-06 15:23:49 +00:00
|
|
|
if import_file && check_version! && restorers.all?(&:restore) && overwrite_project
|
2016-06-14 18:32:19 +00:00
|
|
|
project_tree.restored_project
|
2016-06-14 10:47:07 +00:00
|
|
|
else
|
2016-06-15 15:31:00 +00:00
|
|
|
raise Projects::ImportService::Error.new(@shared.errors.join(', '))
|
2016-06-14 10:47:07 +00:00
|
|
|
end
|
2018-04-06 15:23:49 +00:00
|
|
|
rescue => e
|
|
|
|
raise Projects::ImportService::Error.new(e.message)
|
|
|
|
ensure
|
2016-06-24 08:50:23 +00:00
|
|
|
remove_import_file
|
2016-04-15 16:14:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-03-30 17:45:58 +00:00
|
|
|
def restorers
|
|
|
|
[repo_restorer, wiki_restorer, project_tree, avatar_restorer,
|
2018-04-06 15:23:49 +00:00
|
|
|
uploads_restorer, lfs_restorer, statistics_restorer]
|
2018-03-30 17:45:58 +00:00
|
|
|
end
|
|
|
|
|
2016-06-24 08:50:23 +00:00
|
|
|
def import_file
|
2018-07-26 10:55:21 +00:00
|
|
|
Gitlab::ImportExport::FileImporter.import(project: @project,
|
|
|
|
archive_file: @archive_file,
|
2016-06-24 08:50:23 +00:00
|
|
|
shared: @shared)
|
|
|
|
end
|
|
|
|
|
2016-06-14 10:47:07 +00:00
|
|
|
def check_version!
|
|
|
|
Gitlab::ImportExport::VersionChecker.check!(shared: @shared)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_tree
|
|
|
|
@project_tree ||= Gitlab::ImportExport::ProjectTreeRestorer.new(user: @current_user,
|
|
|
|
shared: @shared,
|
|
|
|
project: @project)
|
|
|
|
end
|
|
|
|
|
2016-07-15 14:03:31 +00:00
|
|
|
def avatar_restorer
|
|
|
|
Gitlab::ImportExport::AvatarRestorer.new(project: project_tree.restored_project, shared: @shared)
|
|
|
|
end
|
|
|
|
|
2016-06-14 10:47:07 +00:00
|
|
|
def repo_restorer
|
|
|
|
Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: repo_path,
|
|
|
|
shared: @shared,
|
2016-06-14 18:32:19 +00:00
|
|
|
project: project_tree.restored_project)
|
2016-06-14 10:47:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def wiki_restorer
|
2018-02-12 15:38:10 +00:00
|
|
|
Gitlab::ImportExport::WikiRestorer.new(path_to_bundle: wiki_repo_path,
|
2016-06-14 10:47:07 +00:00
|
|
|
shared: @shared,
|
2018-02-12 15:38:10 +00:00
|
|
|
project: ProjectWiki.new(project_tree.restored_project),
|
|
|
|
wiki_enabled: @project.wiki_enabled?)
|
2016-06-14 10:47:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def uploads_restorer
|
2016-06-14 18:32:19 +00:00
|
|
|
Gitlab::ImportExport::UploadsRestorer.new(project: project_tree.restored_project, shared: @shared)
|
2016-06-14 10:47:07 +00:00
|
|
|
end
|
|
|
|
|
2018-03-30 17:45:58 +00:00
|
|
|
def lfs_restorer
|
|
|
|
Gitlab::ImportExport::LfsRestorer.new(project: project_tree.restored_project, shared: @shared)
|
|
|
|
end
|
|
|
|
|
2018-04-06 15:23:49 +00:00
|
|
|
def statistics_restorer
|
|
|
|
Gitlab::ImportExport::StatisticsRestorer.new(project: project_tree.restored_project, shared: @shared)
|
|
|
|
end
|
|
|
|
|
2016-06-14 18:32:19 +00:00
|
|
|
def path_with_namespace
|
2017-02-14 14:03:24 +00:00
|
|
|
File.join(@project.namespace.full_path, @project.path)
|
2016-06-14 10:47:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def repo_path
|
|
|
|
File.join(@shared.export_path, 'project.bundle')
|
|
|
|
end
|
|
|
|
|
|
|
|
def wiki_repo_path
|
|
|
|
File.join(@shared.export_path, 'project.wiki.bundle')
|
|
|
|
end
|
2016-06-24 08:50:23 +00:00
|
|
|
|
|
|
|
def remove_import_file
|
2018-07-26 10:55:21 +00:00
|
|
|
upload = @project.import_export_upload
|
|
|
|
|
|
|
|
return unless upload&.import_file&.file
|
|
|
|
|
|
|
|
upload.remove_import_file!
|
|
|
|
upload.save!
|
2016-06-24 08:50:23 +00:00
|
|
|
end
|
2018-04-06 15:23:49 +00:00
|
|
|
|
|
|
|
def overwrite_project
|
|
|
|
project = project_tree.restored_project
|
|
|
|
|
|
|
|
return unless can?(@current_user, :admin_namespace, project.namespace)
|
|
|
|
|
|
|
|
if overwrite_project?
|
|
|
|
::Projects::OverwriteProjectService.new(project, @current_user)
|
|
|
|
.execute(project_to_overwrite)
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def original_path
|
|
|
|
@project.import_data&.data&.fetch('original_path', nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def overwrite_project?
|
|
|
|
original_path.present? && project_to_overwrite.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_to_overwrite
|
|
|
|
strong_memoize(:project_to_overwrite) do
|
|
|
|
Project.find_by_full_path("#{@project.namespace.full_path}/#{original_path}")
|
|
|
|
end
|
|
|
|
end
|
2016-04-15 16:14:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|