summaryrefslogtreecommitdiff
path: root/lib/gitlab/import_export
diff options
context:
space:
mode:
authorJames Lopez <james@jameslopez.es>2016-06-14 12:47:07 +0200
committerJames Lopez <james@jameslopez.es>2016-06-14 12:47:07 +0200
commit3f7ed550110daaec8a76af7146b701dfc0210e60 (patch)
tree25fa93f0c306f8ea2ab9b28f26dfb11bb7551184 /lib/gitlab/import_export
parent1ea44ee75077d67f3f24a288cc44d7c275ba8581 (diff)
downloadgitlab-ce-3f7ed550110daaec8a76af7146b701dfc0210e60.tar.gz
lots of refactoring to reuse import service
Diffstat (limited to 'lib/gitlab/import_export')
-rw-r--r--lib/gitlab/import_export/file_importer.rb30
-rw-r--r--lib/gitlab/import_export/import_export.yml2
-rw-r--r--lib/gitlab/import_export/import_service.rb81
-rw-r--r--lib/gitlab/import_export/importer.rb78
-rw-r--r--lib/gitlab/import_export/project_factory.rb41
-rw-r--r--lib/gitlab/import_export/project_tree_restorer.rb17
6 files changed, 100 insertions, 149 deletions
diff --git a/lib/gitlab/import_export/file_importer.rb b/lib/gitlab/import_export/file_importer.rb
new file mode 100644
index 00000000000..0e70d9282d5
--- /dev/null
+++ b/lib/gitlab/import_export/file_importer.rb
@@ -0,0 +1,30 @@
+module Gitlab
+ module ImportExport
+ class FileImporter
+ include Gitlab::ImportExport::CommandLineUtil
+
+ def self.import(*args)
+ new(*args).import
+ end
+
+ def initialize(archive_file:, shared:)
+ @archive_file = archive_file
+ @shared = shared
+ end
+
+ def import
+ FileUtils.mkdir_p(@shared.export_path)
+ decompress_archive
+ rescue => e
+ @shared.error(e)
+ false
+ end
+
+ private
+
+ def decompress_archive
+ untar_zxf(archive: @archive_file, dir: @shared.export_path)
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/import_export/import_export.yml b/lib/gitlab/import_export/import_export.yml
index 3796fc8cd02..164ab6238c4 100644
--- a/lib/gitlab/import_export/import_export.yml
+++ b/lib/gitlab/import_export/import_export.yml
@@ -30,8 +30,6 @@ project_tree:
# Only include the following attributes for the models specified.
included_attributes:
project:
- - :name
- - :path
- :description
- :issues_enabled
- :merge_requests_enabled
diff --git a/lib/gitlab/import_export/import_service.rb b/lib/gitlab/import_export/import_service.rb
deleted file mode 100644
index 95d4fb17ead..00000000000
--- a/lib/gitlab/import_export/import_service.rb
+++ /dev/null
@@ -1,81 +0,0 @@
-module Gitlab
- module ImportExport
- class ImportService
-
- def self.execute(*args)
- new(*args).execute
- end
-
- def initialize(archive_file:, owner:, namespace_id:, project_path:)
- @archive_file = archive_file
- @current_user = owner
- @namespace = Namespace.find(namespace_id)
- @shared = Gitlab::ImportExport::Shared.new(relative_path: path_with_namespace(project_path), project_path: project_path)
- end
-
- def execute
- Gitlab::ImportExport::Importer.import(archive_file: @archive_file,
- shared: @shared)
- 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
- nil
- end
- end
-
- private
-
- def check_version!
- Gitlab::ImportExport::VersionChecker.check!(shared: @shared)
- end
-
- def project_tree
- @project_tree ||= Gitlab::ImportExport::ProjectTreeRestorer.new(user: @current_user,
- shared: @shared,
- namespace_id: @namespace.id)
- end
-
- def repo_restorer
- Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: repo_path,
- shared: @shared,
- project: project_tree.project)
- end
-
- def wiki_restorer
- Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: wiki_repo_path,
- shared: @shared,
- project: ProjectWiki.new(project_tree.project),
- wiki: true)
- end
-
- def uploads_restorer
- Gitlab::ImportExport::UploadsRestorer.new(project: project_tree.project, shared: @shared)
- end
-
- def path_with_namespace(project_path)
- File.join(@namespace.path, project_path)
- end
-
- def repo_path
- File.join(@shared.export_path, 'project.bundle')
- end
-
- def wiki_repo_path
- File.join(@shared.export_path, 'project.wiki.bundle')
- end
-
- def attributes_for_todo
- { user_id: @current_user.id,
- project_id: project_tree.project.id,
- target_type: 'Project',
- target: project_tree.project,
- action: Todo::IMPORTED,
- author_id: @current_user.id,
- state: :pending,
- target_id: project_tree.project.id
- }
- end
- end
- end
-end
diff --git a/lib/gitlab/import_export/importer.rb b/lib/gitlab/import_export/importer.rb
index 8020aab3da9..d096e17bdf0 100644
--- a/lib/gitlab/import_export/importer.rb
+++ b/lib/gitlab/import_export/importer.rb
@@ -1,29 +1,79 @@
module Gitlab
module ImportExport
class Importer
- include Gitlab::ImportExport::CommandLineUtil
- def self.import(*args)
- new(*args).import
+ def self.execute(*args)
+ new(*args).execute
end
- def initialize(archive_file:, shared:)
- @archive_file = archive_file
- @shared = shared
+ def initialize(project)
+ @archive_file = project.import_source
+ @current_user = project.creator
+ @shared = Gitlab::ImportExport::Shared.new(relative_path: path_with_namespace(@project.path))
end
- def import
- FileUtils.mkdir_p(@shared.export_path)
- decompress_archive
- rescue => e
- @shared.error(e)
- false
+ def execute
+ Gitlab::ImportExport::FileImporter.import(archive_file: @archive_file,
+ shared: @shared)
+ 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
+ nil
+ end
end
private
- def decompress_archive
- untar_zxf(archive: @archive_file, dir: @shared.export_path)
+ def check_version!
+ Gitlab::ImportExport::VersionChecker.check!(shared: @shared)
+ end
+
+ def project_tree
+ @project_tree ||= Gitlab::ImportExport::ProjectTreeRestorer.new(user: @current_user,
+ shared: @shared,
+ project: @project)
+ end
+
+ def repo_restorer
+ Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: repo_path,
+ shared: @shared,
+ project: project_tree.project)
+ end
+
+ def wiki_restorer
+ Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: wiki_repo_path,
+ shared: @shared,
+ project: ProjectWiki.new(project_tree.project),
+ wiki: true)
+ end
+
+ def uploads_restorer
+ Gitlab::ImportExport::UploadsRestorer.new(project: project_tree.project, shared: @shared)
+ end
+
+ def path_with_namespace(project_path)
+ File.join(@namespace.path, project_path)
+ end
+
+ def repo_path
+ File.join(@shared.export_path, 'project.bundle')
+ end
+
+ def wiki_repo_path
+ File.join(@shared.export_path, 'project.wiki.bundle')
+ end
+
+ def attributes_for_todo
+ { user_id: @current_user.id,
+ project_id: project_tree.project.id,
+ target_type: 'Project',
+ target: project_tree.project,
+ action: Todo::IMPORTED,
+ author_id: @current_user.id,
+ state: :pending,
+ target_id: project_tree.project.id
+ }
end
end
end
diff --git a/lib/gitlab/import_export/project_factory.rb b/lib/gitlab/import_export/project_factory.rb
deleted file mode 100644
index 6cd4736649b..00000000000
--- a/lib/gitlab/import_export/project_factory.rb
+++ /dev/null
@@ -1,41 +0,0 @@
-module Gitlab
- module ImportExport
- module ProjectFactory
- extend self
-
- def create(project_params:, user:, namespace_id:)
- project = Project.new(project_params.except('id'))
- project.creator = user
- check_namespace(namespace_id, project, user)
- end
-
- def check_namespace(namespace_id, project, user)
- if namespace_id
- # Find matching namespace and check if it allowed
- # for current user if namespace_id passed.
- if allowed_namespace?(user, namespace_id)
- project.namespace_id = namespace_id
- else
- project.namespace_id = nil
- deny_namespace(project)
- end
- else
- # Set current user namespace if namespace_id is nil
- project.namespace_id = user.namespace_id
- end
- project
- end
-
- private
-
- def allowed_namespace?(user, namespace_id)
- namespace = Namespace.find_by(id: namespace_id)
- user.can?(:create_projects, namespace)
- end
-
- def deny_namespace(project)
- project.errors.add(:namespace, "is not valid")
- end
- end
- end
-end
diff --git a/lib/gitlab/import_export/project_tree_restorer.rb b/lib/gitlab/import_export/project_tree_restorer.rb
index c7c1c376ab7..290b38927ae 100644
--- a/lib/gitlab/import_export/project_tree_restorer.rb
+++ b/lib/gitlab/import_export/project_tree_restorer.rb
@@ -2,12 +2,11 @@ module Gitlab
module ImportExport
class ProjectTreeRestorer
- def initialize(user:, shared:, namespace_id:)
+ def initialize(user:, shared:, project:)
@path = File.join(shared.export_path, 'project.json')
@user = user
- @project_path = shared.opts[:project_path]
- @namespace_id = namespace_id
@shared = shared
+ @project = project
end
def restore
@@ -21,7 +20,7 @@ module Gitlab
end
def project
- @project ||= create_project
+ @restored_project ||= restore_project
end
private
@@ -57,14 +56,10 @@ module Gitlab
end
end
- def create_project
+ def restore_project
project_params = @tree_hash.reject { |_key, value| value.is_a?(Array) }
- project = Gitlab::ImportExport::ProjectFactory.create(
- project_params: project_params, user: @user, namespace_id: @namespace_id)
- project.path = @project_path
- project.name = @project_path
- project.save!
- project
+ @project.update(project_params)
+ @project
end
# Given a relation hash containing one or more models and its relationships,