2020-03-06 18:08:08 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class SnippetRepoRestorer < RepoRestorer
|
2020-07-01 21:08:51 +00:00
|
|
|
attr_reader :snippet, :user
|
2020-03-06 18:08:08 +00:00
|
|
|
|
2020-06-04 18:08:32 +00:00
|
|
|
SnippetRepositoryError = Class.new(StandardError)
|
|
|
|
|
2020-03-06 18:08:08 +00:00
|
|
|
def initialize(snippet:, user:, shared:, path_to_bundle:)
|
|
|
|
@snippet = snippet
|
|
|
|
@user = user
|
|
|
|
@repository = snippet.repository
|
|
|
|
@path_to_bundle = path_to_bundle.to_s
|
|
|
|
@shared = shared
|
|
|
|
end
|
|
|
|
|
|
|
|
def restore
|
|
|
|
if File.exist?(path_to_bundle)
|
|
|
|
create_repository_from_bundle
|
|
|
|
else
|
|
|
|
create_repository_from_db
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue => e
|
|
|
|
shared.error(e)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_repository_from_bundle
|
|
|
|
repository.create_from_bundle(path_to_bundle)
|
2020-03-25 12:08:19 +00:00
|
|
|
snippet.track_snippet_repository(repository.storage)
|
2020-07-01 21:08:51 +00:00
|
|
|
|
|
|
|
response = Snippets::RepositoryValidationService.new(user, snippet).execute
|
|
|
|
|
|
|
|
if response.error?
|
|
|
|
repository.remove
|
|
|
|
snippet.snippet_repository.delete
|
|
|
|
snippet.repository.expire_exists_cache
|
|
|
|
|
|
|
|
raise SnippetRepositoryError, _("Invalid repository bundle for snippet with id %{snippet_id}") % { snippet_id: snippet.id }
|
2020-07-02 09:09:00 +00:00
|
|
|
else
|
|
|
|
Snippets::UpdateStatisticsService.new(snippet).execute
|
2020-07-01 21:08:51 +00:00
|
|
|
end
|
2020-03-06 18:08:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_repository_from_db
|
2020-06-02 06:08:01 +00:00
|
|
|
Gitlab::BackgroundMigration::BackfillSnippetRepositories.new.perform_by_ids([snippet.id])
|
2020-06-04 18:08:32 +00:00
|
|
|
|
|
|
|
unless snippet.reset.snippet_repository
|
|
|
|
raise SnippetRepositoryError, _("Error creating repository for snippet with id %{snippet_id}") % { snippet_id: snippet.id }
|
|
|
|
end
|
2020-03-06 18:08:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|