2018-12-23 12:14:25 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class Importer
|
2018-05-09 12:01:36 +05:30
|
|
|
include Gitlab::Allowable
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def self.imports_repository?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
def initialize(project)
|
|
|
|
@archive_file = project.import_source
|
|
|
|
@current_user = project.creator
|
|
|
|
@project = project
|
2018-03-27 19:54:05 +05:30
|
|
|
@shared = project.import_export_shared
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2018-05-09 12:01:36 +05:30
|
|
|
if import_file && check_version! && restorers.all?(&:restore) && overwrite_project
|
2016-06-22 15:30:34 +05:30
|
|
|
project_tree.restored_project
|
|
|
|
else
|
|
|
|
raise Projects::ImportService::Error.new(@shared.errors.join(', '))
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
rescue => e
|
|
|
|
raise Projects::ImportService::Error.new(e.message)
|
|
|
|
ensure
|
2016-08-24 12:49:21 +05:30
|
|
|
remove_import_file
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def restorers
|
|
|
|
[repo_restorer, wiki_restorer, project_tree, avatar_restorer,
|
|
|
|
uploads_restorer, lfs_restorer, statistics_restorer]
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def import_file
|
2018-11-18 11:00:15 +05:30
|
|
|
Gitlab::ImportExport::FileImporter.import(project: @project,
|
|
|
|
archive_file: @archive_file,
|
2016-08-24 12:49:21 +05:30
|
|
|
shared: @shared)
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
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-08-24 12:49:21 +05:30
|
|
|
def avatar_restorer
|
|
|
|
Gitlab::ImportExport::AvatarRestorer.new(project: project_tree.restored_project, shared: @shared)
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
def repo_restorer
|
|
|
|
Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: repo_path,
|
|
|
|
shared: @shared,
|
|
|
|
project: project_tree.restored_project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def wiki_restorer
|
2018-03-27 19:54:05 +05:30
|
|
|
Gitlab::ImportExport::WikiRestorer.new(path_to_bundle: wiki_repo_path,
|
2016-06-22 15:30:34 +05:30
|
|
|
shared: @shared,
|
2018-03-27 19:54:05 +05:30
|
|
|
project: ProjectWiki.new(project_tree.restored_project),
|
|
|
|
wiki_enabled: @project.wiki_enabled?)
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def uploads_restorer
|
|
|
|
Gitlab::ImportExport::UploadsRestorer.new(project: project_tree.restored_project, shared: @shared)
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def lfs_restorer
|
|
|
|
Gitlab::ImportExport::LfsRestorer.new(project: project_tree.restored_project, shared: @shared)
|
|
|
|
end
|
|
|
|
|
|
|
|
def statistics_restorer
|
|
|
|
Gitlab::ImportExport::StatisticsRestorer.new(project: project_tree.restored_project, shared: @shared)
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
def path_with_namespace
|
2017-08-17 22:00:37 +05:30
|
|
|
File.join(@project.namespace.full_path, @project.path)
|
2016-06-22 15:30:34 +05:30
|
|
|
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-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
def remove_import_file
|
2018-11-18 11:00:15 +05:30
|
|
|
upload = @project.import_export_upload
|
|
|
|
|
|
|
|
return unless upload&.import_file&.file
|
|
|
|
|
|
|
|
upload.remove_import_file!
|
|
|
|
upload.save!
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
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-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|