2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
module ImportExport
|
|
|
|
module CommonUtil
|
|
|
|
def setup_symlink(tmpdir, symlink_name)
|
2020-03-13 15:44:24 +05:30
|
|
|
allow_next_instance_of(Gitlab::ImportExport) do |instance|
|
|
|
|
allow(instance).to receive(:storage_path).and_return(tmpdir)
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
File.open("#{tmpdir}/test", 'w') { |file| file.write("test") }
|
|
|
|
FileUtils.ln_s("#{tmpdir}/test", "#{tmpdir}/#{symlink_name}")
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
def setup_import_export_config(name, prefix = nil)
|
|
|
|
export_path = [prefix, 'spec', 'fixtures', 'lib', 'gitlab', 'import_export', name].compact
|
|
|
|
export_path = File.join(*export_path)
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
allow(Gitlab::ImportExport).to receive(:export_path) { export_path }
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def setup_reader(reader)
|
2020-06-23 00:09:42 +05:30
|
|
|
if reader == :ndjson_reader && Feature.enabled?(:project_import_ndjson, default_enabled: true)
|
2021-09-04 01:27:46 +05:30
|
|
|
allow_any_instance_of(Gitlab::ImportExport::Json::LegacyReader::File).to receive(:exist?).and_return(false)
|
|
|
|
allow_any_instance_of(Gitlab::ImportExport::Json::NdjsonReader).to receive(:exist?).and_return(true)
|
2020-04-22 19:07:51 +05:30
|
|
|
else
|
2021-09-04 01:27:46 +05:30
|
|
|
allow_any_instance_of(Gitlab::ImportExport::Json::LegacyReader::File).to receive(:exist?).and_return(true)
|
|
|
|
allow_any_instance_of(Gitlab::ImportExport::Json::NdjsonReader).to receive(:exist?).and_return(false)
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def fixtures_path
|
|
|
|
"spec/fixtures/lib/gitlab/import_export"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tmp_path
|
|
|
|
"tmp/tests/gitlab-test/import_export"
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def get_json(path, exportable_path, key, ndjson_enabled)
|
|
|
|
if ndjson_enabled
|
|
|
|
json = if key == :projects
|
|
|
|
consume_attributes(path, exportable_path)
|
|
|
|
else
|
|
|
|
consume_relations(path, exportable_path, key)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
json = project_json(path)
|
|
|
|
json = json[key.to_s] unless key == :projects
|
|
|
|
end
|
|
|
|
|
|
|
|
json
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def restore_then_save_project(project, import_path:, export_path:)
|
|
|
|
project_restorer = get_project_restorer(project, import_path)
|
|
|
|
project_saver = get_project_saver(project, export_path)
|
|
|
|
|
|
|
|
project_restorer.restore && project_saver.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_project_restorer(project, import_path)
|
2020-04-08 14:13:33 +05:30
|
|
|
Gitlab::ImportExport::Project::TreeRestorer.new(
|
2020-03-13 15:44:24 +05:30
|
|
|
user: project.creator, shared: get_shared_env(path: import_path), project: project
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_project_saver(project, export_path)
|
2020-04-08 14:13:33 +05:30
|
|
|
Gitlab::ImportExport::Project::TreeSaver.new(
|
2020-03-13 15:44:24 +05:30
|
|
|
project: project, current_user: project.creator, shared: get_shared_env(path: export_path)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def get_shared_env(path:, logger: nil)
|
|
|
|
logger ||= double(info: true, warn: true, error: true)
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
instance_double(Gitlab::ImportExport::Shared).tap do |shared|
|
|
|
|
allow(shared).to receive(:export_path).and_return(path)
|
2021-02-22 17:27:13 +05:30
|
|
|
allow(shared).to receive(:logger).and_return(logger)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
def consume_attributes(dir_path, exportable_path)
|
|
|
|
path = File.join(dir_path, "#{exportable_path}.json")
|
|
|
|
return unless File.exist?(path)
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
Gitlab::Json.parse(IO.read(path))
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def consume_relations(dir_path, exportable_path, key)
|
|
|
|
path = File.join(dir_path, exportable_path, "#{key}.ndjson")
|
|
|
|
return unless File.exist?(path)
|
|
|
|
|
|
|
|
relations = []
|
|
|
|
|
|
|
|
File.foreach(path) do |line|
|
2021-10-27 15:23:28 +05:30
|
|
|
json = Gitlab::Json.parse(line)
|
2020-04-22 19:07:51 +05:30
|
|
|
relations << json
|
|
|
|
end
|
|
|
|
|
|
|
|
key == :project_feature ? relations.first : relations.flatten
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_json(filename)
|
2021-10-27 15:23:28 +05:30
|
|
|
Gitlab::Json.parse(IO.read(filename))
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|