From 102074c80152a0d9b808f3eea78a195234ef80bb Mon Sep 17 00:00:00 2001 From: James Lopez Date: Thu, 2 Jun 2016 10:59:54 +0200 Subject: [PATCH] more and more refactoring --- lib/gitlab/import_export/import_service.rb | 22 ++++---- lib/gitlab/import_export/members_mapper.rb | 53 ++++++++++--------- .../import_export/project_tree_restorer.rb | 4 +- lib/gitlab/import_export/relation_factory.rb | 2 +- lib/gitlab/import_export/uploads_saver.rb | 4 -- ...version_restorer.rb => version_checker.rb} | 6 +-- 6 files changed, 43 insertions(+), 48 deletions(-) rename lib/gitlab/import_export/{version_restorer.rb => version_checker.rb} (91%) diff --git a/lib/gitlab/import_export/import_service.rb b/lib/gitlab/import_export/import_service.rb index acd734f0890..96cb8f4b174 100644 --- a/lib/gitlab/import_export/import_service.rb +++ b/lib/gitlab/import_export/import_service.rb @@ -16,7 +16,7 @@ module Gitlab def execute Gitlab::ImportExport::Importer.import(archive_file: @archive_file, shared: @shared) - if [restore_version, restore_project_tree, restore_repo, restore_wiki_repo, restore_uploads].all? + if check_version! && [project_tree, repo_restorer, wiki_restorer, uploads_restorer].all?(&:restore) project_tree.project else project_tree.project.destroy if project_tree.project @@ -26,12 +26,8 @@ module Gitlab private - def restore_version - Gitlab::ImportExport::VersionRestorer.restore(shared: @shared) - end - - def restore_project_tree - project_tree.restore + def check_version! + Gitlab::ImportExport::VersionChecker.check!(shared: @shared) end def project_tree @@ -40,20 +36,20 @@ module Gitlab namespace_id: @namespace.id) end - def restore_repo + def repo_restorer Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: repo_path, shared: @shared, - project: project_tree.project).restore + project: project_tree.project) end - def restore_wiki_repo + def wiki_restorer 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)) end - def restore_uploads - Gitlab::ImportExport::UploadsRestorer.restore(project: project_tree.project, shared: @shared) + def uploads_restorer + Gitlab::ImportExport::UploadsRestorer.new(project: project_tree.project, shared: @shared) end def path_with_namespace(project_path) diff --git a/lib/gitlab/import_export/members_mapper.rb b/lib/gitlab/import_export/members_mapper.rb index 9c47216eb8d..13fe68d5408 100644 --- a/lib/gitlab/import_export/members_mapper.rb +++ b/lib/gitlab/import_export/members_mapper.rb @@ -12,32 +12,27 @@ module Gitlab # This needs to run first, as second call would be from generate_map # which means project members already exist. - default_project_member - - @project_member_map = Hash.new do |_, key| - @note_member_list << key - default_project_member - end - - end - - def default_project_member - @default_project_member ||= - begin - default_member = ProjectMember.new(default_project_member_hash) - default_member.create! - default_member.user.id - end + ensure_default_member! end def map + @map ||= generate_map + end + + def default_user_id + @user.id + end + + private + + + def generate_map @map ||= begin - @exported_members.inject(@project_member_map) do |hash, member| + @exported_members.inject(missing_keys_tracking_hash) do |hash, member| existing_user = User.where(find_project_user_query(member)).first - if existing_user - old_user_id = member['user']['id'] - add_user_as_team_member(existing_user, member) + old_user_id = member['user']['id'] + if existing_user && add_user_as_team_member(existing_user, member).persisted? hash[old_user_id] = existing_user.id end hash @@ -45,21 +40,27 @@ module Gitlab end end - private + def missing_keys_tracking_hash + Hash.new do |_, key| + @note_member_list << key + @user.id + end + end + + def ensure_default_member! + ProjectMember.create!(user: @user, access_level: ProjectMember::MASTER, source_id: @project.id, importing: true) + end def add_user_as_team_member(existing_user, member) member['user'] = existing_user - ProjectMember.create!(member_hash(member)) + + ProjectMember.create(member_hash(member)) end def member_hash(member) member.except('id').merge(source_id: @project.id, importing: true) end - def default_project_member_hash - { user: @user, access_level: ProjectMember::MASTER, source_id: @project.id, importing: true } - end - def find_project_user_query(member) user_arel[:username].eq(member['user']['username']).or(user_arel[:email].eq(member['user']['email'])) end diff --git a/lib/gitlab/import_export/project_tree_restorer.rb b/lib/gitlab/import_export/project_tree_restorer.rb index e8f5fb88382..8d3a016ad71 100644 --- a/lib/gitlab/import_export/project_tree_restorer.rb +++ b/lib/gitlab/import_export/project_tree_restorer.rb @@ -80,12 +80,14 @@ module Gitlab end def create_relation(relation, relation_hash_list) - [relation_hash_list].flatten.map do |relation_hash| + relation_array = [relation_hash_list].flatten.map do |relation_hash| Gitlab::ImportExport::RelationFactory.create(relation_sym: relation.to_sym, relation_hash: relation_hash.merge('project_id' => project.id), members_mapper: members_mapper, user_admin: @user.is_admin?) end + + relation_hash_list.is_a?(Array) ? relation_array : relation_array.first end end end diff --git a/lib/gitlab/import_export/relation_factory.rb b/lib/gitlab/import_export/relation_factory.rb index 2794eafe4b6..cecb4747822 100644 --- a/lib/gitlab/import_export/relation_factory.rb +++ b/lib/gitlab/import_export/relation_factory.rb @@ -45,7 +45,7 @@ module Gitlab if user_admin relation_hash['author_id'] = members_map.map[old_author_id] else - relation_hash['author_id'] = members_map.default_project_member + relation_hash['author_id'] = members_map.default_user_id end author = relation_hash.delete('author') diff --git a/lib/gitlab/import_export/uploads_saver.rb b/lib/gitlab/import_export/uploads_saver.rb index 93bc626b363..7292e9d9712 100644 --- a/lib/gitlab/import_export/uploads_saver.rb +++ b/lib/gitlab/import_export/uploads_saver.rb @@ -2,10 +2,6 @@ module Gitlab module ImportExport class UploadsSaver - def self.save(*args) - new(*args).save - end - def initialize(project:, shared:) @project = project @shared = shared diff --git a/lib/gitlab/import_export/version_restorer.rb b/lib/gitlab/import_export/version_checker.rb similarity index 91% rename from lib/gitlab/import_export/version_restorer.rb rename to lib/gitlab/import_export/version_checker.rb index 892352f5adf..4f467760862 100644 --- a/lib/gitlab/import_export/version_restorer.rb +++ b/lib/gitlab/import_export/version_checker.rb @@ -1,16 +1,16 @@ module Gitlab module ImportExport - class VersionRestorer + class VersionChecker def self.restore(*args) - new(*args).restore + new(*args).check end def initialize(shared:) @shared = shared end - def restore + def check! version = File.open(version_file, &:readline) verify_version!(version) rescue => e