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
a1fee4c7
Commit
a1fee4c7
authored
Jul 29, 2021
by
Corinna Wiesner
Committed by
Toon Claes
Jul 29, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add optional owned_only param to namespaces endpoint
parent
a639a44d
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
38 additions
and
47 deletions
+38
-47
app/models/user.rb
app/models/user.rb
+5
-4
doc/api/namespaces.md
doc/api/namespaces.md
+7
-42
lib/api/namespaces.rb
lib/api/namespaces.rb
+4
-1
spec/models/user_spec.rb
spec/models/user_spec.rb
+9
-0
spec/requests/api/namespaces_spec.rb
spec/requests/api/namespaces_spec.rb
+13
-0
No files found.
app/models/user.rb
View file @
a1fee4c7
...
...
@@ -1576,10 +1576,11 @@ class User < ApplicationRecord
.
order
(
'routes.path'
)
end
def
namespaces
namespace_ids
=
groups
.
pluck
(
:id
)
namespace_ids
.
push
(
namespace
.
id
)
Namespace
.
where
(
id:
namespace_ids
)
def
namespaces
(
owned_only:
false
)
user_groups
=
owned_only
?
owned_groups
:
groups
personal_namespace
=
Namespace
.
where
(
id:
namespace
.
id
)
Namespace
.
from_union
([
user_groups
,
personal_namespace
])
end
def
oauth_authorized_tokens
...
...
doc/api/namespaces.md
View file @
a1fee4c7
...
...
@@ -21,8 +21,15 @@ administrator, a list of all namespaces in the GitLab instance is shown.
```
plaintext
GET /namespaces
GET /namespaces?search=foobar
GET /namespaces?owned_only=true
```
| Attribute | Type | Required | Description |
| ------------ | ------- | -------- | ----------- |
|
`search`
| string | no | Returns a list of namespaces the user is authorized to view based on the search criteria |
|
`owned_only`
| boolean | no | In GitLab 14.2 and later, returns a list of owned namespaces only |
Example request:
```
shell
...
...
@@ -116,48 +123,6 @@ once a day.
NOTE:
Only group owners are presented with
`members_count_with_descendants`
and
`plan`
.
## Search for namespace
Get all namespaces that match a string in their name or path.
```
plaintext
GET /namespaces?search=foobar
```
| Attribute | Type | Required | Description |
| --------- | ------ | -------- | ----------- |
|
`search`
| string | no | Returns a list of namespaces the user is authorized to see based on the search criteria |
Example request:
```
shell
curl
--header
"PRIVATE-TOKEN: <your_access_token>"
"https://gitlab.example.com/api/v4/namespaces?search=twitter"
```
Example response:
```
json
[
{
"id"
:
4
,
"name"
:
"twitter"
,
"path"
:
"twitter"
,
"kind"
:
"group"
,
"full_path"
:
"twitter"
,
"parent_id"
:
null
,
"avatar_url"
:
null
,
"web_url"
:
"https://gitlab.example.com/groups/twitter"
,
"members_count_with_descendants"
:
2
,
"billable_members_count"
:
2
,
"max_seats_used"
:
0
,
"seats_in_use"
:
0
,
"plan"
:
"default"
,
"trial_ends_on"
:
null
,
"trial"
:
false
}
]
```
## Get namespace by ID
Get a namespace by ID.
...
...
lib/api/namespaces.rb
View file @
a1fee4c7
...
...
@@ -27,12 +27,15 @@ module API
end
params
do
optional
:search
,
type:
String
,
desc:
"Search query for namespaces"
optional
:owned_only
,
type:
Boolean
,
desc:
"Owned namespaces only"
use
:pagination
use
:optional_list_params_ee
end
get
do
namespaces
=
current_user
.
admin
?
Namespace
.
all
:
current_user
.
namespaces
owned_only
=
params
[
:owned_only
]
==
true
namespaces
=
current_user
.
admin
?
Namespace
.
all
:
current_user
.
namespaces
(
owned_only:
owned_only
)
namespaces
=
namespaces
.
include_route
...
...
spec/models/user_spec.rb
View file @
a1fee4c7
...
...
@@ -1798,6 +1798,15 @@ RSpec.describe User do
it
{
expect
(
user
.
namespaces
).
to
contain_exactly
(
user
.
namespace
,
group
)
}
it
{
expect
(
user
.
manageable_namespaces
).
to
contain_exactly
(
user
.
namespace
,
group
)
}
context
'with owned groups only'
do
before
do
other_group
=
create
(
:group
)
other_group
.
add_developer
(
user
)
end
it
{
expect
(
user
.
namespaces
(
owned_only:
true
)).
to
contain_exactly
(
user
.
namespace
,
group
)
}
end
context
'with child groups'
do
let!
(
:subgroup
)
{
create
(
:group
,
parent:
group
)
}
...
...
spec/requests/api/namespaces_spec.rb
View file @
a1fee4c7
...
...
@@ -91,6 +91,19 @@ RSpec.describe API::Namespaces do
expect
(
json_response
).
to
be_an
Array
expect
(
json_response
.
length
).
to
eq
(
1
)
end
context
'with owned_only param'
do
it
'returns only owned groups'
do
group1
.
add_developer
(
user
)
group2
.
add_owner
(
user
)
get
api
(
"/namespaces?owned_only=true"
,
user
)
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
response
).
to
include_pagination_headers
expect
(
json_response
.
map
{
|
resource
|
resource
[
'id'
]
}).
to
match_array
([
user
.
namespace_id
,
group2
.
id
])
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