Skip to content

Commit 0f7065d

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. This commit was created with the assistance of AI Closes: #3261
1 parent f78a873 commit 0f7065d

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

gitlab/v4/objects/epics.py

Lines changed: 78 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
@@ -24,11 +25,88 @@
2425

2526
class GroupEpic(ObjectDeleteMixin, SaveMixin, RESTObject):
2627
_id_attr = "iid"
28+
manager: GroupEpicManager
2729

2830
issues: GroupEpicIssueManager
2931
resourcelabelevents: GroupEpicResourceLabelEventManager
3032
notes: GroupEpicNoteManager
3133

34+
def _epic_path(self) -> str:
35+
"""Return the API path for this epic using its real group."""
36+
group_id = getattr(self, "group_id", None)
37+
if group_id is None:
38+
raise AttributeError(
39+
"Cannot compute epic path: attribute 'group_id' is missing."
40+
)
41+
encoded_group_id = gitlab.utils.EncodedId(group_id)
42+
return f"/groups/{encoded_group_id}/epics/{self.encoded_id}"
43+
44+
@exc.on_http_error(exc.GitlabUpdateError)
45+
def save(self, **kwargs: Any) -> dict[str, Any] | None:
46+
"""Save the changes made to the object to the server.
47+
48+
The object is updated to match what the server returns.
49+
50+
This method uses the epic's group_id attribute to construct the correct
51+
API path. This is important when the epic was retrieved from a parent
52+
group but actually belongs to a sub-group.
53+
54+
Args:
55+
**kwargs: Extra options to send to the server (e.g. sudo)
56+
57+
Returns:
58+
The new object data (*not* a RESTObject)
59+
60+
Raises:
61+
GitlabAuthenticationError: If authentication is not correct
62+
GitlabUpdateError: If the server cannot perform the request
63+
"""
64+
updated_data = self._get_updated_data()
65+
# Nothing to update. Server fails if sent an empty dict.
66+
if not updated_data:
67+
return None
68+
69+
# Use the epic's actual group_id to construct the correct path.
70+
path = self._epic_path()
71+
72+
# Validate and transform the data
73+
excludes = [self._id_attr] if self._id_attr else []
74+
self.manager._update_attrs.validate_attrs(data=updated_data, excludes=excludes)
75+
76+
updated_data, files = gitlab.utils._transform_types(
77+
data=updated_data, custom_types=self.manager._types, transform_data=False
78+
)
79+
80+
# Make the request
81+
http_method = self.manager._get_update_method()
82+
server_data = http_method(path, post_data=updated_data, files=files, **kwargs)
83+
if TYPE_CHECKING:
84+
assert isinstance(server_data, dict)
85+
self._update_attrs(server_data)
86+
return server_data
87+
88+
@exc.on_http_error(exc.GitlabDeleteError)
89+
def delete(self, **kwargs: Any) -> None:
90+
"""Delete the object from the server.
91+
92+
This method uses the epic's group_id attribute to construct the correct
93+
API path. This is important when the epic was retrieved from a parent
94+
group but actually belongs to a sub-group.
95+
96+
Args:
97+
**kwargs: Extra options to send to the server (e.g. sudo)
98+
99+
Raises:
100+
GitlabAuthenticationError: If authentication is not correct
101+
GitlabDeleteError: If the server cannot perform the request
102+
"""
103+
if TYPE_CHECKING:
104+
assert self.encoded_id is not None
105+
106+
# Use the epic's actual group_id to construct the correct path.
107+
path = self._epic_path()
108+
self.manager.gitlab.http_delete(path, **kwargs)
109+
32110

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

tests/functional/api/test_epics.py

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

5+
from tests.functional import helpers
6+
37
pytestmark = pytest.mark.gitlab_premium
48

59

@@ -30,3 +34,35 @@ def test_epic_notes(epic):
3034

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

tests/unit/objects/test_epics.py

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

0 commit comments

Comments
 (0)