Commit f13fce3f authored by Mark Chao's avatar Mark Chao

Merge branch 'kassio/avoid-import-error-if-repository-already-exists' into 'master'

Ensure repository folder doesn't exist when importing a Project Export

See merge request gitlab-org/gitlab!44207
parents 7e2ff88b 6ae43b2c
...@@ -14,10 +14,10 @@ module Gitlab ...@@ -14,10 +14,10 @@ module Gitlab
def restore def restore
return true unless File.exist?(path_to_bundle) return true unless File.exist?(path_to_bundle)
ensure_repository_does_not_exist!
repository.create_from_bundle(path_to_bundle) repository.create_from_bundle(path_to_bundle)
rescue => e rescue => e
Repositories::DestroyService.new(repository).execute
shared.error(e) shared.error(e)
false false
end end
...@@ -25,6 +25,16 @@ module Gitlab ...@@ -25,6 +25,16 @@ module Gitlab
private private
attr_accessor :repository, :path_to_bundle, :shared 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 end
end end
...@@ -36,21 +36,20 @@ RSpec.describe Gitlab::ImportExport::RepoRestorer do ...@@ -36,21 +36,20 @@ RSpec.describe Gitlab::ImportExport::RepoRestorer do
expect(subject.restore).to be_truthy expect(subject.restore).to be_truthy
end end
context 'when the repository creation fails' do context 'when the repository already exists' do
before do it 'deletes the existing repository before importing' do
allow_next_instance_of(Repositories::DestroyService) do |instance| 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 expect(instance).to receive(:execute).and_call_original
end 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 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