Commit 6ae43b2c authored by Kassio Borges's avatar Kassio Borges

Ensure clean repository folder before importing

To avoid `9:CreateRepositoryFromBundle: target directory is non-empty`
exceptions from Gitaly, when importing a Project, if the repository
folder already exists, ensure to Destroy existing repository before
importing the new one.
parent a3ea9afb
......@@ -14,10 +14,10 @@ module Gitlab
def restore
return true unless File.exist?(path_to_bundle)
ensure_repository_does_not_exist!
repository.create_from_bundle(path_to_bundle)
rescue => e
Repositories::DestroyService.new(repository).execute
shared.error(e)
false
end
......@@ -25,6 +25,16 @@ module Gitlab
private
attr_accessor :repository, :path_to_bundle, :shared
def ensure_repository_does_not_exist!
if repository.exists?
shared.logger.info(
message: %Q{Deleting existing "#{repository.path}" to re-import it.}
)
Repositories::DestroyService.new(repository).execute
end
end
end
end
end
......@@ -36,21 +36,20 @@ RSpec.describe Gitlab::ImportExport::RepoRestorer do
expect(subject.restore).to be_truthy
end
context 'when the repository creation fails' do
before do
allow_next_instance_of(Repositories::DestroyService) do |instance|
context 'when the repository already exists' do
it 'deletes the existing repository before importing' do
allow(project.repository).to receive(:exists?).and_return(true)
allow(project.repository).to receive(:path).and_return('repository_path')
expect_next_instance_of(Repositories::DestroyService) do |instance|
expect(instance).to receive(:execute).and_call_original
end
end
it 'logs the error' do
allow(project.repository)
.to receive(:create_from_bundle)
.and_raise('9:CreateRepositoryFromBundle: target directory is non-empty')
expect(shared).to receive(:error).and_call_original
expect(shared.logger).to receive(:info).with(
message: 'Deleting existing "repository_path" to re-import it.'
)
expect(subject.restore).to be_falsey
expect(subject.restore).to be_truthy
end
end
end
......
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