Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/planecli/commands/work_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import asyncio
import datetime
from typing import Annotated

import cyclopts
Expand Down Expand Up @@ -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]]:
Expand Down Expand Up @@ -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.
Expand All @@ -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()
Expand All @@ -543,6 +574,12 @@ async def create(
if description:
create_data.description_html = f"<p>{description}</p>"

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())
Expand Down Expand Up @@ -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.
Expand All @@ -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()
Expand Down Expand Up @@ -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

Expand Down
31 changes: 29 additions & 2 deletions tests/test_commands/test_work_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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")