Merge origin/main into item-441

This commit is contained in:
2026-11-19 01:37:46 +01:00
49 changed files with 2178 additions and 324 deletions
+83 -45
View File
@@ -9,7 +9,7 @@ import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
from project_config.settings import Epic
from project_config.settings import Epic, Finish
from do_epic.backend.contract import EpicStoreContract
from failure import Failure
from plan_store import jira_epic
@@ -24,8 +24,11 @@ ISSUE_TYPES = f"{SITE}/rest/api/3/issue/createmeta/ABC/issuetypes"
SEARCH = f"{SITE}/rest/api/3/search/jql"
ISSUES = f"{SITE}/rest/api/3/issue"
RANK = f"{SITE}/rest/agile/1.0/issue/rank"
DONE = {"name": "Done", "statusCategory": {"key": "done"}}
TO_DO = {"name": "To Do", "statusCategory": {"key": "new"}}
DONE = {"name": "Done", "statusCategory": {"key": "done"}}
TRANSITIONS = f"{ISSUES}/ABC-12/transitions"
EPIC_COMMENT = f"{ISSUES}/ABC-12/comment"
EPIC_STATUS = f"{ISSUES}/ABC-12?fields=status"
TYPES = {"issueTypes": [{"id": "10001", "name": "Task", "subtask": False}, {"id": "10003", "name": "Subtask", "subtask": True}]}
@@ -49,6 +52,8 @@ class FakeJira:
transitioned: list[str] = dataclasses.field(default_factory=list)
# The transitions Jira offers an issue that isn't done.
workflow: list[dict[str, object]] = dataclasses.field(default_factory=lambda: [{"id": "31", "name": "Mark as done", "to": DONE}])
# The epic's own status, which the transition "Mark as done" moves to DONE.
epic_status: dict[str, object] = dataclasses.field(default_factory=lambda: dict(TO_DO))
def __call__(self, method: str, url: str, authorization: str, body: object) -> object:
self.calls.append((method, url))
@@ -58,6 +63,18 @@ class FakeJira:
return self.answers[method, url]
if method == "GET" and url == EPIC_URL:
return {"key": "ABC-12"}
if method == "GET" and url == EPIC_STATUS:
return {"key": "ABC-12", "fields": {"status": self.epic_status}}
if method == "GET" and url == TRANSITIONS:
return {"transitions": [] if self.epic_status == DONE else [{"id": "31", "name": "Mark as done", "to": DONE}, {"id": "21", "name": "Start", "to": {"name": "In Progress", "statusCategory": {"key": "indeterminate"}}}]}
if method == "POST" and url == TRANSITIONS:
assert body == {"transition": {"id": "31"}}, body
self.epic_status = DONE
return None
if method == "POST" and url == EPIC_COMMENT:
assert isinstance(body, dict)
self.comments.setdefault("ABC-12", []).append(body["body"])
return {"id": "1"}
if method == "GET" and url == ISSUE_TYPES:
return TYPES
if method == "POST" and url == SEARCH:
@@ -140,7 +157,7 @@ class Case(unittest.TestCase):
self.jira = FakeJira()
def new_store(self) -> JiraEpic:
return JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf)
return JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf, Finish("Mark as done", "Done"))
class JiraEpicContract(Case, EpicStoreContract):
@@ -166,52 +183,73 @@ class JiraEpicContract(Case, EpicStoreContract):
class Jira(Case):
def test_finishes_an_issue_by_the_one_transition_that_leads_to_a_done_status(self) -> None:
def test_closes_the_epic_by_the_transition_the_settings_name_after_the_summary(self) -> None:
self.assertEqual(self.new_store().close("Shipped."), None)
self.assertEqual(self.jira.calls[-3:], [("GET", TRANSITIONS), ("POST", EPIC_COMMENT), ("POST", TRANSITIONS)])
def test_posts_no_summary_on_an_epic_it_cannot_close(self) -> None:
unnamed = JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf)
wrong_status = JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf, Finish("Start", "Done"))
no_such = JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf, Finish("Close", "Done"))
unconverted = JiraEpic(EPIC, self.env_file, self.jira, to_markdown, lambda markdown: Failure("adf-codec refused it"), Finish("Mark as done", "Done"))
for store, reason in (
(unnamed, "do-epic's settings name no transition to close an epic by: set `jira-finish-transition` and `jira-finish-status` in ~/.claude-projects/do-epic/config.toml, to the names Jira shows, such as \"Mark as done\" and \"Done\""),
(wrong_status, "ABC-12's transition 'Start' leads to the status 'In Progress', not to 'Done', which `jira-finish-status` names"),
(unconverted, "cannot convert the summary to a Jira comment: adf-codec refused it"),
):
self.assertEqual(store.close("Shipped."), Failure(reason))
refused = no_such.close("Shipped.")
assert isinstance(refused, Failure)
self.assertIn("ABC-12 has no transition 'Close' from its status", refused.reason)
self.jira.answers[("GET", TRANSITIONS)] = Failure("Jira answered 503")
self.assertEqual(self.new_store().close("Shipped."), Failure("cannot read ABC-12's transitions: Jira answered 503"))
self.assertEqual((self.jira.comments, self.jira.epic_status), ({}, TO_DO))
def test_says_what_was_posted_where_the_comment_or_the_move_fails(self) -> None:
self.jira.answers[("POST", EPIC_COMMENT)] = Failure("Jira answered 503")
self.assertEqual(self.new_store().close("Shipped."), Failure("cannot comment on ABC-12: Jira answered 503"))
self.assertEqual(self.jira.epic_status, TO_DO)
del self.jira.answers[("POST", EPIC_COMMENT)]
self.jira.answers[("POST", TRANSITIONS)] = Failure("Jira answered 503")
self.assertEqual(self.new_store().close("Shipped."), Failure("posted the summary on ABC-12, but cannot move ABC-12 by 'Mark as done': Jira answered 503; close the epic in Jira rather than posting the summary again"))
def test_says_why_the_epics_status_cannot_be_read(self) -> None:
for answer, reason in ((Failure("Jira answered 503"), "cannot read ABC-12's status: Jira answered 503"), ({"fields": {}}, "ABC-12's status is not in the shape Jira Cloud's REST API version 3 documents: KeyError('status')"), (["ABC-12"], "ABC-12's status is not in the shape Jira Cloud's REST API version 3 documents: TypeError('list indices must be integers or slices, not str')")):
self.jira.answers[("GET", EPIC_STATUS)] = answer
self.assertEqual(self.new_store().closed(), Failure(reason))
def test_finishes_an_issue_by_the_transition_the_settings_name(self) -> None:
store = self.new_store()
key = store.file("ABC-12", "Count the runs.", "", ())
self.jira.workflow = [{"id": "11", "name": "Start", "to": {"name": "In Progress", "statusCategory": {"key": "indeterminate"}}}, {"id": "31", "name": "Mark as done", "to": DONE}]
self.assertIsNone(store.finish(key))
self.assertIsNone(store.finish_issue(key))
self.assertEqual(self.jira.calls[-1], ("POST", f"{ISSUES}/{key}/transitions"))
self.assertEqual(self.jira.issues[0]["fields"]["status"], DONE) # type: ignore[index]
self.assertEqual((self.jira.transitioned, self.jira.issues[0]["fields"]["status"]), (["31"], DONE)) # type: ignore[index]
def test_leaves_an_issue_that_is_done_without_asking_for_its_transitions(self) -> None:
store = self.new_store()
key = store.file("ABC-12", "Count the runs.", "", ())
self.jira.change(key, status=DONE)
self.assertIsNone(store.finish(key))
self.assertNotIn(("GET", f"{ISSUES}/{key}/transitions"), self.jira.calls)
def test_a_finish_names_why_no_move_closes_the_issue(self) -> None:
key = self.new_store().file("ABC-12", "Count the runs.", "", ())
closed = {"name": "Closed", "statusCategory": {"key": "done"}}
start = {"id": "11", "name": "Start", "to": {"name": "In Progress", "statusCategory": {"key": "indeterminate"}}}
both = [{"id": "31", "name": "Mark as done", "to": DONE}, {"id": "41", "name": "Close", "to": closed}]
config = " in `jira-finish-transition`, in ~/.claude-projects/do-epic/config.toml"
for workflow, named, reason in (
([start, {"id": "12", "name": "Pause", "to": {"name": "On Hold", "statusCategory": {"key": "indeterminate"}}}], None, f"{key}'s status has no transition to a done status, only 'Start' to 'In Progress' and 'Pause' to 'On Hold': Jira lists only the transitions the account may make, so move the issue in Jira, or let the account transition issues"),
([start], None, f"{key}'s status has no transition to a done status, only 'Start' to 'In Progress': Jira lists only the transitions the account may make, so move the issue in Jira, or let the account transition issues"),
([], None, f"{key}'s status has no transition to a done status: Jira lists only the transitions the account may make, so move the issue in Jira, or let the account transition issues"),
(both, None, f"{key}'s status has more than one transition to a done status, 'Mark as done' to 'Done' and 'Close' to 'Closed': name the one to use{config}"),
(both, "Resolve", f"{key}'s status has no transition called 'Resolve' to a done status, only 'Mark as done' to 'Done' and 'Close' to 'Closed': correct `jira-finish-transition`, in ~/.claude-projects/do-epic/config.toml"),
([start], "Close", f"{key}'s status has no transition to a done status, only 'Start' to 'In Progress': Jira lists only the transitions the account may make, so move the issue in Jira, or let the account transition issues"),
):
with self.subTest(workflow=workflow, named=named):
self.jira.workflow = workflow # type: ignore[assignment]
self.assertEqual(JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf, named).finish(key), Failure(reason))
self.assertEqual(self.jira.issues[0]["fields"]["status"], TO_DO) # type: ignore[index]
def test_finishes_by_the_transition_the_config_names_where_several_lead_to_a_done_status(self) -> None:
def test_finishes_by_the_named_transition_where_several_lead_to_a_done_status(self) -> None:
key = self.new_store().file("ABC-12", "Count the runs.", "", ())
self.jira.workflow = [{"id": "31", "name": "Resolve", "to": DONE}, {"id": "41", "name": "Close", "to": {"name": "Closed", "statusCategory": {"key": "done"}}}]
self.assertIsNone(JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf, "Close").finish(key))
self.assertEqual(self.jira.calls[-1], ("POST", f"{ISSUES}/{key}/transitions"))
self.assertIsNone(JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf, Finish("Close", "Closed")).finish_issue(key))
self.assertEqual(self.jira.transitioned, ["41"])
def test_finishes_by_the_one_transition_to_a_done_status_whatever_name_the_config_gives(self) -> None:
def test_leaves_an_issue_that_is_done_without_asking_for_its_transitions(self) -> None:
store = JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf)
key = self.new_store().file("ABC-12", "Count the runs.", "", ())
self.jira.workflow = [{"id": "31", "name": "Resolve", "to": DONE}]
self.assertIsNone(JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf, "Close").finish(key))
self.assertEqual(self.jira.transitioned, ["31"])
self.jira.change(key, status=DONE)
self.assertIsNone(store.finish_issue(key))
self.assertNotIn(("GET", f"{ISSUES}/{key}/transitions"), self.jira.calls)
def test_a_finish_names_why_no_move_finishes_the_issue(self) -> None:
key = self.new_store().file("ABC-12", "Count the runs.", "", ())
unnamed = JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf)
wrong_status = JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf, Finish("Mark as done", "Closed"))
no_such = JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf, Finish("Close", "Done"))
self.assertEqual(unnamed.finish_issue(key), Failure("do-epic's settings name no transition to finish an issue by: set `jira-finish-transition` and `jira-finish-status` in ~/.claude-projects/do-epic/config.toml, to the names Jira shows, such as \"Mark as done\" and \"Done\""))
self.assertEqual(wrong_status.finish_issue(key), Failure(f"{key}'s transition 'Mark as done' leads to the status 'Done', not to 'Closed', which `jira-finish-status` names"))
refused = no_such.finish_issue(key)
assert isinstance(refused, Failure)
self.assertIn(f"{key} has no transition 'Close' from its status", refused.reason)
self.assertEqual((self.jira.transitioned, self.jira.issues[0]["fields"]["status"]), ([], TO_DO)) # type: ignore[index]
def test_refuses_to_finish_a_sub_task_of_an_issue_that_is_no_child_of_the_epic_or_an_issue_without_a_parent(self) -> None:
store = self.new_store()
@@ -220,35 +258,35 @@ class Jira(Case):
self.jira.issues.append({"key": key, "fields": fields})
for key in ("ABC-50", "ABC-61"):
with self.subTest(key=key):
self.assertEqual(store.finish(key), Failure(f"{key} is not one of the child issues of the epic ABC-12, so it cannot be finished"))
self.assertEqual(store.finish_issue(key), Failure(f"{key} is not one of the child issues of the epic ABC-12, so it cannot be finished"))
self.assertNotIn("POST", [method for method, _ in self.jira.calls])
def test_a_finish_jira_refuses_says_so_and_that_the_issue_may_have_moved(self) -> None:
store = self.new_store()
key = store.file("ABC-12", "Count the runs.", "", ())
self.jira.answers[("POST", f"{ISSUES}/{key}/transitions")] = Failure("Jira answered 400: Resolution is required")
self.assertEqual(store.finish(key), Failure(f"cannot move {key} to done: Jira answered 400: Resolution is required; unless Jira refused the request, {key} may be done: read it in Jira before moving it again"))
self.assertEqual(store.finish_issue(key), Failure(f"cannot move {key} by 'Mark as done': Jira answered 400: Resolution is required; unless Jira refused the request, the issue may have moved: look at {key} before finishing the item again"))
def test_a_finish_whose_transitions_jira_cannot_give_or_gives_in_another_shape_fails(self) -> None:
store = self.new_store()
key = store.file("ABC-12", "Count the runs.", "", ())
url = ("GET", f"{ISSUES}/{key}/transitions")
self.jira.answers[url] = Failure("Jira answered 503")
self.assertEqual(store.finish(key), Failure(f"cannot read {key}'s transitions: Jira answered 503"))
self.assertEqual(store.finish_issue(key), Failure(f"cannot read {key}'s transitions: Jira answered 503"))
for answer in ({"transitions": [{"id": 31}]}, ["Mark as done"], {"transitions": 5}):
with self.subTest(answer=answer):
self.jira.answers[url] = answer
self.assertEqual(store.finish(key), Failure(f"Jira answered {key}'s transitions in a shape other than the one it documents"))
self.assertEqual(store.finish_issue(key), Failure(f"Jira answered {key}'s transitions in a shape other than the one it documents"))
def test_a_finish_whose_issue_or_parent_jira_cannot_read_says_which(self) -> None:
store = self.new_store()
child = store.file("ABC-12", "Count the runs.", "", ())
task = store.file(child, "Count them in the api.", "", ())
self.jira.answers[("GET", f"{ISSUES}/{task}?fields=parent,status")] = Failure("Jira answered 503")
self.assertEqual(store.finish(task), Failure(f"cannot read {task} to finish it: Jira answered 503"))
self.assertEqual(store.finish_issue(task), Failure(f"cannot read {task} to finish it: Jira answered 503"))
del self.jira.answers[("GET", f"{ISSUES}/{task}?fields=parent,status")]
self.jira.answers[("GET", f"{ISSUES}/{child}?fields=parent,status")] = Failure("Jira answered 503")
self.assertEqual(store.finish(task), Failure(f"cannot read {child} to finish its sub-task: Jira answered 503"))
self.assertEqual(store.finish_issue(task), Failure(f"cannot read {child} to finish its sub-task: Jira answered 503"))
def test_reads_the_sub_tasks_of_more_children_than_one_search_names(self) -> None:
store = self.new_store()