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
+45 -33
View File
@@ -5,15 +5,15 @@ 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, DONE, FILED_BY, ISSUE_KEY, ISSUE_TYPE, offer_of, project_of, PROPERTY, rank_refusal, scores, searched
from project_config.settings import Epic, JIRA_FINISH_TRANSITION
from plan_store.jira_epic import blocked_by_links, DONE, FILED_BY, finishing_transition, ISSUE_KEY, ISSUE_TYPE, move_refusal, PERHAPS_MOVED, project_of, PROPERTY, rank_refusal, scores, searched
from project_config.settings import Epic, Finish, JIRA_FINISH_STATUS, JIRA_FINISH_TRANSITION
from do_epic.backend.protocol import EpicIssue, no_child, no_parent, no_words, unfit, unfit_label
from failure import Failure
FIELDS = ["summary", "description", "labels", "assignee", "status", "parent", "issuelinks"]
# How many child issues one search for sub-tasks names, which keeps its JQL well inside the length Jira takes.
PARENTS_PER_SEARCH = 50
PERHAPS_MOVED = "unless Jira refused the request, {key} may be done: read it in Jira before moving it again"
NO_FINISH = "do-epic's settings name no transition to {to} by: set `{transition}` and `{status}` in ~/.claude-projects/do-epic/config.toml, to the names Jira shows, such as \"Mark as done\" and \"Done\""
PERHAPS_FILED = "unless Jira refused the request, the issue may exist: look under {parent} before filing it again"
@@ -26,14 +26,13 @@ class Found:
@dataclasses.dataclass(frozen=True)
class JiraEpic:
"""The issues of a Jira epic, read and filed with the credentials env_file holds."""
"""The issues of a Jira epic, read and filed with the credentials env_file holds. finish says how the epic itself closes; None where the machine's settings don't say."""
epic: Epic
env_file: pathlib.Path
http: Http = jira_json
to_markdown: ToMarkdown = adfs_to_markdown
to_adf: ToAdf = markdown_to_adf
# The name of the transition to finish an issue by, where its status offers several to a done status.
finish_by: str | None = None
finish: Finish | None = None
@property
def epic_key(self) -> str:
@@ -119,7 +118,41 @@ 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:
def closed(self) -> bool | Failure:
authorization = basic_auth(self.env_file)
if isinstance(authorization, Failure):
return authorization
answered = self.http("GET", f"{self.epic.site}/rest/api/3/issue/{self.epic_key}?fields=status", authorization, None)
if isinstance(answered, Failure):
return Failure(f"cannot read {self.epic_key}'s status: {answered.reason}")
try:
category = answered["fields"]["status"]["statusCategory"]["key"] # type: ignore[index]
except (KeyError, TypeError) as failure:
return Failure(f"{self.epic_key}'s status is not in the shape Jira Cloud's REST API version 3 documents: {failure!r}")
return bool(category == "done")
def close(self, summary: str) -> None | Failure:
if self.finish is None:
return Failure(NO_FINISH.format(to="close an epic", transition=JIRA_FINISH_TRANSITION, status=JIRA_FINISH_STATUS))
authorization = basic_auth(self.env_file)
if isinstance(authorization, Failure):
return authorization
if refused := no_words(summary):
return refused
body = self.to_adf(summary)
if isinstance(body, Failure):
return Failure(f"cannot convert the summary to a Jira comment: {body.reason}")
# The transition is read before the comment is posted, so an epic that cannot move gets no summary.
transition = finishing_transition(self.epic.site, self.epic_key, self.finish, authorization, self.http)
if isinstance(transition, Failure):
return transition
posted = self.http("POST", f"{self.epic.site}/rest/api/3/issue/{self.epic_key}/comment", authorization, {"body": body})
if isinstance(posted, Failure):
return Failure(f"cannot comment on {self.epic_key}: {posted.reason}")
refused_move = move_refusal(self.epic.site, self.epic_key, transition, self.finish, authorization, self.http)
return Failure(f"posted the summary on {self.epic_key}, but {refused_move}; close the epic in Jira rather than posting the summary again") if refused_move else None
def finish_issue(self, key: str) -> None | Failure:
authorization = basic_auth(self.env_file)
if isinstance(authorization, Failure):
return authorization
@@ -135,15 +168,13 @@ class JiraEpic:
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, self.finish_by)
if self.finish is None:
return Failure(NO_FINISH.format(to="finish an issue", transition=JIRA_FINISH_TRANSITION, status=JIRA_FINISH_STATUS))
transition = finishing_transition(self.epic.site, key, self.finish, authorization, self.http)
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}; {PERHAPS_MOVED.format(key=key)}") if isinstance(moved, Failure) else None
refused = move_refusal(self.epic.site, key, transition, self.finish, authorization, self.http)
return Failure(f"{refused}; {PERHAPS_MOVED.format(key=key)}") if refused 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."""
@@ -210,25 +241,6 @@ 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, named: str | None) -> str | Failure:
"""The ID of the transition that finishes the issue named key, among those Jira offered, which are the ones its status has and the account may make. It is the one that leads to a done status, or where there are several, the one called named; or why Jira offered no such move. Which of several finishes an issue is the team's workflow, so none is picked without a name."""
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]
chosen = closing if len(closing) == 1 else [move for move in closing if move[1] == named]
if len(chosen) == 1:
return chosen[0][0]
listed = " and ".join(f"{name!r} to {status!r}" for _, name, status in closing)
if closing and not named:
return Failure(f"{key}'s status has more than one transition to a done status, {listed}: name the one to use in `{JIRA_FINISH_TRANSITION}`, in ~/.claude-projects/do-epic/config.toml")
if closing:
return Failure(f"{key}'s status has no transition called {named!r} to a done status, only {listed}: correct `{JIRA_FINISH_TRANSITION}`, in ~/.claude-projects/do-epic/config.toml")
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 "") + ": Jira lists only the transitions the account may make, so move the issue in Jira, or let the account transition issues")
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