Skip to content

Commit 45a8599

Browse files
fix(epics): use actual group_id for save/delete operations on nested epics
When an epic belonging to a subgroup is retrieved through a parent group's epic listing, save() and delete() operations would fail because they used the parent group's path instead of the epic's actual group_id. This commit overrides save() and delete() methods in GroupEpic to use the epic's group_id attribute to construct the correct API path, ensuring operations work correctly regardless of how the epic was retrieved. Also add the ability to pass a custom path using `_pg_custom_path` to the `UpdateMixin.update()` and `SaveMixin.save()` methods. This allowed the override of the `update()` method to re-use the `SaveMixin.save()` method. Closes: #3261
1 parent 4221195 commit 45a8599

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

gitlab/mixins.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ def update(
314314
path = self.path
315315
else:
316316
path = f"{self.path}/{utils.EncodedId(id)}"
317+
if "_pg_custom_path" in kwargs:
318+
path = kwargs.pop("_pg_custom_path")
317319

318320
excludes = []
319321
if self._obj_cls is not None and self._obj_cls._id_attr is not None:

gitlab/v4/objects/epics.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import Any, TYPE_CHECKING
44

5+
import gitlab.utils
56
from gitlab import exceptions as exc
67
from gitlab import types
78
from gitlab.base import RESTObject
@@ -29,6 +30,63 @@ class GroupEpic(ObjectDeleteMixin, SaveMixin, RESTObject):
2930
resourcelabelevents: GroupEpicResourceLabelEventManager
3031
notes: GroupEpicNoteManager
3132

33+
def _epic_path(self) -> str:
34+
"""Return the API path for this epic using its real group."""
35+
if not hasattr(self, "group_id") or self.group_id is None:
36+
raise AttributeError(
37+
"Cannot compute epic path: attribute 'group_id' is missing."
38+
)
39+
encoded_group_id = gitlab.utils.EncodedId(self.group_id)
40+
return f"/groups/{encoded_group_id}/epics/{self.encoded_id}"
41+
42+
@exc.on_http_error(exc.GitlabUpdateError)
43+
def save(self, **kwargs: Any) -> dict[str, Any] | None:
44+
"""Save the changes made to the object to the server.
45+
46+
The object is updated to match what the server returns.
47+
48+
This method uses the epic's group_id attribute to construct the correct
49+
API path. This is important when the epic was retrieved from a parent
50+
group but actually belongs to a sub-group.
51+
52+
Args:
53+
**kwargs: Extra options to send to the server (e.g. sudo)
54+
55+
Returns:
56+
The new object data (*not* a RESTObject)
57+
58+
Raises:
59+
GitlabAuthenticationError: If authentication is not correct
60+
GitlabUpdateError: If the server cannot perform the request
61+
"""
62+
# Use the epic's actual group_id to construct the correct path.
63+
path = self._epic_path()
64+
65+
# Call SaveMixin.save() method
66+
return super().save(_pg_custom_path=path, **kwargs)
67+
68+
@exc.on_http_error(exc.GitlabDeleteError)
69+
def delete(self, **kwargs: Any) -> None:
70+
"""Delete the object from the server.
71+
72+
This method uses the epic's group_id attribute to construct the correct
73+
API path. This is important when the epic was retrieved from a parent
74+
group but actually belongs to a sub-group.
75+
76+
Args:
77+
**kwargs: Extra options to send to the server (e.g. sudo)
78+
79+
Raises:
80+
GitlabAuthenticationError: If authentication is not correct
81+
GitlabDeleteError: If the server cannot perform the request
82+
"""
83+
if TYPE_CHECKING:
84+
assert self.encoded_id is not None
85+
86+
# Use the epic's actual group_id to construct the correct path.
87+
path = self._epic_path()
88+
self.manager.gitlab.http_delete(path, **kwargs)
89+
3290

3391
class GroupEpicManager(CRUDMixin[GroupEpic]):
3492
_path = "/groups/{group_id}/epics"

tests/functional/api/test_epics.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import uuid
2+
13
import pytest
24

5+
import gitlab.exceptions
6+
from tests.functional import helpers
7+
38
pytestmark = pytest.mark.gitlab_premium
49

510

@@ -30,3 +35,42 @@ def test_epic_notes(epic):
3035

3136
epic.notes.create({"body": "Test note"})
3237
assert epic.notes.list()
38+
39+
40+
def test_epic_save_from_parent_group_updates_subgroup_epic(gl, group):
41+
subgroup_id = uuid.uuid4().hex
42+
subgroup = gl.groups.create(
43+
{
44+
"name": f"subgroup-{subgroup_id}",
45+
"path": f"sg-{subgroup_id}",
46+
"parent_id": group.id,
47+
}
48+
)
49+
50+
nested_epic = subgroup.epics.create(
51+
{"title": f"Nested epic {subgroup_id}", "description": "Nested epic"}
52+
)
53+
54+
try:
55+
fetched_epics = group.epics.list(search=nested_epic.title)
56+
assert fetched_epics, "Expected to discover nested epic via parent group list"
57+
subgroup.epics.get(nested_epic.iid)
58+
59+
fetched_epic = next(
60+
(epic for epic in fetched_epics if epic.id == nested_epic.id), None
61+
)
62+
assert (
63+
fetched_epic is not None
64+
), "Parent group listing did not include nested epic"
65+
66+
new_label = f"nested-{subgroup_id}"
67+
fetched_epic.labels = [new_label]
68+
fetched_epic.save()
69+
70+
refreshed_epic = subgroup.epics.get(nested_epic.iid)
71+
assert new_label in refreshed_epic.labels
72+
finally:
73+
helpers.safe_delete(nested_epic)
74+
with pytest.raises(gitlab.exceptions.GitlabGetError):
75+
subgroup.epics.get(nested_epic.iid)
76+
helpers.safe_delete(subgroup)

tests/unit/objects/test_epics.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pytest
2+
import responses
3+
4+
from gitlab.v4.objects.epics import GroupEpic
5+
6+
7+
def _build_epic(manager, iid=3, group_id=2, title="Epic"):
8+
data = {"iid": iid, "group_id": group_id, "title": title}
9+
return GroupEpic(manager, data)
10+
11+
12+
def test_group_epic_save_uses_actual_group_path(group):
13+
epic_manager = group.epics
14+
epic = _build_epic(epic_manager, title="Original")
15+
epic.title = "Updated"
16+
17+
with responses.RequestsMock() as rsps:
18+
rsps.add(
19+
method=responses.PUT,
20+
url="http://localhost/api/v4/groups/2/epics/3",
21+
json={"iid": 3, "group_id": 2, "title": "Updated"},
22+
content_type="application/json",
23+
status=200,
24+
match=[responses.matchers.json_params_matcher({"title": "Updated"})],
25+
)
26+
27+
epic.save()
28+
29+
assert epic.title == "Updated"
30+
31+
32+
def test_group_epic_delete_uses_actual_group_path(group):
33+
epic_manager = group.epics
34+
epic = _build_epic(epic_manager)
35+
36+
with responses.RequestsMock() as rsps:
37+
rsps.add(
38+
method=responses.DELETE,
39+
url="http://localhost/api/v4/groups/2/epics/3",
40+
status=204,
41+
)
42+
43+
epic.delete()
44+
45+
assert len(epic._updated_attrs) == 0
46+
47+
48+
def test_group_epic_path_requires_group_id(fake_manager):
49+
epic = GroupEpic(manager=fake_manager, attrs={"iid": 5})
50+
51+
with pytest.raises(AttributeError):
52+
epic._epic_path()

0 commit comments

Comments
 (0)