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 ...@@ -13,7 +13,6 @@ class ProjectImportWorker
project_path: path) project_path: path)
if project if project
project.repository.after_import project.repository.after_import
project.import_finish
else else
logger.error("There was an error during the import: #{tmp_file}") logger.error("There was an error during the import: #{tmp_file}")
end end
......
...@@ -2,6 +2,8 @@ module Gitlab ...@@ -2,6 +2,8 @@ module Gitlab
module ImportExport module ImportExport
class ImportExportReader class ImportExportReader
attr_reader :tree
def initialize(config: 'lib/gitlab/import_export/import_export.yml', shared:) def initialize(config: 'lib/gitlab/import_export/import_export.yml', shared:)
@shared = shared @shared = shared
config_hash = YAML.load_file(config).deep_symbolize_keys config_hash = YAML.load_file(config).deep_symbolize_keys
......
...@@ -10,12 +10,12 @@ module Gitlab ...@@ -10,12 +10,12 @@ module Gitlab
@archive_file = archive_file @archive_file = archive_file
@current_user = owner @current_user = owner
@namespace = Namespace.find(namespace_id) @namespace = Namespace.find(namespace_id)
@project_path = project_path @shared = Gitlab::ImportExport::Shared.new(relative_path: path_with_namespace, project_path: project_path)
end end
def execute def execute
Gitlab::ImportExport::Importer.import(archive_file: @archive_file, 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? project_tree.project if [restore_project_tree, restore_repo, restore_wiki_repo].all?
end end
...@@ -26,36 +26,33 @@ module Gitlab ...@@ -26,36 +26,33 @@ module Gitlab
end end
def project_tree def project_tree
@project_tree ||= Gitlab::ImportExport::ProjectTreeRestorer.new(path: storage_path, @project_tree ||= Gitlab::ImportExport::ProjectTreeRestorer.new(user: @current_user,
user: @current_user, shared: @shared,
project_path: @project_path,
namespace_id: @namespace.id) namespace_id: @namespace.id)
end end
def restore_repo def restore_repo
Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: repo_path, Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: repo_path,
shared: @shared,
project: project_tree.project).restore project: project_tree.project).restore
end end
def restore_wiki_repo def restore_wiki_repo
Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: wiki_repo_path, Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: wiki_repo_path,
shared: @shared,
project: ProjectWiki.new(project_tree.project)).restore project: ProjectWiki.new(project_tree.project)).restore
end end
def storage_path
@storage_path ||= Gitlab::ImportExport.export_path(relative_path: path_with_namespace)
end
def path_with_namespace def path_with_namespace
File.join(@namespace.path, @project_path) File.join(@namespace.path, @project_path)
end end
def repo_path def repo_path
File.join(storage_path, 'project.bundle') File.join(@shared.export_path, 'project.bundle')
end end
def wiki_repo_path def wiki_repo_path
File.join(storage_path, 'project.wiki.bundle') File.join(@shared.export_path, 'project.wiki.bundle')
end end
end end
end end
......
...@@ -7,20 +7,23 @@ module Gitlab ...@@ -7,20 +7,23 @@ module Gitlab
new(*args).import new(*args).import
end end
def initialize(archive_file: , storage_path:) def initialize(archive_file: , shared:)
@archive_file = archive_file @archive_file = archive_file
@storage_path = storage_path @shared = shared
end end
def import def import
FileUtils.mkdir_p(@storage_path) FileUtils.mkdir_p(@shared.storage_path)
decompress_archive decompress_archive
rescue => e
@shared.error(e.message)
false
end end
private private
def decompress_archive def decompress_archive
untar_zxf(archive: @archive_file, dir: @storage_path) untar_zxf(archive: @archive_file, dir: @shared.storage_path)
end end
end end
end end
......
...@@ -2,11 +2,12 @@ module Gitlab ...@@ -2,11 +2,12 @@ module Gitlab
module ImportExport module ImportExport
class ProjectTreeRestorer class ProjectTreeRestorer
def initialize(path:, user:, project_path:, namespace_id:) def initialize(user:, shared:, namespace_id:)
@path = File.join(path, 'project.json') @path = File.join(shared.export_path, 'project.json')
@user = user @user = user
@project_path = project_path @project_path = shared.opts[:project_path]
@namespace_id = namespace_id @namespace_id = namespace_id
@shared = shared
end end
def restore def restore
...@@ -15,7 +16,7 @@ module Gitlab ...@@ -15,7 +16,7 @@ module Gitlab
@project_members = @tree_hash.delete('project_members') @project_members = @tree_hash.delete('project_members')
create_relations create_relations
rescue => e rescue => e
# TODO: handle errors better, move them to a shared thing @shared.error(e.message)
false false
end end
...@@ -44,7 +45,9 @@ module Gitlab ...@@ -44,7 +45,9 @@ module Gitlab
end end
def default_relation_list 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 end
def create_project def create_project
......
...@@ -3,9 +3,10 @@ module Gitlab ...@@ -3,9 +3,10 @@ module Gitlab
class RepoRestorer class RepoRestorer
include Gitlab::ImportExport::CommandLineUtil include Gitlab::ImportExport::CommandLineUtil
def initialize(project:, path_to_bundle:) def initialize(project:, shared:, path_to_bundle:)
@project = project @project = project
@path_to_bundle = path_to_bundle @path_to_bundle = path_to_bundle
@shared = shared
end end
def restore def restore
...@@ -15,7 +16,8 @@ module Gitlab ...@@ -15,7 +16,8 @@ module Gitlab
FileUtils.mkdir_p(path_to_repo) FileUtils.mkdir_p(path_to_repo)
git_unbundle(repo_path: path_to_repo, bundle_path: @path_to_bundle) git_unbundle(repo_path: path_to_repo, bundle_path: @path_to_bundle)
rescue rescue => e
@shared.error(e.message)
false false
end end
......
...@@ -2,7 +2,7 @@ module Gitlab ...@@ -2,7 +2,7 @@ module Gitlab
module ImportExport module ImportExport
class Shared class Shared
attr_reader :errors attr_reader :errors, :opts
def initialize(opts) def initialize(opts)
@opts = opts @opts = opts
......
...@@ -5,7 +5,8 @@ describe Gitlab::ImportExport::ProjectTreeRestorer, services: true do ...@@ -5,7 +5,8 @@ describe Gitlab::ImportExport::ProjectTreeRestorer, services: true do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:namespace) { create(:namespace, owner: 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 context 'JSON' do
let(:restored_project_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