Skip to content

Commit 4882cb2

Browse files
nejchJohnVillalovos
authored andcommitted
feat(namespaces): add support for namespace existence API
1 parent 1ecbc7c commit 4882cb2

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

docs/gl_objects/namespaces.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@ List namespaces::
2323
Search namespaces::
2424

2525
namespaces = gl.namespaces.list(search='foo')
26+
27+
Get a namespace by ID or path::
28+
29+
namespace = gl.namespaces.get("my-namespace")
30+
31+
Get existence of a namespace by path::
32+
33+
namespace = gl.namespaces.exists("new-namespace")
34+
35+
if namespace.exists:
36+
# get suggestions of namespaces that don't already exist
37+
print(namespace.suggests)

gitlab/v4/objects/namespaces.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
from typing import Any, cast, Union
1+
from typing import Any, cast, TYPE_CHECKING, Union
22

3+
from gitlab import cli
4+
from gitlab import exceptions as exc
35
from gitlab.base import RESTManager, RESTObject
46
from gitlab.mixins import RetrieveMixin
7+
from gitlab.utils import EncodedId
58

69
__all__ = [
710
"Namespace",
@@ -20,3 +23,25 @@ class NamespaceManager(RetrieveMixin, RESTManager):
2023

2124
def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> Namespace:
2225
return cast(Namespace, super().get(id=id, lazy=lazy, **kwargs))
26+
27+
@cli.register_custom_action("NamespaceManager", ("namespace", "parent_id"))
28+
@exc.on_http_error(exc.GitlabGetError)
29+
def exists(self, namespace: str, **kwargs: Any) -> Namespace:
30+
"""Get existence of a namespace by path.
31+
32+
Args:
33+
namespace: The path to the namespace.
34+
**kwargs: Extra options to send to the server (e.g. sudo)
35+
36+
Raises:
37+
GitlabAuthenticationError: If authentication is not correct
38+
GitlabGetError: If the server failed to perform the request
39+
40+
Returns:
41+
Data on namespace existence returned from the server.
42+
"""
43+
path = f"{self.path}/{EncodedId(namespace)}/exists"
44+
server_data = self.gitlab.http_get(path, **kwargs)
45+
if TYPE_CHECKING:
46+
assert isinstance(server_data, dict)
47+
return self._obj_cls(self, server_data)

tests/functional/api/test_gitlab.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,20 @@ def test_hooks(gl):
126126

127127

128128
def test_namespaces(gl, get_all_kwargs):
129-
namespace = gl.namespaces.list(**get_all_kwargs)
130-
assert namespace
129+
current_user = gl.user.username
131130

132-
namespace = gl.namespaces.list(search="root", **get_all_kwargs)[0]
131+
namespaces = gl.namespaces.list(**get_all_kwargs)
132+
assert namespaces
133+
134+
namespaces = gl.namespaces.list(search=current_user, **get_all_kwargs)
135+
assert namespaces[0].kind == "user"
136+
137+
namespace = gl.namespaces.get(current_user)
133138
assert namespace.kind == "user"
134139

140+
namespace = gl.namespaces.exists(current_user)
141+
assert namespace.exists
142+
135143

136144
def test_notification_settings(gl):
137145
settings = gl.notificationsettings.get()

0 commit comments

Comments
 (0)