Commit 0bc56568 authored by Vijay Hawoldar's avatar Vijay Hawoldar Committed by Peter Leitzen

Validate snippet repos when restoring

parent 30053faa
......@@ -46,9 +46,11 @@ module Backup
restore_repository(project, Gitlab::GlRepository::DESIGN)
end
Snippet.find_each(batch_size: 1000) do |snippet|
restore_repository(snippet, Gitlab::GlRepository::SNIPPET)
end
invalid_ids = Snippet.find_each(batch_size: 1000)
.map { |snippet| restore_snippet_repository(snippet) }
.compact
cleanup_snippets_without_repositories(invalid_ids)
restore_object_pools
end
......@@ -192,6 +194,28 @@ module Backup
end
end
def restore_snippet_repository(snippet)
restore_repository(snippet, Gitlab::GlRepository::SNIPPET)
response = Snippets::RepositoryValidationService.new(nil, snippet).execute
if response.error?
snippet.repository.remove
progress.puts("Snippet #{snippet.full_path} can't be restored: #{response.message}")
snippet.id
else
nil
end
end
# Snippets without a repository should be removed because they failed to import
# due to having invalid repositories
def cleanup_snippets_without_repositories(ids)
Snippet.id_in(ids).delete_all
end
class BackupRestore
attr_accessor :progress, :repository, :backup_repos_path
......
......@@ -162,15 +162,17 @@ RSpec.describe Backup::Repositories do
let_it_be(:personal_snippet) { create(:personal_snippet, author: project.owner) }
let_it_be(:project_snippet) { create(:project_snippet, project: project, author: project.owner) }
it 'restores repositories from bundles', :aggregate_failures do
next_path_to_bundle = [
let(:next_path_to_bundle) do
[
Rails.root.join('spec/fixtures/lib/backup/project_repo.bundle'),
Rails.root.join('spec/fixtures/lib/backup/wiki_repo.bundle'),
Rails.root.join('spec/fixtures/lib/backup/design_repo.bundle'),
Rails.root.join('spec/fixtures/lib/backup/personal_snippet_repo.bundle'),
Rails.root.join('spec/fixtures/lib/backup/project_snippet_repo.bundle')
].to_enum
end
it 'restores repositories from bundles', :aggregate_failures do
allow_next_instance_of(described_class::BackupRestore) do |backup_restore|
allow(backup_restore).to receive(:path_to_bundle).and_return(next_path_to_bundle.next)
end
......@@ -231,6 +233,9 @@ RSpec.describe Backup::Repositories do
end
it 'cleans existing repositories' do
success_response = ServiceResponse.success(message: "Valid Snippet Repo")
allow(Snippets::RepositoryValidationService).to receive_message_chain(:new, :execute).and_return(success_response)
expect_next_instance_of(DesignManagement::Repository) do |repository|
expect(repository).to receive(:remove)
end
......@@ -246,5 +251,58 @@ RSpec.describe Backup::Repositories do
subject.restore
end
context 'restoring snippets' do
before do
create(:snippet_repository, snippet: personal_snippet)
create(:snippet_repository, snippet: project_snippet)
allow_next_instance_of(described_class::BackupRestore) do |backup_restore|
allow(backup_restore).to receive(:path_to_bundle).and_return(next_path_to_bundle.next)
end
end
context 'when the repository is valid' do
it 'restores the snippet repositories' do
subject.restore
expect(personal_snippet.snippet_repository.persisted?).to be true
expect(personal_snippet.repository).to exist
expect(project_snippet.snippet_repository.persisted?).to be true
expect(project_snippet.repository).to exist
end
end
context 'when repository is invalid' do
before do
error_response = ServiceResponse.error(message: "Repository has more than one branch")
allow(Snippets::RepositoryValidationService).to receive_message_chain(:new, :execute).and_return(error_response)
end
it 'shows the appropriate error' do
subject.restore
expect(progress).to have_received(:puts).with("Snippet #{personal_snippet.full_path} can't be restored: Repository has more than one branch")
expect(progress).to have_received(:puts).with("Snippet #{project_snippet.full_path} can't be restored: Repository has more than one branch")
end
it 'removes the snippets from the DB' do
expect { subject.restore }.to change(PersonalSnippet, :count).by(-1)
.and change(ProjectSnippet, :count).by(-1)
.and change(SnippetRepository, :count).by(-2)
end
it 'removes the repository from disk' do
gitlab_shell = Gitlab::Shell.new
shard_name = personal_snippet.repository.shard
path = personal_snippet.disk_path + '.git'
subject.restore
expect(gitlab_shell.repository_exists?(shard_name, path)).to eq false
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