picking stuff from ui related to import
This commit is contained in:
parent
548c91e3c5
commit
9d306eb132
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue