Skip to content

Commit c36cc8a

Browse files
committed
refactor(api): use JsonAttribute for feature flag strategies
Add `JsonAttribute` to `gitlab.types` to handle JSON parsing for CLI arguments. Update `ProjectFeatureFlagManager` to use `JsonAttribute` for the `strategies` attribute, replacing custom `create` and `update` overrides.
1 parent de83fe4 commit c36cc8a

File tree

2 files changed

+11
-53
lines changed

2 files changed

+11
-53
lines changed

gitlab/types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import dataclasses
4+
import json
45
from typing import Any, TYPE_CHECKING
56

67

@@ -49,6 +50,14 @@ def get_for_api(self, *, key: str) -> tuple[str, Any]:
4950
return (key, self._value)
5051

5152

53+
class JsonAttribute(GitlabAttribute):
54+
def set_from_cli(self, cli_value: str) -> None:
55+
if not cli_value.strip():
56+
self._value = None
57+
else:
58+
self._value = json.loads(cli_value)
59+
60+
5261
class _ListArrayAttribute(GitlabAttribute):
5362
"""Helper class to support `list` / `array` types."""
5463

gitlab/v4/objects/feature_flags.py

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
from __future__ import annotations
77

8-
import json
9-
from typing import Any
10-
8+
from gitlab import types
119
from gitlab.base import RESTObject
1210
from gitlab.mixins import CRUDMixin, ObjectDeleteMixin, SaveMixin
1311
from gitlab.types import RequiredOptional
@@ -28,53 +26,4 @@ class ProjectFeatureFlagManager(CRUDMixin[ProjectFeatureFlag]):
2826
)
2927
_update_attrs = RequiredOptional(optional=("description", "active", "strategies"))
3028
_list_filters = ("scope",)
31-
32-
def create(
33-
self, data: dict[str, Any] | None = None, **kwargs: Any
34-
) -> ProjectFeatureFlag:
35-
"""Create a new object.
36-
37-
Args:
38-
data: Parameters to send to the server to create the
39-
resource
40-
**kwargs: Extra options to send to the server (e.g. sudo)
41-
42-
Returns:
43-
A new instance of the managed object class build with
44-
the data sent by the server
45-
"""
46-
# Handle strategies being passed as a JSON string (e.g. from CLI)
47-
if "strategies" in kwargs and isinstance(kwargs["strategies"], str):
48-
kwargs["strategies"] = json.loads(kwargs["strategies"])
49-
if data and "strategies" in data and isinstance(data["strategies"], str):
50-
data["strategies"] = json.loads(data["strategies"])
51-
52-
return super().create(data, **kwargs)
53-
54-
def update(
55-
self,
56-
id: str | int | None = None,
57-
new_data: dict[str, Any] | None = None,
58-
**kwargs: Any,
59-
) -> dict[str, Any]:
60-
"""Update an object on the server.
61-
62-
Args:
63-
id: ID of the object to update (can be None if not required)
64-
new_data: the update data for the object
65-
**kwargs: Extra options to send to the server (e.g. sudo)
66-
67-
Returns:
68-
The new object data (*not* a RESTObject)
69-
"""
70-
# Handle strategies being passed as a JSON string (e.g. from CLI)
71-
if "strategies" in kwargs and isinstance(kwargs["strategies"], str):
72-
kwargs["strategies"] = json.loads(kwargs["strategies"])
73-
if (
74-
new_data
75-
and "strategies" in new_data
76-
and isinstance(new_data["strategies"], str)
77-
):
78-
new_data["strategies"] = json.loads(new_data["strategies"])
79-
80-
return super().update(id, new_data, **kwargs)
29+
_types = {"strategies": types.JsonAttribute}

0 commit comments

Comments
 (0)