Commit 102074c8 authored by James Lopez's avatar James Lopez

more and more refactoring

parent 7c8359b7
......@@ -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)
......
......@@ -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
ensure_default_member!
end
def map
@map ||= generate_map
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
def default_user_id
@user.id
end
def map
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)
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
......
......@@ -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
......
......@@ -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')
......
......@@ -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
......
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
......
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