From 14dfee627b0dba1c3585b0ed265c41bd246d4085 Mon Sep 17 00:00:00 2001 From: isletspace Date: Fri, 4 Sep 2026 00:23:02 +0800 Subject: [PATCH] feat(work-items): --start-date/--target-date on wi create and update Plane exposes start_date/target_date on work items, but the CLI had no way to set them, so users had to fall back to raw API calls (resolve the UUID, PATCH the endpoint by hand). A five-step shell dance for a field the SDK models already carry. Also note the API silently ignores mis-spelled date fields with a 200 (asking for due_date is accepted and dropped), so wi update verifies the echoed values after the write and raises APIError when the server did not apply the requested date (ADR-0007 pattern). --- src/planecli/commands/work_items.py | 66 ++++++++++++++++++++++++++ tests/test_commands/test_work_items.py | 31 +++++++++++- 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/src/planecli/commands/work_items.py b/src/planecli/commands/work_items.py index 8030118..9726e4a 100644 --- a/src/planecli/commands/work_items.py +++ b/src/planecli/commands/work_items.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio +import datetime from typing import Annotated import cyclopts @@ -197,6 +198,27 @@ async def _resolve_project_id_async(project: str | None) -> str: return resolved["id"] +def _validate_date(value: str | None, flag: str) -> str | None: + """Validate a YYYY-MM-DD date flag; None passes through unchanged. + + Rejecting bad formats here matters more than for other flags: the API + silently ignores a malformed or misnamed date field and answers 200 + (ADR-0007), so a typo would otherwise look like success. + """ + if value is None: + return None + try: + datetime.date.fromisoformat(value) + except ValueError: + from planecli.exceptions import ValidationError + + raise ValidationError( + f"Invalid date for {flag}: {value!r} (expected YYYY-MM-DD).", + hint=f"Example: {flag} 2026-09-15", + ) + return value + + async def _fetch_project_data( client, workspace: str, project_id: str ) -> tuple[list[dict], list[dict], list[dict]]: @@ -491,6 +513,8 @@ async def create( parent: str | None = None, estimate: Annotated[int | None, Parameter(alias="-e")] = None, description: Annotated[str | None, Parameter(alias="-d")] = None, + start_date: str | None = None, + target_date: str | None = None, json: bool = False, ) -> None: """Create a new work item. @@ -517,9 +541,16 @@ async def create( Story point estimate. description Work item description (plain text). + start_date + Start date (YYYY-MM-DD). Defaults to the creation date when omitted. + target_date + Target end date (YYYY-MM-DD). """ from plane.models.work_items import CreateWorkItem + start_date = _validate_date(start_date, "--start-date") + target_date = _validate_date(target_date, "--target-date") + try: client = get_client() workspace = get_workspace() @@ -543,6 +574,12 @@ async def create( if description: create_data.description_html = f"

{description}

" + if start_date: + create_data.start_date = start_date + + if target_date: + create_data.target_date = target_date + if priority: priority_map = {"0": "none", "1": "urgent", "2": "high", "3": "medium", "4": "low"} create_data.priority = priority_map.get(priority, priority.lower()) @@ -626,6 +663,8 @@ async def update( name: str | None = None, estimate: Annotated[int | None, Parameter(alias="-e")] = None, description: Annotated[str | None, Parameter(alias="-d")] = None, + start_date: str | None = None, + target_date: str | None = None, json: bool = False, ) -> None: """Update a work item. @@ -652,9 +691,16 @@ async def update( Story point estimate. description New description (plain text). + start_date + New start date (YYYY-MM-DD). + target_date + New target end date (YYYY-MM-DD). """ from plane.models.work_items import UpdateWorkItem + start_date = _validate_date(start_date, "--start-date") + target_date = _validate_date(target_date, "--target-date") + try: client = get_client() workspace = get_workspace() @@ -724,11 +770,31 @@ async def update( if clear_labels: update_data.labels = [] + if start_date is not None: + update_data.start_date = start_date + + if target_date is not None: + update_data.target_date = target_date + updated = await run_sdk( client.work_items.update, workspace, project_id, item_id, update_data ) data = _enrich_work_item(updated.model_dump()) + # Verify the server actually applied the dates (ADR-0007: Plane silently + # ignores unknown/mis-spelled fields with a 200, so a missing echo means + # the write was dropped, not applied). + for flag, asked, got in ( + ("--start-date", start_date, data.get("start_date")), + ("--target-date", target_date, data.get("target_date")), + ): + if asked is not None and got != asked: + from planecli.exceptions import APIError + + raise APIError( + f"The server did not apply {flag}: asked {asked!r}, got {got!r}." + ) + # Invalidate work items cache for this project from planecli.cache import invalidate_resource diff --git a/tests/test_commands/test_work_items.py b/tests/test_commands/test_work_items.py index f97cb8c..42348d9 100644 --- a/tests/test_commands/test_work_items.py +++ b/tests/test_commands/test_work_items.py @@ -7,8 +7,15 @@ import pytest from plane.errors import HttpError -from planecli.commands.work_items import WI_COLUMNS, WI_FIELDS, create, list_, update -from planecli.commands.work_items import _enrich_work_item +from planecli.commands.work_items import ( + WI_COLUMNS, + WI_FIELDS, + _enrich_work_item, + _validate_date, + create, + list_, + update, +) def _make_http_error(status_code: int, message: str = "") -> HttpError: @@ -1418,3 +1425,23 @@ async def test_show_no_false_failure_when_fetch_not_attempted( mock_fetch.assert_not_awaited() printed = " ".join(str(c.args[0]) for c in mock_console.print.call_args_list) assert "failed to load" not in printed + + +class TestValidateDate: + """Unit tests for the YYYY-MM-DD flag validator used by wi create/update.""" + + def test_passes_none_through(self): + assert _validate_date(None, "--start-date") is None + + @pytest.mark.parametrize("good", ["2026-09-15", "2026-01-01", "2026-12-31"]) + def test_accepts_valid_dates(self, good): + assert _validate_date(good, "--target-date") == good + + @pytest.mark.parametrize( + "bad", ["2026-9-5", "09-15-2026", "2026-09-31", "tomorrow", ""] + ) + def test_rejects_invalid_dates(self, bad): + from planecli.exceptions import ValidationError + + with pytest.raises(ValidationError): + _validate_date(bad, "--start-date")