Skip to content

Commit 2d5475e

Browse files
committed
feat(functional): update for getting id based on CR
1 parent c020f11 commit 2d5475e

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

gitlab/base.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import textwrap
88
from collections.abc import Iterable
99
from types import ModuleType
10-
from typing import Any, ClassVar, Generic, TYPE_CHECKING, TypeVar
10+
from typing import Any, cast, ClassVar, Generic, TYPE_CHECKING, TypeVar
1111

1212
import gitlab
1313
from gitlab import types as g_types
@@ -351,6 +351,7 @@ class RESTManager(Generic[TObjCls]):
351351
_path: ClassVar[str]
352352
_obj_cls: type[TObjCls]
353353
_from_parent_attrs: dict[str, Any] = {}
354+
_parent_ref_attr: ClassVar[str | None] = None
354355
_types: dict[str, type[g_types.GitlabAttribute]] = {}
355356

356357
_computed_path: str
@@ -389,6 +390,18 @@ def _compute_path(self, path: str | None = None) -> str:
389390
self._parent_attrs = data
390391
return path.format(**data)
391392

393+
def _get_parent_ref_id(self) -> int | str | None:
394+
if self._parent is None or not self._parent_ref_attr:
395+
return None
396+
if not hasattr(self._parent, self._parent_ref_attr):
397+
return None
398+
parent_ref = getattr(self._parent, self._parent_ref_attr)
399+
if parent_ref is None:
400+
return None
401+
if hasattr(parent_ref, "iid"):
402+
return cast(int, parent_ref.iid)
403+
return None
404+
392405
@property
393406
def path(self) -> str:
394407
return self._computed_path

gitlab/mixins.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ class GetMixin(HeadMixin[base.TObjCls]):
7272
_optional_get_attrs: tuple[str, ...] = ()
7373

7474
@exc.on_http_error(exc.GitlabGetError)
75-
def get(self, id: str | int, lazy: bool = False, **kwargs: Any) -> base.TObjCls:
75+
def get(
76+
self, id: str | int | None = None, lazy: bool = False, **kwargs: Any
77+
) -> base.TObjCls:
7678
"""Retrieve a single object.
7779
7880
Args:
79-
id: ID of the object to retrieve
81+
id: ID of the object to retrieve. If not provided, falls back to
82+
_parent_ref_id from the parent object (if available).
8083
lazy: If True, don't request the server, but create a
8184
shallow object giving access to the managers. This is
8285
useful if you want to avoid useless calls to the API.
@@ -89,6 +92,13 @@ def get(self, id: str | int, lazy: bool = False, **kwargs: Any) -> base.TObjCls:
8992
GitlabAuthenticationError: If authentication is not correct
9093
GitlabGetError: If the server cannot perform the request
9194
"""
95+
if id is None:
96+
id = self._get_parent_ref_id()
97+
if id is None:
98+
raise ValueError(
99+
"id is required. Either provide it explicitly or set "
100+
"_parent_ref_attr on the manager to use the parent's reference."
101+
)
92102
if isinstance(id, str):
93103
id = utils.EncodedId(id)
94104
path = f"{self.path}/{id}"
@@ -310,6 +320,8 @@ def update(
310320
"""
311321
new_data = new_data or {}
312322

323+
if id is None:
324+
id = self._get_parent_ref_id()
313325
if id is None:
314326
path = self.path
315327
else:

gitlab/v4/objects/merge_trains.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ProjectMergeTrainMergeRequestManager(
2222
_path = "/projects/{project_id}/merge_trains/merge_requests"
2323
_obj_cls = ProjectMergeTrainMergeRequest
2424
_from_parent_attrs = {"project_id": "project_id"}
25+
_parent_ref_attr = "merge_request"
2526
_update_method: UpdateMethod = UpdateMethod.POST
2627

2728
_update_attrs = RequiredOptional(

0 commit comments

Comments
 (0)