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
2c489f82
Commit
2c489f82
authored
Sep 12, 2017
by
Gabriel Mazetto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor on namespace and repository checks and added specs
parent
cc28abea
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
196 additions
and
33 deletions
+196
-33
lib/system_check/orphans/namespace_check.rb
lib/system_check/orphans/namespace_check.rb
+29
-12
lib/system_check/orphans/repository_check.rb
lib/system_check/orphans/repository_check.rb
+38
-20
lib/tasks/gitlab/check.rake
lib/tasks/gitlab/check.rake
+0
-1
spec/lib/system_check/orphans/namespace_check_spec.rb
spec/lib/system_check/orphans/namespace_check_spec.rb
+61
-0
spec/lib/system_check/orphans/repository_check_spec.rb
spec/lib/system_check/orphans/repository_check_spec.rb
+68
-0
No files found.
lib/system_check/orphans/namespace_check.rb
View file @
2c489f82
...
...
@@ -4,15 +4,24 @@ module SystemCheck
set_name
'Orphaned namespaces:'
def
multi_check
Gitlab
.
config
.
repositories
.
storages
.
each
do
|
name
,
repository_storage
|
Gitlab
.
config
.
repositories
.
storages
.
each
do
|
storage_
name
,
repository_storage
|
$stdout
.
puts
$stdout
.
puts
"* Storage:
#{
name
}
(
#{
repository_storage
[
'path'
]
}
)"
.
color
(
:yellow
)
toplevel_namespace_dirs
=
Dir
.
glob
(
File
.
join
(
repository_storage
[
'path'
],
'*'
)).
map
{
|
p
|
File
.
basename
(
p
)}
$stdout
.
puts
"* Storage:
#{
storage_
name
}
(
#{
repository_storage
[
'path'
]
}
)"
.
color
(
:yellow
)
toplevel_namespace_dirs
=
disk_namespaces
(
repository_storage
[
'path'
])
orphans
=
(
toplevel_namespace_dirs
-
existing_namespaces
)
print_orphans
(
orphans
,
storage_name
)
end
clear_namespaces!
# releases memory when check finishes
end
private
def
print_orphans
(
orphans
,
storage_name
)
if
orphans
.
empty?
$stdout
.
puts
"* No orphaned namespaces for
#{
name
}
storage"
.
color
(
:green
)
next
$stdout
.
puts
"* No orphaned namespaces for
#{
storage_
name
}
storage"
.
color
(
:green
)
return
end
orphans
.
each
do
|
orphan
|
...
...
@@ -20,13 +29,21 @@ module SystemCheck
end
end
clear_namespaces!
# releases memory when check finishes
def
disk_namespaces
(
storage_path
)
fetch_disk_namespaces
(
storage_path
).
each_with_object
([])
do
|
namespace_path
,
result
|
namespace
=
File
.
basename
(
namespace_path
)
next
if
namespace
.
eql?
(
'@hashed'
)
result
<<
namespace
end
end
private
def
fetch_disk_namespaces
(
storage_path
)
Dir
.
glob
(
File
.
join
(
storage_path
,
'*'
))
end
def
existing_namespaces
@namespaces
||=
Namespace
.
all
.
pluck
(
:path
)
@namespaces
||=
Namespace
.
where
(
parent:
nil
).
all
.
pluck
(
:path
)
end
def
clear_namespaces!
...
...
lib/system_check/orphans/repository_check.rb
View file @
2c489f82
...
...
@@ -2,49 +2,67 @@ module SystemCheck
module
Orphans
class
RepositoryCheck
<
SystemCheck
::
BaseCheck
set_name
'Orphaned repositories:'
attr_accessor
:orphans
def
multi_check
Gitlab
.
config
.
repositories
.
storages
.
each
do
|
name
,
repository_storage
|
Gitlab
.
config
.
repositories
.
storages
.
each
do
|
storage_
name
,
repository_storage
|
$stdout
.
puts
$stdout
.
puts
"* Storage:
#{
name
}
(
#{
repository_storage
[
'path'
]
}
)"
.
color
(
:yellow
)
$stdout
.
puts
"* Storage:
#{
storage_
name
}
(
#{
repository_storage
[
'path'
]
}
)"
.
color
(
:yellow
)
repositories
=
toplevel_namespace_dirs
(
repository_storage
[
'path'
]).
map
do
|
path
|
namespace
=
File
.
basename
(
path
)
Dir
.
glob
(
File
.
join
(
path
,
'*'
)).
map
{
|
repo
|
"
#{
namespace
}
/
#{
File
.
basename
(
repo
)
}
"
}
end
.
try
(
:flatten!
)
repositories
=
disk_repositories
(
repository_storage
[
'path'
])
orphans
=
(
repositories
-
fetch_repositories
(
storage_name
))
orphans
=
(
repositories
-
list_repositories
(
name
))
print_orphans
(
orphans
,
storage_name
)
end
end
private
def
print_orphans
(
orphans
,
storage_name
)
if
orphans
.
empty?
$stdout
.
puts
"* No orphaned repositories for
#{
name
}
storage"
.
color
(
:green
)
next
$stdout
.
puts
"* No orphaned repositories for
#{
storage_
name
}
storage"
.
color
(
:green
)
return
end
orphans
.
each
do
|
orphan
|
$stdout
.
puts
" -
#{
orphan
}
"
.
color
(
:red
)
end
end
end
private
def
disk_repositories
(
storage_path
)
fetch_disk_namespaces
(
storage_path
).
each_with_object
([])
do
|
namespace_path
,
result
|
namespace
=
File
.
basename
(
namespace_path
)
next
if
namespace
.
eql?
(
'@hashed'
)
def
list_repositories
(
storage_name
)
fetch_disk_repositories
(
namespace_path
).
each
do
|
repo
|
result
<<
"
#{
namespace
}
/
#{
File
.
basename
(
repo
)
}
"
end
end
end
def
fetch_repositories
(
storage_name
)
sql
=
"
SELECT
CONCAT(n.path, '/', p.path, '.git') repo,
CONCAT(n.path, '/', p.path, '.wiki.git') wiki
FROM projects p
JOIN namespaces n
ON (p.namespace_id = n.id)
ON (p.namespace_id = n.id AND
n.parent_id IS NULL)
WHERE (p.repository_storage LIKE ?)
"
query
=
ActiveRecord
::
Base
.
send
(
:sanitize_sql_array
,
[
sql
,
storage_name
])
ActiveRecord
::
Base
.
connection
.
select_all
(
query
).
rows
.
try
(
:flatten!
)
query
=
ActiveRecord
::
Base
.
send
(
:sanitize_sql_array
,
[
sql
,
storage_name
])
# rubocop:disable GitlabSecurity/PublicSend
ActiveRecord
::
Base
.
connection
.
select_all
(
query
).
rows
.
try
(
:flatten!
)
||
[]
end
def
toplevel_namespace_dir
s
(
storage_path
)
def
fetch_disk_namespace
s
(
storage_path
)
Dir
.
glob
(
File
.
join
(
storage_path
,
'*'
))
end
def
fetch_disk_repositories
(
namespace_path
)
Dir
.
glob
(
File
.
join
(
namespace_path
,
'*'
))
end
end
end
end
lib/tasks/gitlab/check.rake
View file @
2c489f82
...
...
@@ -427,7 +427,6 @@ namespace :gitlab do
end
end
namespace
:user
do
desc
"GitLab | Check the integrity of a specific user's repositories"
task
:check_repos
,
[
:username
]
=>
:environment
do
|
t
,
args
|
...
...
spec/lib/system_check/orphans/namespace_check_spec.rb
0 → 100644
View file @
2c489f82
require
'spec_helper'
require
'rake_helper'
describe
SystemCheck
::
Orphans
::
NamespaceCheck
do
let
(
:storages
)
{
Gitlab
.
config
.
repositories
.
storages
.
reject
{
|
key
,
_
|
key
.
eql?
'broken'
}
}
before
do
allow
(
Gitlab
.
config
.
repositories
).
to
receive
(
:storages
).
and_return
(
storages
)
allow
(
subject
).
to
receive
(
:fetch_disk_namespaces
).
and_return
(
disk_namespaces
)
silence_output
end
describe
'#multi_check'
do
context
'all orphans'
do
let
(
:disk_namespaces
)
{
%w(/repos/orphan1 /repos/orphan2 repos/@hashed)
}
it
'prints list of all orphaned namespaces except @hashed'
do
expect_list_of_orphans
(
%w(orphan1 orphan2)
)
subject
.
multi_check
end
end
context
'few orphans with existing namespace'
do
let!
(
:first_level
)
{
create
(
:group
,
path:
'my-namespace'
)
}
let
(
:disk_namespaces
)
{
%w(/repos/orphan1 /repos/orphan2 /repos/my-namespace /repos/@hashed)
}
it
'prints list of orphaned namespaces'
do
expect_list_of_orphans
(
%w(orphan1 orphan2)
)
subject
.
multi_check
end
end
context
'few orphans with existing namespace and parents with same name as orphans'
do
let!
(
:first_level
)
{
create
(
:group
,
path:
'my-namespace'
)
}
let!
(
:second_level
)
{
create
(
:group
,
path:
'second-level'
,
parent:
first_level
)
}
let
(
:disk_namespaces
)
{
%w(/repos/orphan1 /repos/orphan2 /repos/my-namespace /repos/second-level /repos/@hashed)
}
it
'prints list of orphaned namespaces ignoring parents with same namespace as orphans'
do
expect_list_of_orphans
(
%w(orphan1 orphan2 second-level)
)
subject
.
multi_check
end
end
context
'no orphans'
do
let
(
:disk_namespaces
)
{
%w(@hashed)
}
it
'prints an empty list ignoring @hashed'
do
expect_list_of_orphans
([])
subject
.
multi_check
end
end
end
def
expect_list_of_orphans
(
orphans
)
expect
(
subject
).
to
receive
(
:print_orphans
).
with
(
orphans
,
'default'
)
end
end
spec/lib/system_check/orphans/repository_check_spec.rb
0 → 100644
View file @
2c489f82
require
'spec_helper'
require
'rake_helper'
describe
SystemCheck
::
Orphans
::
RepositoryCheck
do
let
(
:storages
)
{
Gitlab
.
config
.
repositories
.
storages
.
reject
{
|
key
,
_
|
key
.
eql?
'broken'
}
}
before
do
allow
(
Gitlab
.
config
.
repositories
).
to
receive
(
:storages
).
and_return
(
storages
)
allow
(
subject
).
to
receive
(
:fetch_disk_namespaces
).
and_return
(
disk_namespaces
)
allow
(
subject
).
to
receive
(
:fetch_disk_repositories
).
and_return
(
disk_repositories
)
# silence_output
end
describe
'#multi_check'
do
context
'all orphans'
do
let
(
:disk_namespaces
)
{
%w(/repos/orphan1 /repos/orphan2 repos/@hashed)
}
let
(
:disk_repositories
)
{
%w(repo1.git repo2.git)
}
it
'prints list of all orphaned namespaces except @hashed'
do
expect_list_of_orphans
(
%w(orphan1/repo1.git orphan1/repo2.git orphan2/repo1.git orphan2/repo2.git)
)
subject
.
multi_check
end
end
context
'few orphans with existing namespace'
do
let!
(
:first_level
)
{
create
(
:group
,
path:
'my-namespace'
)
}
let!
(
:project
)
{
create
(
:project
,
path:
'repo'
,
namespace:
first_level
)
}
let
(
:disk_namespaces
)
{
%w(/repos/orphan1 /repos/orphan2 /repos/my-namespace /repos/@hashed)
}
let
(
:disk_repositories
)
{
%w(repo.git)
}
it
'prints list of orphaned namespaces'
do
expect_list_of_orphans
(
%w(orphan1/repo.git orphan2/repo.git)
)
subject
.
multi_check
end
end
context
'few orphans with existing namespace and parents with same name as orphans'
do
let!
(
:first_level
)
{
create
(
:group
,
path:
'my-namespace'
)
}
let!
(
:second_level
)
{
create
(
:group
,
path:
'second-level'
,
parent:
first_level
)
}
let!
(
:project
)
{
create
(
:project
,
path:
'repo'
,
namespace:
first_level
)
}
let
(
:disk_namespaces
)
{
%w(/repos/orphan1 /repos/orphan2 /repos/my-namespace /repos/second-level /repos/@hashed)
}
let
(
:disk_repositories
)
{
%w(repo.git)
}
it
'prints list of orphaned namespaces ignoring parents with same namespace as orphans'
do
expect_list_of_orphans
(
%w(orphan1/repo.git orphan2/repo.git second-level/repo.git)
)
subject
.
multi_check
end
end
context
'no orphans'
do
let
(
:disk_namespaces
)
{
%w(@hashed)
}
let
(
:disk_repositories
)
{
%w(repo.git)
}
it
'prints an empty list ignoring @hashed'
do
expect_list_of_orphans
([])
subject
.
multi_check
end
end
end
def
expect_list_of_orphans
(
orphans
)
expect
(
subject
).
to
receive
(
:print_orphans
).
with
(
orphans
,
'default'
)
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