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
2cc26a5e
Commit
2cc26a5e
authored
Mar 27, 2019
by
GitLab Bot
Browse files
Options
Browse Files
Download
Plain Diff
Automatic merge of gitlab-org/gitlab-ce master
parents
0950b973
2cc01f12
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
89 additions
and
10 deletions
+89
-10
app/finders/projects_finder.rb
app/finders/projects_finder.rb
+1
-1
app/models/project.rb
app/models/project.rb
+35
-8
changelogs/unreleased/sh-optimize-projects-api.yml
changelogs/unreleased/sh-optimize-projects-api.yml
+5
-0
spec/models/project_spec.rb
spec/models/project_spec.rb
+48
-1
No files found.
app/finders/projects_finder.rb
View file @
2cc26a5e
...
...
@@ -83,7 +83,7 @@ class ProjectsFinder < UnionFinder
if
private_only?
current_user
.
authorized_projects
else
Project
.
public_or_visible_to_user
(
current_user
)
Project
.
public_or_visible_to_user
(
current_user
,
params
[
:visibility_level
]
)
end
end
end
...
...
app/models/project.rb
View file @
2cc26a5e
...
...
@@ -459,14 +459,41 @@ class Project < ActiveRecord::Base
# Returns a collection of projects that is either public or visible to the
# logged in user.
def
self
.
public_or_visible_to_user
(
user
=
nil
)
if
user
where
(
'EXISTS (?) OR projects.visibility_level IN (?)'
,
user
.
authorizations_for_projects
,
Gitlab
::
VisibilityLevel
.
levels_for_user
(
user
))
#
# requested_visiblity_levels: Normally all projects that are visible
# to the user (e.g. internal and public) are queried, but this
# parameter allows the caller to narrow the search space to optimize
# database queries. For instance, a caller may only want to see
# internal projects. Instead of querying for internal and public
# projects and throwing away public projects, this parameter allows
# the query to be targeted for only internal projects.
def
self
.
public_or_visible_to_user
(
user
=
nil
,
requested_visibility_levels
=
[])
return
public_to_user
unless
user
visible_levels
=
Gitlab
::
VisibilityLevel
.
levels_for_user
(
user
)
include_private
=
true
requested_visibility_levels
=
Array
(
requested_visibility_levels
)
if
requested_visibility_levels
.
present?
visible_levels
&=
requested_visibility_levels
include_private
=
requested_visibility_levels
.
include?
(
Gitlab
::
VisibilityLevel
::
PRIVATE
)
end
public_or_internal_rel
=
if
visible_levels
.
present?
where
(
'projects.visibility_level IN (?)'
,
visible_levels
)
else
public_to_user
Project
.
none
end
private_rel
=
if
include_private
where
(
'EXISTS (?)'
,
user
.
authorizations_for_projects
)
else
Project
.
none
end
public_or_internal_rel
.
or
(
private_rel
)
end
# project features may be "disabled", "internal", "enabled" or "public". If "internal",
...
...
changelogs/unreleased/sh-optimize-projects-api.yml
0 → 100644
View file @
2cc26a5e
---
title
:
Optimize /api/v4/projects endpoint for visibility level
merge_request
:
26481
author
:
type
:
performance
spec/models/project_spec.rb
View file @
2cc26a5e
...
...
@@ -2990,7 +2990,7 @@ describe Project do
end
describe
'#any_lfs_file_locks?'
,
:request_store
do
set
(
:project
)
{
create
(
:project
)
}
let!
(
:project
)
{
create
(
:project
)
}
it
'returns false when there are no LFS file locks'
do
expect
(
project
.
any_lfs_file_locks?
).
to
be_falsey
...
...
@@ -3438,6 +3438,53 @@ describe Project do
expect
(
projects
).
to
eq
([
public_project
])
end
end
context
'with requested visibility levels'
do
set
(
:internal_project
)
{
create
(
:project
,
:internal
,
:repository
)
}
set
(
:private_project_2
)
{
create
(
:project
,
:private
)
}
context
'with admin user'
do
set
(
:admin
)
{
create
(
:admin
)
}
it
'returns all projects'
do
projects
=
described_class
.
all
.
public_or_visible_to_user
(
admin
,
[])
expect
(
projects
).
to
match_array
([
public_project
,
private_project
,
private_project_2
,
internal_project
])
end
it
'returns all public and private projects'
do
projects
=
described_class
.
all
.
public_or_visible_to_user
(
admin
,
[
Gitlab
::
VisibilityLevel
::
PUBLIC
,
Gitlab
::
VisibilityLevel
::
PRIVATE
])
expect
(
projects
).
to
match_array
([
public_project
,
private_project
,
private_project_2
])
end
it
'returns all private projects'
do
projects
=
described_class
.
all
.
public_or_visible_to_user
(
admin
,
[
Gitlab
::
VisibilityLevel
::
PRIVATE
])
expect
(
projects
).
to
match_array
([
private_project
,
private_project_2
])
end
end
context
'with regular user'
do
it
'returns authorized projects'
do
projects
=
described_class
.
all
.
public_or_visible_to_user
(
user
,
[])
expect
(
projects
).
to
match_array
([
public_project
,
private_project
,
internal_project
])
end
it
"returns user's public and private projects"
do
projects
=
described_class
.
all
.
public_or_visible_to_user
(
user
,
[
Gitlab
::
VisibilityLevel
::
PUBLIC
,
Gitlab
::
VisibilityLevel
::
PRIVATE
])
expect
(
projects
).
to
match_array
([
public_project
,
private_project
])
end
it
'returns one private project'
do
projects
=
described_class
.
all
.
public_or_visible_to_user
(
user
,
[
Gitlab
::
VisibilityLevel
::
PRIVATE
])
expect
(
projects
).
to
eq
([
private_project
])
end
end
end
end
describe
'.with_feature_available_for_user'
do
...
...
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