debian-mirror-gitlab/lib/gitlab/import_export/importer.rb

142 lines
4.3 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +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
2019-12-21 20:55:43 +05:30
project
2016-06-22 15:30:34 +05:30
else
2019-12-21 20:55:43 +05:30
raise Projects::ImportService::Error.new(shared.errors.to_sentence)
2016-06-22 15:30:34 +05:30
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
2019-12-21 20:55:43 +05:30
attr_accessor :archive_file, :current_user, :project, :shared
2018-05-09 12:01:36 +05:30
def restorers
[repo_restorer, wiki_restorer, project_tree, avatar_restorer,
2020-04-08 14:13:33 +05:30
uploads_restorer, lfs_restorer, statistics_restorer, snippets_repo_restorer]
2018-05-09 12:01:36 +05:30
end
2016-08-24 12:49:21 +05:30
def import_file
2020-01-01 13:55:28 +05:30
Gitlab::ImportExport::FileImporter.import(importable: project,
2019-12-21 20:55:43 +05:30
archive_file: archive_file,
shared: shared)
2016-08-24 12:49:21 +05:30
end
2016-06-22 15:30:34 +05:30
def check_version!
2019-12-21 20:55:43 +05:30
Gitlab::ImportExport::VersionChecker.check!(shared: shared)
2016-06-22 15:30:34 +05:30
end
def project_tree
2020-04-08 14:13:33 +05:30
@project_tree ||= Gitlab::ImportExport::Project::TreeRestorer.new(user: current_user,
2019-12-21 20:55:43 +05:30
shared: shared,
project: project)
2016-06-22 15:30:34 +05:30
end
2016-08-24 12:49:21 +05:30
def avatar_restorer
2019-12-21 20:55:43 +05:30
Gitlab::ImportExport::AvatarRestorer.new(project: project, shared: shared)
2016-08-24 12:49:21 +05:30
end
2016-06-22 15:30:34 +05:30
def repo_restorer
Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: repo_path,
2019-12-21 20:55:43 +05:30
shared: shared,
project: project)
2016-06-22 15:30:34 +05:30
end
def wiki_restorer
2018-03-27 19:54:05 +05:30
Gitlab::ImportExport::WikiRestorer.new(path_to_bundle: wiki_repo_path,
2019-12-21 20:55:43 +05:30
shared: shared,
project: ProjectWiki.new(project),
wiki_enabled: project.wiki_enabled?)
2016-06-22 15:30:34 +05:30
end
def uploads_restorer
2019-12-21 20:55:43 +05:30
Gitlab::ImportExport::UploadsRestorer.new(project: project, shared: shared)
2016-06-22 15:30:34 +05:30
end
2018-05-09 12:01:36 +05:30
def lfs_restorer
2019-12-21 20:55:43 +05:30
Gitlab::ImportExport::LfsRestorer.new(project: project, shared: shared)
2018-05-09 12:01:36 +05:30
end
2020-04-08 14:13:33 +05:30
def snippets_repo_restorer
Gitlab::ImportExport::SnippetsRepoRestorer.new(project: project,
shared: shared,
user: current_user)
end
2018-05-09 12:01:36 +05:30
def statistics_restorer
2019-12-21 20:55:43 +05:30
Gitlab::ImportExport::StatisticsRestorer.new(project: project, shared: shared)
2018-05-09 12:01:36 +05:30
end
2016-06-22 15:30:34 +05:30
def path_with_namespace
2019-12-21 20:55:43 +05:30
File.join(project.namespace.full_path, project.path)
2016-06-22 15:30:34 +05:30
end
def repo_path
2019-12-21 20:55:43 +05:30
File.join(shared.export_path, Gitlab::ImportExport.project_bundle_filename)
2016-06-22 15:30:34 +05:30
end
def wiki_repo_path
2019-12-21 20:55:43 +05:30
File.join(shared.export_path, Gitlab::ImportExport.wiki_repo_bundle_filename)
2016-06-22 15:30:34 +05:30
end
2016-08-24 12:49:21 +05:30
def remove_import_file
2019-12-21 20:55:43 +05:30
upload = project.import_export_upload
2018-11-18 11:00:15 +05:30
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
2019-12-21 20:55:43 +05:30
return unless can?(current_user, :admin_namespace, project.namespace)
2018-05-09 12:01:36 +05:30
if overwrite_project?
2019-12-21 20:55:43 +05:30
::Projects::OverwriteProjectService.new(project, current_user)
2018-05-09 12:01:36 +05:30
.execute(project_to_overwrite)
end
true
end
def original_path
2019-12-21 20:55:43 +05:30
project.import_data&.data&.fetch('original_path', nil)
2018-05-09 12:01:36 +05:30
end
def overwrite_project?
original_path.present? && project_to_overwrite.present?
end
def project_to_overwrite
strong_memoize(:project_to_overwrite) do
2020-04-08 14:13:33 +05:30
::Project.find_by_full_path("#{project.namespace.full_path}/#{original_path}")
2018-05-09 12:01:36 +05:30
end
end
2016-06-22 15:30:34 +05:30
end
end
end
2019-12-21 20:55:43 +05:30
Gitlab::ImportExport::Importer.prepend_if_ee('EE::Gitlab::ImportExport::Importer')