Commit 98bd252a authored by Toon Claes's avatar Toon Claes

Merge branch '2955_add_optional_owned_only_param_to_namespaces_endpoint' into 'master'

Add optional owned_only param to namespaces endpoint

See merge request gitlab-org/gitlab!66166
parents 0325de0c a1fee4c7
...@@ -1576,10 +1576,11 @@ class User < ApplicationRecord ...@@ -1576,10 +1576,11 @@ class User < ApplicationRecord
.order('routes.path') .order('routes.path')
end end
def namespaces def namespaces(owned_only: false)
namespace_ids = groups.pluck(:id) user_groups = owned_only ? owned_groups : groups
namespace_ids.push(namespace.id) personal_namespace = Namespace.where(id: namespace.id)
Namespace.where(id: namespace_ids)
Namespace.from_union([user_groups, personal_namespace])
end end
def oauth_authorized_tokens def oauth_authorized_tokens
......
...@@ -21,8 +21,15 @@ administrator, a list of all namespaces in the GitLab instance is shown. ...@@ -21,8 +21,15 @@ administrator, a list of all namespaces in the GitLab instance is shown.
```plaintext ```plaintext
GET /namespaces 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: Example request:
```shell ```shell
...@@ -116,48 +123,6 @@ once a day. ...@@ -116,48 +123,6 @@ once a day.
NOTE: NOTE:
Only group owners are presented with `members_count_with_descendants` and `plan`. 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 namespace by ID
Get a namespace by ID. Get a namespace by ID.
......
...@@ -27,12 +27,15 @@ module API ...@@ -27,12 +27,15 @@ module API
end end
params do params do
optional :search, type: String, desc: "Search query for namespaces" optional :search, type: String, desc: "Search query for namespaces"
optional :owned_only, type: Boolean, desc: "Owned namespaces only"
use :pagination use :pagination
use :optional_list_params_ee use :optional_list_params_ee
end end
get do 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 namespaces = namespaces.include_route
......
...@@ -1798,6 +1798,15 @@ RSpec.describe User do ...@@ -1798,6 +1798,15 @@ RSpec.describe User do
it { expect(user.namespaces).to contain_exactly(user.namespace, group) } it { expect(user.namespaces).to contain_exactly(user.namespace, group) }
it { expect(user.manageable_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 context 'with child groups' do
let!(:subgroup) { create(:group, parent: group) } let!(:subgroup) { create(:group, parent: group) }
......
...@@ -91,6 +91,19 @@ RSpec.describe API::Namespaces do ...@@ -91,6 +91,19 @@ RSpec.describe API::Namespaces do
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(json_response.length).to eq(1) expect(json_response.length).to eq(1)
end 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
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment