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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
dfbfd3c7
Commit
dfbfd3c7
authored
Nov 23, 2017
by
Tomasz Maczukin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow request namespace by ID or path
parent
5c2578e6
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
72 additions
and
28 deletions
+72
-28
doc/api/namespaces.md
doc/api/namespaces.md
+21
-1
lib/api/helpers.rb
lib/api/helpers.rb
+22
-0
lib/api/namespaces.rb
lib/api/namespaces.rb
+2
-16
spec/requests/api/namespaces_spec.rb
spec/requests/api/namespaces_spec.rb
+27
-11
No files found.
doc/api/namespaces.md
View file @
dfbfd3c7
...
@@ -100,7 +100,7 @@ GET /namespaces/:id
...
@@ -100,7 +100,7 @@ GET /namespaces/:id
| Attribute | Type | Required | Description |
| Attribute | Type | Required | Description |
| --------- | ---- | -------- | ----------- |
| --------- | ---- | -------- | ----------- |
|
`id`
| integer
| yes | ID
of the namespace |
|
`id`
| integer
/string | yes | ID or path
of the namespace |
Example request:
Example request:
...
@@ -121,3 +121,23 @@ Example response:
...
@@ -121,3 +121,23 @@ Example response:
"members_count_with_descendants"
:
2
"members_count_with_descendants"
:
2
}
}
```
```
Example request:
```
bash
curl
--header
"PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK"
https://gitlab.example.com/api/v4/namespaces/group1
```
Example response:
```
json
{
"id"
:
2
,
"name"
:
"group1"
,
"path"
:
"group1"
,
"kind"
:
"group"
,
"full_path"
:
"group1"
,
"parent_id"
:
"null"
,
"members_count_with_descendants"
:
2
}
```
lib/api/helpers.rb
View file @
dfbfd3c7
...
@@ -50,6 +50,10 @@ module API
...
@@ -50,6 +50,10 @@ module API
initial_current_user
!=
current_user
initial_current_user
!=
current_user
end
end
def
user_namespace
@user_namespace
||=
find_namespace!
(
params
[
:id
])
end
def
user_group
def
user_group
@group
||=
find_group!
(
params
[
:id
])
@group
||=
find_group!
(
params
[
:id
])
end
end
...
@@ -112,6 +116,24 @@ module API
...
@@ -112,6 +116,24 @@ module API
end
end
end
end
def
find_namespace
(
id
)
if
id
.
to_s
=~
/^\d+$/
Namespace
.
find_by
(
id:
id
)
else
Namespace
.
find_by_full_path
(
id
)
end
end
def
find_namespace!
(
id
)
namespace
=
find_namespace
(
id
)
if
can?
(
current_user
,
:admin_namespace
,
namespace
)
namespace
else
not_found!
(
'Namespace'
)
end
end
def
find_project_label
(
id
)
def
find_project_label
(
id
)
label
=
available_labels
.
find_by_id
(
id
)
||
available_labels
.
find_by_title
(
id
)
label
=
available_labels
.
find_by_id
(
id
)
||
available_labels
.
find_by_title
(
id
)
label
||
not_found!
(
'Label'
)
label
||
not_found!
(
'Label'
)
...
...
lib/api/namespaces.rb
View file @
dfbfd3c7
...
@@ -24,24 +24,10 @@ module API
...
@@ -24,24 +24,10 @@ module API
success
Entities
::
Namespace
success
Entities
::
Namespace
end
end
params
do
params
do
requires
:id
,
type:
Integer
,
desc:
"Namespace's ID
"
requires
:id
,
type:
String
,
desc:
"Namespace's ID or path
"
end
end
get
':id'
do
get
':id'
do
namespace
=
Namespace
.
find
(
params
[
:id
])
present
user_namespace
,
with:
Entities
::
Namespace
,
current_user:
current_user
authenticate_get_namespace!
(
namespace
)
present
namespace
,
with:
Entities
::
Namespace
,
current_user:
current_user
end
end
helpers
do
def
authenticate_get_namespace!
(
namespace
)
return
if
current_user
.
admin?
forbidden!
(
'No access granted'
)
unless
user_can_access_namespace?
(
namespace
)
end
def
user_can_access_namespace?
(
namespace
)
namespace
.
has_owner?
(
current_user
)
end
end
end
end
end
end
...
...
spec/requests/api/namespaces_spec.rb
View file @
dfbfd3c7
...
@@ -95,19 +95,36 @@ describe API::Namespaces do
...
@@ -95,19 +95,36 @@ describe API::Namespaces do
describe
'GET /namespaces/:id'
do
describe
'GET /namespaces/:id'
do
let
(
:owned_group
)
{
group1
}
let
(
:owned_group
)
{
group1
}
shared_examples
'can access namespace'
do
it
'returns namespace details'
do
get
api
(
"/namespaces/
#{
namespace_id
}
"
,
request_actor
)
expect
(
response
).
to
have_gitlab_http_status
(
200
)
expect
(
json_response
[
'id'
]).
to
eq
(
requested_namespace
.
id
)
expect
(
json_response
[
'path'
]).
to
eq
(
requested_namespace
.
path
)
expect
(
json_response
[
'name'
]).
to
eq
(
requested_namespace
.
name
)
end
end
shared_examples
'namespace reader'
do
shared_examples
'namespace reader'
do
let
(
:requested_namespace
)
{
owned_group
}
before
do
before
do
owned_group
.
add_owner
(
request_actor
)
owned_group
.
add_owner
(
request_actor
)
end
end
context
'when namespace exists'
do
context
'when namespace exists'
do
it
'returns namespace details
'
do
context
'when requested by ID
'
do
get
api
(
"/namespaces/
#{
owned_group
.
id
}
"
,
request_actor
)
let
(
:namespace_id
)
{
owned_group
.
id
}
expect
(
response
).
to
have_gitlab_http_status
(
200
)
it_behaves_like
'can access namespace'
end
context
'when requested by path'
do
let
(
:namespace_id
)
{
owned_group
.
path
}
expect
(
json_response
[
'id'
]).
to
eq
(
owned_group
.
id
)
it_behaves_like
'can access namespace'
expect
(
json_response
[
'name'
]).
to
eq
(
owned_group
.
name
)
end
end
end
end
...
@@ -132,10 +149,10 @@ describe API::Namespaces do
...
@@ -132,10 +149,10 @@ describe API::Namespaces do
let
(
:request_actor
)
{
user
}
let
(
:request_actor
)
{
user
}
context
'when requested namespace is not owned by user'
do
context
'when requested namespace is not owned by user'
do
it
'returns
authentication error
'
do
it
'returns
not-found
'
do
get
api
(
"/namespaces/
#{
group2
.
id
}
"
,
request_actor
)
get
api
(
"/namespaces/
#{
group2
.
id
}
"
,
request_actor
)
expect
(
response
).
to
have_gitlab_http_status
(
40
3
)
expect
(
response
).
to
have_gitlab_http_status
(
40
4
)
end
end
end
end
...
@@ -148,11 +165,10 @@ describe API::Namespaces do
...
@@ -148,11 +165,10 @@ describe API::Namespaces do
let
(
:request_actor
)
{
admin
}
let
(
:request_actor
)
{
admin
}
context
'when requested namespace is not owned by user'
do
context
'when requested namespace is not owned by user'
do
it
'returns authentication error'
do
let
(
:namespace_id
)
{
group2
.
id
}
get
api
(
"/namespaces/
#{
group2
.
id
}
"
,
request_actor
)
let
(
:requested_namespace
)
{
group2
}
expect
(
response
).
to
have_gitlab_http_status
(
200
)
it_behaves_like
'can access namespace'
end
end
end
context
'when requested namespace is owned by user'
do
context
'when requested namespace is owned by 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