1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# frozen_string_literal: true
module ImportExport
module CommonUtil
def setup_symlink(tmpdir, symlink_name)
allow_next_instance_of(Gitlab::ImportExport) do |instance|
allow(instance).to receive(:storage_path).and_return(tmpdir)
end
File.open("#{tmpdir}/test", 'w') { |file| file.write("test") }
FileUtils.ln_s("#{tmpdir}/test", "#{tmpdir}/#{symlink_name}")
end
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)
allow(Gitlab::ImportExport).to receive(:export_path) { export_path }
end
def setup_reader
allow_any_instance_of(Gitlab::ImportExport::Json::NdjsonReader).to receive(:exist?).and_return(true)
end
def fixtures_path
"spec/fixtures/lib/gitlab/import_export"
end
def test_tmp_path
"tmp/tests/gitlab-test/import_export"
end
def get_json(path, exportable_path, key)
if key == :projects
consume_attributes(path, exportable_path)
else
consume_relations(path, exportable_path, key)
end
end
def restore_then_save_project(project, user, import_path:, export_path:)
project_restorer = get_project_restorer(project, user, import_path)
project_saver = get_project_saver(project, user, export_path)
project_restorer.restore && project_saver.save
end
def get_project_restorer(project, user, import_path)
Gitlab::ImportExport::Project::TreeRestorer.new(
user: user, shared: get_shared_env(path: import_path), project: project
)
end
def get_project_saver(project, user, export_path)
Gitlab::ImportExport::Project::TreeSaver.new(
project: project, current_user: user, shared: get_shared_env(path: export_path)
)
end
def get_shared_env(path:, logger: nil)
logger ||= double(info: true, warn: true, error: true)
instance_double(Gitlab::ImportExport::Shared).tap do |shared|
allow(shared).to receive(:export_path).and_return(path)
allow(shared).to receive(:logger).and_return(logger)
end
end
def consume_attributes(dir_path, exportable_path)
path = File.join(dir_path, "#{exportable_path}.json")
return unless File.exist?(path)
Gitlab::Json.parse(File.read(path))
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|
json = Gitlab::Json.parse(line)
relations << json
end
key == :project_feature ? relations.first : relations.flatten
end
def project_json(filename)
Gitlab::Json.parse(File.read(filename))
end
end
end
|