Move an epic's issue to done in Jira when its chunk finishes
CI / tests (pull_request) Successful in 4m20s

This commit is contained in:
2026-11-18 23:04:34 +01:00
parent ea3da03468
commit 42cff3a8a7
9 changed files with 182 additions and 9 deletions
+57 -2
View File
@@ -24,6 +24,7 @@ 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"}}
TYPES = {"issueTypes": [{"id": "10001", "name": "Task", "subtask": False}, {"id": "10003", "name": "Subtask", "subtask": True}]}
@@ -44,6 +45,8 @@ class FakeJira:
answers: dict[tuple[str, str], object] = dataclasses.field(default_factory=dict)
calls: list[tuple[str, str]] = dataclasses.field(default_factory=list)
comments: dict[str, list[object]] = dataclasses.field(default_factory=dict)
# 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}])
def __call__(self, method: str, url: str, authorization: str, body: object) -> object:
self.calls.append((method, url))
@@ -57,8 +60,15 @@ class FakeJira:
return TYPES
if method == "POST" and url == SEARCH:
return self.page(body)
if method == "GET" and (held := next((issue for issue in self.issues if url == f"{ISSUES}/{issue['key']}?fields=parent"), None)):
return {"key": held["key"], "fields": {"parent": held["fields"]["parent"]}} # type: ignore[index]
if method == "GET" and (held := next((issue for issue in self.issues if url.startswith(f"{ISSUES}/{issue['key']}?fields=")), None)):
asked = url.partition("?fields=")[2].split(",")
return {"key": held["key"], "fields": {name: held["fields"][name] for name in asked}} # type: ignore[index]
if method == "GET" and (held := next((issue for issue in self.issues if url == f"{ISSUES}/{issue['key']}/transitions"), None)):
return {"transitions": [] if held["fields"]["status"] == DONE else self.workflow} # type: ignore[index]
if method == "POST" and (held := next((issue for issue in self.issues if url == f"{ISSUES}/{issue['key']}/transitions"), None)):
assert isinstance(body, dict)
held["fields"]["status"] = DONE # type: ignore[index]
return None
if method == "POST" and url == ISSUES:
return self.created(body)
if method == "PUT" and url == RANK:
@@ -153,6 +163,51 @@ class JiraEpicContract(Case, EpicStoreContract):
class Jira(Case):
def test_finishes_an_issue_by_the_one_transition_that_leads_to_a_done_status(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.assertEqual(self.jira.calls[-1], ("POST", f"{ISSUES}/{key}/transitions"))
self.assertEqual(self.jira.issues[0]["fields"]["status"], 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_one_move_closes_the_issue(self) -> None:
store = self.new_store()
key = store.file("ABC-12", "Count the runs.", "", ())
wont = {"name": "Closed", "statusCategory": {"key": "done"}}
start = {"id": "11", "name": "Start", "to": {"name": "In Progress", "statusCategory": {"key": "indeterminate"}}}
for workflow, reason in (
([start], f"{key}'s status has no transition to a done status, only 'Start' to 'In Progress'"),
([], f"{key}'s status has no transition to a done status"),
([{"id": "31", "name": "Mark as done", "to": DONE}, {"id": "41", "name": "Won't do", "to": wont}], f"{key}'s status has more than one transition to a done status, 'Mark as done' to 'Done' and \"Won't do\" to 'Closed'"),
):
with self.subTest(workflow=workflow):
self.jira.workflow = workflow # type: ignore[assignment]
self.assertEqual(store.finish(key), Failure(f"{reason}: move the issue in Jira, or change the workflow so that one move closes it"))
self.assertEqual(self.jira.issues[0]["fields"]["status"], TO_DO) # type: ignore[index]
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; the issue may have moved all the same: read it in Jira before moving it 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.jira.answers[url] = {"transitions": [{"id": 31}]}
self.assertEqual(store.finish(key), Failure(f"Jira answered {key}'s transitions in a shape other than the one it documents"))
def test_reads_the_sub_tasks_of_more_children_than_one_search_names(self) -> None:
store = self.new_store()
children = [store.file("ABC-12", f"Item {n}.", "", ()) for n in range(60)]