summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/project.rb10
-rw-r--r--app/workers/project_impor_worker.rb25
-rw-r--r--lib/gitlab/import_export/import_service.rb25
-rw-r--r--lib/gitlab/import_export/importer.rb3
-rw-r--r--lib/gitlab/import_export/project_tree_restorer.rb15
-rw-r--r--lib/gitlab/import_export/repo_restorer.rb9
6 files changed, 65 insertions, 22 deletions
diff --git a/app/models/project.rb b/app/models/project.rb
index af62e8ecd90..3e782be637e 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -359,6 +359,16 @@ class Project < ActiveRecord::Base
def visible_to_user(user)
where(id: user.authorized_projects.select(:id).reorder(nil))
end
+
+ def create_from_import_job(current_user_id:, tmp_file:, namespace_id:, project_path:)
+ job_id = ProjectImportWorker.perform_async(current_user_id, tmp_file, namespace_id, project_path)
+
+ if job_id
+ Rails.logger.info "Import job started for export #{tmp_file} with job ID #{job_id}"
+ else
+ Rails.logger.error "Import job failed to start for #{tmp_file}"
+ end
+ end
end
def team
diff --git a/app/workers/project_impor_worker.rb b/app/workers/project_impor_worker.rb
new file mode 100644
index 00000000000..0ef353fa441
--- /dev/null
+++ b/app/workers/project_impor_worker.rb
@@ -0,0 +1,25 @@
+class ProjectImportWorker
+ include Sidekiq::Worker
+ include Gitlab::ShellAdapter
+
+ sidekiq_options queue: :gitlab_shell, retry: false
+
+ def perform(current_user_id, tmp_file, namespace_id, path)
+ current_user = User.find(current_user_id)
+
+ project = Gitlab::ImportExport::ImportService.execute(archive_file: tmp_file,
+ owner: current_user,
+ namespace_id: namespace_id,
+ project_path: path)
+
+ # TODO: Move this to import service
+ # if result[:status] == :error
+ # project.update(import_error: result[:message])
+ # project.import_fail
+ # return
+ # end
+
+ project.repository.after_import
+ project.import_finish
+ end
+end \ No newline at end of file
diff --git a/lib/gitlab/import_export/import_service.rb b/lib/gitlab/import_export/import_service.rb
index 978a581f57a..227053481cd 100644
--- a/lib/gitlab/import_export/import_service.rb
+++ b/lib/gitlab/import_export/import_service.rb
@@ -3,18 +3,19 @@ module Gitlab
class ImportService
def self.execute(*args)
- new(args).execute
+ new(*args).execute
end
- def initialize(options = {})
- @archive_file = options[:archive_file]
- @current_user = options[:owner]
+ def initialize(archive_file:, owner:, namespace_id:, project_path:)
+ @archive_file = archive_file
+ @current_user = owner
+ @namespace_path = Namespace.find(namespace_id).path
+ @project_path = project_path
end
def execute
Gitlab::ImportExport::Importer.import(archive_file: @archive_file, storage_path: storage_path)
- restore_project_tree
- restore_repo(project_tree.project)
+ project_tree.project if [restore_project_tree, restore_repo].all?
end
private
@@ -24,15 +25,19 @@ module Gitlab
end
def project_tree
- @project_tree ||= Gitlab::ImportExport::ProjectTreeRestorer.new(path: storage_path, user: @current_user)
+ @project_tree ||= Gitlab::ImportExport::ProjectTreeRestorer.new(path: storage_path, user: @current_user, project_path: @project_path)
end
- def restore_repo(project)
- Gitlab::ImportExport::RepoRestorer.new(path: storage_path, project: project).restore
+ def restore_repo
+ Gitlab::ImportExport::RepoRestorer.new(path: storage_path, project: project_tree.project).restore
end
def storage_path
- @storage_path ||= Gitlab::ImportExport.export_path(relative_path: project.path_with_namespace)
+ @storage_path ||= Gitlab::ImportExport.export_path(relative_path: path_with_namespace)
+ end
+
+ def path_with_namespace
+ File.join(@namespace_path, @project_path)
end
end
end
diff --git a/lib/gitlab/import_export/importer.rb b/lib/gitlab/import_export/importer.rb
index 9f399845437..8f838287f97 100644
--- a/lib/gitlab/import_export/importer.rb
+++ b/lib/gitlab/import_export/importer.rb
@@ -13,13 +13,14 @@ module Gitlab
end
def import
+ FileUtils.mkdir_p(@storage_path)
decompress_archive
end
private
def decompress_archive
- untar_czf(archive: @archive_file, dir: @storage_path)
+ untar_zxf(archive: @archive_file, dir: @storage_path)
end
end
end
diff --git a/lib/gitlab/import_export/project_tree_restorer.rb b/lib/gitlab/import_export/project_tree_restorer.rb
index 4e0f555afe9..0f2e3716779 100644
--- a/lib/gitlab/import_export/project_tree_restorer.rb
+++ b/lib/gitlab/import_export/project_tree_restorer.rb
@@ -1,11 +1,11 @@
module Gitlab
module ImportExport
class ProjectTreeRestorer
- attr_reader :project
- def initialize(path:, user:)
+ def initialize(path:, user:, project_path:)
@path = File.join(path, 'project.json')
@user = user
+ @project_path = project_path
end
def restore
@@ -15,6 +15,10 @@ module Gitlab
create_relations
end
+ def project
+ @project ||= create_project
+ end
+
private
def members_map
@@ -40,14 +44,12 @@ module Gitlab
Gitlab::ImportExport::ImportExportReader.tree.reject { |model| model.is_a?(Hash) && model[:project_members] }
end
- def project
- @project ||= create_project
- end
-
def create_project
project_params = @tree_hash.reject { |_key, value| value.is_a?(Array) }
project = Gitlab::ImportExport::ProjectFactory.create(
project_params: project_params, user: @user)
+ project.path = @project_path
+ project.name = @project_path
project.save
project
end
@@ -63,7 +65,6 @@ module Gitlab
end
def process_sub_relation(relation_hash, relation_item, sub_relation)
- sub_relation_object = nil
if relation_hash.is_a?(Array)
sub_relation_object = create_relation(sub_relation, relation_hash)
else
diff --git a/lib/gitlab/import_export/repo_restorer.rb b/lib/gitlab/import_export/repo_restorer.rb
index 47be303e22a..315ad88ee0c 100644
--- a/lib/gitlab/import_export/repo_restorer.rb
+++ b/lib/gitlab/import_export/repo_restorer.rb
@@ -3,18 +3,19 @@ module Gitlab
class RepoRestorer
include Gitlab::ImportExport::CommandLineUtil
- def initialize(project: , path:, bundler_file: )
+ def initialize(project: , path: )
@project = project
- @path = File.join(path, bundler_file)
+ # TODO remove magic keyword and move it to a shared config
+ @path = File.join(path, 'project.bundle')
end
def restore
return false unless File.exists?(@path)
- # Move repos dir to 'repositories.old' dir
+ # Move repos dir to 'repositories.old' dir
FileUtils.mkdir_p(repos_path)
FileUtils.mkdir_p(path_to_repo)
- untar_cf(archive: @path, dir: path_to_repo)
+ untar_xf(archive: @path, dir: path_to_repo)
end
private