Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
0bc56568
Commit
0bc56568
authored
Oct 13, 2020
by
Vijay Hawoldar
Committed by
Peter Leitzen
Oct 13, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validate snippet repos when restoring
parent
30053faa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
5 deletions
+87
-5
lib/backup/repositories.rb
lib/backup/repositories.rb
+27
-3
spec/lib/backup/repositories_spec.rb
spec/lib/backup/repositories_spec.rb
+60
-2
No files found.
lib/backup/repositories.rb
View file @
0bc56568
...
...
@@ -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
...
...
spec/lib/backup/repositories_spec.rb
View file @
0bc56568
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment