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
|
2020-06-23 00:09:42 +05:30
|
|
|
# If some exception was raised could mean that the SnippetsRepoRestorer
|
|
|
|
# was not called. This would leave us with snippets without a repository.
|
|
|
|
# This is a state we don't want them to be, so we better delete them.
|
|
|
|
remove_non_migrated_snippets
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
raise Projects::ImportService::Error.new(e.message)
|
|
|
|
ensure
|
2020-06-23 00:09:42 +05:30
|
|
|
remove_base_tmp_dir
|
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
|
2020-05-24 23:13:21 +05:30
|
|
|
[repo_restorer, wiki_restorer, project_tree, avatar_restorer, design_repo_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
|
2021-01-29 00:20:46 +05:30
|
|
|
@project_tree ||= project_tree_class.new(user: current_user,
|
|
|
|
shared: shared,
|
|
|
|
project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_tree_class
|
|
|
|
sample_data_template? ? Gitlab::ImportExport::Project::Sample::TreeRestorer : Gitlab::ImportExport::Project::TreeRestorer
|
|
|
|
end
|
|
|
|
|
|
|
|
def sample_data_template?
|
|
|
|
project&.import_data&.data&.dig('sample_data')
|
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,
|
2021-03-11 19:13:27 +05:30
|
|
|
importable: project)
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def wiki_restorer
|
2021-02-22 17:27:13 +05:30
|
|
|
Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: wiki_repo_path,
|
2019-12-21 20:55:43 +05:30
|
|
|
shared: shared,
|
2021-03-11 19:13:27 +05:30
|
|
|
importable: ProjectWiki.new(project))
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def design_repo_restorer
|
|
|
|
Gitlab::ImportExport::DesignRepoRestorer.new(path_to_bundle: design_repo_path,
|
|
|
|
shared: shared,
|
2021-03-11 19:13:27 +05:30
|
|
|
importable: project)
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
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
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def design_repo_path
|
|
|
|
File.join(shared.export_path, Gitlab::ImportExport.design_repo_bundle_filename)
|
|
|
|
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
|
2020-05-24 23:13:21 +05:30
|
|
|
return true unless overwrite_project?
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
unless can?(current_user, :admin_namespace, project.namespace)
|
|
|
|
message = "User #{current_user&.username} (#{current_user&.id}) cannot overwrite a project in #{project.namespace.path}"
|
|
|
|
@shared.error(::Projects::ImportService::PermissionError.new(message))
|
|
|
|
return false
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
::Projects::OverwriteProjectService.new(project, current_user)
|
|
|
|
.execute(project_to_overwrite)
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
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
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
def remove_base_tmp_dir
|
|
|
|
FileUtils.rm_rf(@shared.base_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_non_migrated_snippets
|
|
|
|
project
|
|
|
|
.snippets
|
|
|
|
.left_joins(:snippet_repository)
|
|
|
|
.where(snippet_repositories: { snippet_id: nil })
|
|
|
|
.delete_all
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|