Commit 9d306eb1 authored by James Lopez's avatar James Lopez

picking stuff from ui related to import

parent 548c91e3
...@@ -359,6 +359,16 @@ class Project < ActiveRecord::Base ...@@ -359,6 +359,16 @@ class Project < ActiveRecord::Base
def visible_to_user(user) def visible_to_user(user)
where(id: user.authorized_projects.select(:id).reorder(nil)) where(id: user.authorized_projects.select(:id).reorder(nil))
end 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 end
def team def team
......
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
...@@ -3,18 +3,19 @@ module Gitlab ...@@ -3,18 +3,19 @@ module Gitlab
class ImportService class ImportService
def self.execute(*args) def self.execute(*args)
new(args).execute new(*args).execute
end end
def initialize(options = {}) def initialize(archive_file:, owner:, namespace_id:, project_path:)
@archive_file = options[:archive_file] @archive_file = archive_file
@current_user = options[:owner] @current_user = owner
@namespace_path = Namespace.find(namespace_id).path
@project_path = project_path
end end
def execute def execute
Gitlab::ImportExport::Importer.import(archive_file: @archive_file, storage_path: storage_path) Gitlab::ImportExport::Importer.import(archive_file: @archive_file, storage_path: storage_path)
restore_project_tree project_tree.project if [restore_project_tree, restore_repo].all?
restore_repo(project_tree.project)
end end
private private
...@@ -24,15 +25,19 @@ module Gitlab ...@@ -24,15 +25,19 @@ module Gitlab
end end
def project_tree 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 end
def restore_repo(project) def restore_repo
Gitlab::ImportExport::RepoRestorer.new(path: storage_path, project: project).restore Gitlab::ImportExport::RepoRestorer.new(path: storage_path, project: project_tree.project).restore
end end
def storage_path 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 end
end end
......
...@@ -13,13 +13,14 @@ module Gitlab ...@@ -13,13 +13,14 @@ module Gitlab
end end
def import def import
FileUtils.mkdir_p(@storage_path)
decompress_archive decompress_archive
end end
private private
def decompress_archive def decompress_archive
untar_czf(archive: @archive_file, dir: @storage_path) untar_zxf(archive: @archive_file, dir: @storage_path)
end end
end end
end end
......
module Gitlab module Gitlab
module ImportExport module ImportExport
class ProjectTreeRestorer class ProjectTreeRestorer
attr_reader :project
def initialize(path:, user:) def initialize(path:, user:, project_path:)
@path = File.join(path, 'project.json') @path = File.join(path, 'project.json')
@user = user @user = user
@project_path = project_path
end end
def restore def restore
...@@ -15,6 +15,10 @@ module Gitlab ...@@ -15,6 +15,10 @@ module Gitlab
create_relations create_relations
end end
def project
@project ||= create_project
end
private private
def members_map def members_map
...@@ -40,14 +44,12 @@ module Gitlab ...@@ -40,14 +44,12 @@ module Gitlab
Gitlab::ImportExport::ImportExportReader.tree.reject { |model| model.is_a?(Hash) && model[:project_members] } Gitlab::ImportExport::ImportExportReader.tree.reject { |model| model.is_a?(Hash) && model[:project_members] }
end end
def project
@project ||= create_project
end
def create_project def create_project
project_params = @tree_hash.reject { |_key, value| value.is_a?(Array) } project_params = @tree_hash.reject { |_key, value| value.is_a?(Array) }
project = Gitlab::ImportExport::ProjectFactory.create( project = Gitlab::ImportExport::ProjectFactory.create(
project_params: project_params, user: @user) project_params: project_params, user: @user)
project.path = @project_path
project.name = @project_path
project.save project.save
project project
end end
...@@ -63,7 +65,6 @@ module Gitlab ...@@ -63,7 +65,6 @@ module Gitlab
end end
def process_sub_relation(relation_hash, relation_item, sub_relation) def process_sub_relation(relation_hash, relation_item, sub_relation)
sub_relation_object = nil
if relation_hash.is_a?(Array) if relation_hash.is_a?(Array)
sub_relation_object = create_relation(sub_relation, relation_hash) sub_relation_object = create_relation(sub_relation, relation_hash)
else else
......
...@@ -3,18 +3,19 @@ module Gitlab ...@@ -3,18 +3,19 @@ module Gitlab
class RepoRestorer class RepoRestorer
include Gitlab::ImportExport::CommandLineUtil include Gitlab::ImportExport::CommandLineUtil
def initialize(project: , path:, bundler_file: ) def initialize(project: , path: )
@project = project @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 end
def restore def restore
return false unless File.exists?(@path) 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(repos_path)
FileUtils.mkdir_p(path_to_repo) FileUtils.mkdir_p(path_to_repo)
untar_cf(archive: @path, dir: path_to_repo) untar_xf(archive: @path, dir: path_to_repo)
end end
private private
......
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