diff --git a/plane/models/v2/work_item_properties.py b/plane/models/v2/work_item_properties.py index df06a0f..439e669 100644 --- a/plane/models/v2/work_item_properties.py +++ b/plane/models/v2/work_item_properties.py @@ -50,7 +50,8 @@ class WorkItemProperty(BaseModel): class CreateWorkItemPropertyOption(BaseModel): """POST body for a standalone option, or one entry of - `CreateWorkItemProperty.options` seeding OPTION values on create.""" + `CreateWorkItemProperty.options` seeding OPTION values on create. `settings` takes + `{"color": "#RRGGBB"}` (stored upper-cased).""" model_config = ConfigDict(extra="ignore") @@ -59,6 +60,7 @@ class CreateWorkItemPropertyOption(BaseModel): external_id: str | None = None external_source: str | None = None is_default: bool | None = None + settings: dict[str, Any] | None = None class CreateWorkItemProperty(BaseModel): @@ -113,11 +115,14 @@ class WorkItemPropertyOption(BaseModel): external_source: str | None = None is_default: bool | None = None name: str | None = None + settings: dict[str, Any] | None = None sort_order: float | None = None class UpdateWorkItemPropertyOption(BaseModel): - """PATCH body -- every field optional.""" + """PATCH body -- every field optional. `settings` keys merge over the stored ones; + `{"color": None}` clears the color (a `settings=None` is dropped, not sent). A dict, + not a model, so that `None` survives the `exclude_none` dump.""" model_config = ConfigDict(extra="ignore") @@ -126,6 +131,7 @@ class UpdateWorkItemPropertyOption(BaseModel): external_source: str | None = None is_default: bool | None = None name: str | None = None + settings: dict[str, Any] | None = None class WorkItemPropertyContext(BaseModel): @@ -155,7 +161,8 @@ class WorkItemPropertyContext(BaseModel): class WorkItemPropertyContextOptionInput(BaseModel): """One entry of `CreateWorkItemPropertyContext.options`. `id` reconciles an - existing option; omit it to create a new one from `name`.""" + existing option; omit it to create a new one from `name`. `settings` sets the + option's color on either, as on `UpdateWorkItemPropertyOption`.""" model_config = ConfigDict(extra="ignore") @@ -163,6 +170,7 @@ class WorkItemPropertyContextOptionInput(BaseModel): name: str | None = None description: str | None = None is_default: bool | None = None + settings: dict[str, Any] | None = None sort_order: float | None = None diff --git a/plane/models/work_item_properties.py b/plane/models/work_item_properties.py index a014789..1446ef5 100644 --- a/plane/models/work_item_properties.py +++ b/plane/models/work_item_properties.py @@ -192,6 +192,7 @@ class WorkItemPropertyOption(BaseModel): sort_order: float | None = None description: str | None = None logo_props: Any | None = None + settings: dict[str, Any] | None = None is_active: bool | None = None is_default: bool | None = None external_source: str | None = None @@ -205,12 +206,16 @@ class WorkItemPropertyOption(BaseModel): class CreateWorkItemPropertyOption(BaseModel): - """Request model for creating a work item property option.""" + """Request model for creating a work item property option. + + `settings` takes `{"color": "#RRGGBB"}` (stored upper-cased). + """ model_config = ConfigDict(extra="ignore", populate_by_name=True) name: str description: str | None = None + settings: dict[str, Any] | None = None is_active: bool | None = None is_default: bool | None = None external_source: str | None = None @@ -219,12 +224,17 @@ class CreateWorkItemPropertyOption(BaseModel): class UpdateWorkItemPropertyOption(BaseModel): - """Request model for updating a work item property option.""" + """Request model for updating a work item property option. + + `settings` keys merge over the stored ones; `{"color": None}` clears the color (a + `settings=None` is dropped, not sent). + """ model_config = ConfigDict(extra="ignore", populate_by_name=True) name: str | None = None description: str | None = None + settings: dict[str, Any] | None = None is_active: bool | None = None is_default: bool | None = None external_source: str | None = None diff --git a/tests/unit/test_work_item_properties.py b/tests/unit/test_work_item_properties.py index e107e9e..ac46ea8 100644 --- a/tests/unit/test_work_item_properties.py +++ b/tests/unit/test_work_item_properties.py @@ -11,7 +11,9 @@ CreateWorkItemPropertyValue, RichTextValue, UpdateWorkItemProperty, + UpdateWorkItemPropertyOption, WorkItemProperty, + WorkItemPropertyOption, WorkItemPropertyValueDetail, ) from plane.models.work_item_property_configurations import ( @@ -799,6 +801,51 @@ def test_rich_text_is_a_relation_type_not_a_property_type(self) -> None: assert "RICH_TEXT" not in {member.value for member in PropertyType} +class TestOptionColorsOffline: + """An option's color lives in `settings`. The request models ignore unknown fields, so + a color passed before `settings` was declared was silently dropped.""" + + @pytest.fixture + def sent(self, monkeypatch: pytest.MonkeyPatch) -> list[dict]: + client = PlaneClient(api_key="k", base_url="http://plane.invalid") + payloads: list[dict] = [] + + def send(endpoint: str, data: dict | None = None, params: dict | None = None) -> dict: + payloads.append(data or {}) + return {"id": "o", "name": "High", "settings": (data or {}).get("settings", {})} + + monkeypatch.setattr(client.work_item_properties.options, "_post", send) + monkeypatch.setattr(client.work_item_properties.options, "_patch", send) + self.options = client.work_item_properties.options + return payloads + + def test_an_option_reads_its_color(self) -> None: + option = WorkItemPropertyOption.model_validate( + {"id": "o", "name": "High", "settings": {"color": "#DC2626"}} + ) + + assert option.settings == {"color": "#DC2626"} + + def test_create_sends_the_color(self, sent: list[dict]) -> None: + self.options.create( + "ws", + "proj", + "prop", + CreateWorkItemPropertyOption(name="High", settings={"color": "#DC2626"}), + ) + + assert sent[-1] == {"name": "High", "settings": {"color": "#DC2626"}} + + def test_clearing_a_color_sends_null(self, sent: list[dict]) -> None: + """`exclude_none` drops model fields that are None, not dict values, so the + `None` that tells Plane to clear the color reaches the request.""" + self.options.update( + "ws", "proj", "prop", "o", UpdateWorkItemPropertyOption(settings={"color": None}) + ) + + assert sent[-1] == {"settings": {"color": None}} + + class TestRichTextValueOffline: """A rich text value is an object with description_html -- Plane answers a bare string with 400 "Rich text value must be an object".""" diff --git a/tests/v2/test_work_item_properties_resource.py b/tests/v2/test_work_item_properties_resource.py index 41c1115..a8cc285 100644 --- a/tests/v2/test_work_item_properties_resource.py +++ b/tests/v2/test_work_item_properties_resource.py @@ -26,6 +26,7 @@ CreateWorkItemPropertyOption, UpdateWorkItemProperty, UpdateWorkItemPropertyOption, + WorkItemPropertyContextOptionInput, ) PROJECT_BASE = "https://api.example.com/api/v2/workspaces/acme/projects/ENG/work-item-properties" @@ -482,5 +483,99 @@ def test_workspace_options_crud(workspace_options: WorkspaceWorkItemPropertyOpti assert responses.calls[3].request.url == f"{WORKSPACE_BASE}/p1/options/o1/" +# -- Option colors (`settings`) ---------------------------------------------------- + + +@responses.activate +def test_an_option_reads_its_color(options: WorkItemPropertyOptions) -> None: + responses.get( + f"{PROJECT_BASE}/p1/options/o1/", + json={"id": "o1", "name": "Critical", "settings": {"color": "#DC2626"}}, + ) + + assert options.retrieve("acme", "ENG", "p1", "o1").settings == {"color": "#DC2626"} + + +@responses.activate +def test_option_create_and_update_send_the_color( + options: WorkItemPropertyOptions, workspace_options: WorkspaceWorkItemPropertyOptions +) -> None: + """The request models ignore unknown fields, so without `settings` declared a color + was dropped from the body and the request still succeeded.""" + responses.post(f"{PROJECT_BASE}/p1/options/", json={"id": "o1"}, status=201) + responses.patch(f"{WORKSPACE_BASE}/p1/options/o1/", json={"id": "o1"}) + + options.create( + "acme", + "ENG", + "p1", + CreateWorkItemPropertyOption(name="Critical", settings={"color": "#DC2626"}), + ) + workspace_options.update( + "acme", "p1", "o1", UpdateWorkItemPropertyOption(settings={"color": "#16A34A"}) + ) + + assert json.loads(responses.calls[0].request.body) == { + "name": "Critical", + "settings": {"color": "#DC2626"}, + } + assert json.loads(responses.calls[1].request.body) == {"settings": {"color": "#16A34A"}} + + +@responses.activate +def test_clearing_a_color_sends_null(options: WorkItemPropertyOptions) -> None: + """`{"color": None}` is how the API clears a color. `settings` is a dict, not a + model, because `exclude_none` would strip the `None` from a model and send `{}`.""" + responses.patch(f"{PROJECT_BASE}/p1/options/o1/", json={"id": "o1", "settings": {}}) + + options.update( + "acme", "ENG", "p1", "o1", UpdateWorkItemPropertyOption(settings={"color": None}) + ) + + assert json.loads(responses.calls[0].request.body) == {"settings": {"color": None}} + + +@responses.activate +def test_inline_property_options_send_their_color(properties: WorkItemProperties) -> None: + responses.post(f"{PROJECT_BASE}/", json={"id": "p1"}, status=201) + + properties.create( + "acme", + "ENG", + CreateWorkItemProperty( + display_name="Severity", + property_type="OPTION", + options=[CreateWorkItemPropertyOption(name="High", settings={"color": "#DC2626"})], + ), + ) + + body = json.loads(responses.calls[0].request.body) + assert body["options"] == [{"name": "High", "settings": {"color": "#DC2626"}}] + + +@responses.activate +def test_context_options_send_their_color(contexts: WorkItemPropertyContexts) -> None: + responses.patch(f"{WORKSPACE_BASE}/p1/contexts/c1/", json={"id": "c1"}) + + contexts.update( + "acme", + "p1", + "c1", + CreateWorkItemPropertyContext( + options=[ + WorkItemPropertyContextOptionInput(id="o1", settings={"color": "#16A34A"}), + WorkItemPropertyContextOptionInput(name="Low", settings={"color": None}), + ] + ), + ) + + assert json.loads(responses.calls[0].request.body) == { + "options": [ + {"id": "o1", "settings": {"color": "#16A34A"}}, + {"name": "Low", "settings": {"color": None}}, + ] + } + + # -- Navigation: a fetched property reaches its options/contexts -- see # tests/v2/test_loaded_work_item_properties.py.