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
+48 -4
View File
@@ -5,7 +5,7 @@ import typing
from adf_codec.convert import adfs_to_markdown, markdown_to_adf, Refused
from atlassian.client import basic_auth, Http, jira_json, ToAdf, ToMarkdown
from plan_store.jira_epic import blocked_by_links, FILED_BY, ISSUE_KEY, ISSUE_TYPE, project_of, PROPERTY, rank_refusal, scores, searched
from plan_store.jira_epic import blocked_by_links, DONE, FILED_BY, ISSUE_KEY, ISSUE_TYPE, offer_of, project_of, PROPERTY, rank_refusal, scores, searched
from project_config.settings import Epic
from do_epic.backend.protocol import EpicIssue, no_child, no_parent, no_words, unfit, unfit_label
from failure import Failure
@@ -116,6 +116,28 @@ class JiraEpic:
posted = self.http("POST", f"{self.epic.site}/rest/api/3/issue/{key}/comment", authorization, {"body": body})
return Failure(f"cannot comment on {key}: {posted.reason}") if isinstance(posted, Failure) else None
def finish(self, key: str) -> None | Failure:
authorization = basic_auth(self.env_file)
if isinstance(authorization, Failure):
return authorization
placed = self.placed(key, authorization, "to finish it")
if isinstance(placed, Failure):
return placed
above, done = placed
if above != self.epic_key:
return no_child(self.epic_key, key, "finished")
if done:
return None
url = f"{self.epic.site}/rest/api/3/issue/{key}/transitions"
offered = self.http("GET", url, authorization, None)
if isinstance(offered, Failure):
return Failure(f"cannot read {key}'s transitions: {offered.reason}")
transition = done_transition(offered, key)
if isinstance(transition, Failure):
return transition
moved = self.http("POST", url, authorization, {"transition": {"id": transition}})
return Failure(f"cannot move {key} to done: {moved.reason}; the issue may have moved all the same: read it in Jira before moving it again") if isinstance(moved, Failure) else None
def not_a_parent(self, parent: str, authorization: str) -> Failure | None:
"""Why nothing can be filed under parent; None where it is the epic or one of its child issues."""
if parent == self.epic_key:
@@ -125,15 +147,22 @@ class JiraEpic:
def parent_of(self, key: str, authorization: str, why: str) -> str | None | Failure:
"""The key of key's parent issue; None where key isn't an issue key or has no parent. A failed read says "cannot read <key> <why>"."""
placed = self.placed(key, authorization, why, "parent")
return placed if isinstance(placed, Failure) else placed[0]
def placed(self, key: str, authorization: str, why: str, asked: str = "parent,status") -> tuple[str | None, bool] | Failure:
"""The key of key's parent issue, None where key isn't an issue key or has no parent, and whether its status is a done one; asked names the fields to read, and a field it leaves out reads as not done. A failed read says "cannot read <key> <why>"."""
if not ISSUE_KEY.fullmatch(key):
return None
return None, False
# A search may not show an issue filed a moment ago, so the issue is asked for by its key.
answered = self.http("GET", f"{self.epic.site}/rest/api/3/issue/{key}?fields=parent", authorization, None)
answered = self.http("GET", f"{self.epic.site}/rest/api/3/issue/{key}?fields={asked}", authorization, None)
if isinstance(answered, Failure):
return Failure(f"cannot read {key} {why}: {answered.reason}")
fields = answered.get("fields") if isinstance(answered, dict) else None
above = fields.get("parent") if isinstance(fields, dict) else None
return above.get("key") if isinstance(above, dict) and isinstance(above.get("key"), str) else None
status = fields.get("status") if isinstance(fields, dict) else None
category = status.get("statusCategory") if isinstance(status, dict) else None
return (above.get("key") if isinstance(above, dict) and isinstance(above.get("key"), str) else None), isinstance(category, dict) and category.get("key") == DONE
def found(self, authorization: str) -> list[Found] | Failure:
"""Each child issue, each followed by its sub-tasks."""
@@ -174,6 +203,21 @@ class JiraEpic:
return Failure(f"{project} has no sub-task issue type; turn sub-tasks on in Jira") if found is None else {"id": found}
def done_transition(offered: object, key: str) -> str | Failure:
"""The ID of the one transition among those Jira offered for the issue named key that leads to a done status; or why no move can finish it. Which of two such moves finishes it is the team's workflow, so none is picked."""
raw = offered.get("transitions") if isinstance(offered, dict) else None
found = [offer for each in raw if (offer := offer_of(each))] if isinstance(raw, list) else []
if not isinstance(raw, list) or len(found) != len(raw):
return Failure(f"Jira answered {key}'s transitions in a shape other than the one it documents")
closing = [(id_, name, status) for id_, name, status, category in found if category == DONE]
if len(closing) == 1:
return closing[0][0]
if closing:
return Failure(f"{key}'s status has more than one transition to a done status, {' and '.join(f'{name!r} to {status!r}' for _, name, status in closing)}: move the issue in Jira, or change the workflow so that one move closes it")
others = " and ".join(f"{name!r} to {status!r}" for _, name, status, _ in found)
return Failure(f"{key}'s status has no transition to a done status" + (f", only {others}" if others else "") + ": move the issue in Jira, or change the workflow so that one move closes it")
def found_of(raw: object) -> Found | Failure:
"""The issue in one entry of a search's answer, or which issue is not in the shape Jira documents."""
key = raw.get("key") if isinstance(raw, dict) else None