Commit a61456e4 authored by James Lopez's avatar James Lopez

refactored import to use shared error stuff and fixed a few issues with recent refactorings

parent 27867f35
......@@ -13,7 +13,6 @@ class ProjectImportWorker
project_path: path)
if project
project.repository.after_import
project.import_finish
else
logger.error("There was an error during the import: #{tmp_file}")
end
......
......@@ -2,6 +2,8 @@ module Gitlab
module ImportExport
class ImportExportReader
attr_reader :tree
def initialize(config: 'lib/gitlab/import_export/import_export.yml', shared:)
@shared = shared
config_hash = YAML.load_file(config).deep_symbolize_keys
......
......@@ -10,12 +10,12 @@ module Gitlab
@archive_file = archive_file
@current_user = owner
@namespace = Namespace.find(namespace_id)
@project_path = project_path
@shared = Gitlab::ImportExport::Shared.new(relative_path: path_with_namespace, project_path: project_path)
end
def execute
Gitlab::ImportExport::Importer.import(archive_file: @archive_file,
storage_path: storage_path)
shared: @shared)
project_tree.project if [restore_project_tree, restore_repo, restore_wiki_repo].all?
end
......@@ -26,36 +26,33 @@ module Gitlab
end
def project_tree
@project_tree ||= Gitlab::ImportExport::ProjectTreeRestorer.new(path: storage_path,
user: @current_user,
project_path: @project_path,
@project_tree ||= Gitlab::ImportExport::ProjectTreeRestorer.new(user: @current_user,
shared: @shared,
namespace_id: @namespace.id)
end
def restore_repo
Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: repo_path,
shared: @shared,
project: project_tree.project).restore
end
def restore_wiki_repo
Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: wiki_repo_path,
shared: @shared,
project: ProjectWiki.new(project_tree.project)).restore
end
def storage_path
@storage_path ||= Gitlab::ImportExport.export_path(relative_path: path_with_namespace)
end
def path_with_namespace
File.join(@namespace.path, @project_path)
end
def repo_path
File.join(storage_path, 'project.bundle')
File.join(@shared.export_path, 'project.bundle')
end
def wiki_repo_path
File.join(storage_path, 'project.wiki.bundle')
File.join(@shared.export_path, 'project.wiki.bundle')
end
end
end
......
......@@ -7,20 +7,23 @@ module Gitlab
new(*args).import
end
def initialize(archive_file: , storage_path:)
def initialize(archive_file: , shared:)
@archive_file = archive_file
@storage_path = storage_path
@shared = shared
end
def import
FileUtils.mkdir_p(@storage_path)
FileUtils.mkdir_p(@shared.storage_path)
decompress_archive
rescue => e
@shared.error(e.message)
false
end
private
def decompress_archive
untar_zxf(archive: @archive_file, dir: @storage_path)
untar_zxf(archive: @archive_file, dir: @shared.storage_path)
end
end
end
......
......@@ -2,11 +2,12 @@ module Gitlab
module ImportExport
class ProjectTreeRestorer
def initialize(path:, user:, project_path:, namespace_id:)
@path = File.join(path, 'project.json')
def initialize(user:, shared:, namespace_id:)
@path = File.join(shared.export_path, 'project.json')
@user = user
@project_path = project_path
@project_path = shared.opts[:project_path]
@namespace_id = namespace_id
@shared = shared
end
def restore
......@@ -15,7 +16,7 @@ module Gitlab
@project_members = @tree_hash.delete('project_members')
create_relations
rescue => e
# TODO: handle errors better, move them to a shared thing
@shared.error(e.message)
false
end
......@@ -44,7 +45,9 @@ module Gitlab
end
def default_relation_list
Gitlab::ImportExport::ImportExportReader.tree.reject { |model| model.is_a?(Hash) && model[:project_members] }
Gitlab::ImportExport::ImportExportReader.new(shared: @shared).tree.reject do |model|
model.is_a?(Hash) && model[:project_members]
end
end
def create_project
......
......@@ -3,9 +3,10 @@ module Gitlab
class RepoRestorer
include Gitlab::ImportExport::CommandLineUtil
def initialize(project:, path_to_bundle:)
def initialize(project:, shared:, path_to_bundle:)
@project = project
@path_to_bundle = path_to_bundle
@shared = shared
end
def restore
......@@ -15,7 +16,8 @@ module Gitlab
FileUtils.mkdir_p(path_to_repo)
git_unbundle(repo_path: path_to_repo, bundle_path: @path_to_bundle)
rescue
rescue => e
@shared.error(e.message)
false
end
......
......@@ -2,7 +2,7 @@ module Gitlab
module ImportExport
class Shared
attr_reader :errors
attr_reader :errors, :opts
def initialize(opts)
@opts = opts
......
......@@ -5,7 +5,8 @@ describe Gitlab::ImportExport::ProjectTreeRestorer, services: true do
let(:user) { create(:user) }
let(:namespace) { create(:namespace, owner: user) }
let(:project_tree_restorer) { described_class.new(path: Rails.root.join("spec/lib/gitlab/import_export/"), user: user, project_path: 'project', namespace_id: namespace.id) }
let(:shared) { Gitlab::ImportExport::Shared.new(relative_path: "../../../spec/lib/gitlab/import_export/", project_path: 'path') }
let(:project_tree_restorer) { described_class.new(user: user, shared: shared, namespace_id: namespace.id) }
context 'JSON' do
let(:restored_project_json) do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment