61 lines
3.0 KiB
Python
61 lines
3.0 KiB
Python
"""A human's task that holds up other issues of an epic (docs/decisions.md → `do-epic: drives one Jira epic …`, "Human tasks").
|
|
|
|
A window that starts again, as after a reboot, DMs again, and so does one whose DM failed, on its next read."""
|
|
import pathlib
|
|
|
|
from project_config.settings import by_name, ProjectConfig
|
|
from loop_effects.effects import Effects, say
|
|
from do_epic.backend.protocol import EpicIssue, EpicStore
|
|
from failure import Failure
|
|
from do_epic.repo_pick import recorded, Repo
|
|
from do_epic import scheduler
|
|
from agent_cycle.direct_message import tell
|
|
from agent_cycle import tmux
|
|
|
|
# What the window of a human's task awaits, in its name.
|
|
AWAITS = "a human's task"
|
|
|
|
|
|
def holds_up(key: str, issues: list[EpicIssue]) -> list[str]:
|
|
"""The keys of the open issues that the human's task called key holds up; none where it is no such task, is closed, or an issue blocks it."""
|
|
if not any(chunk.human and chunk.issue == key for chunk in scheduler.chunks(issues)[0]):
|
|
return []
|
|
return scheduler.held_up(issues)[key]
|
|
|
|
|
|
def message(task: EpicIssue, epic: str, held: list[str]) -> str:
|
|
return f"do-epic {epic} awaits you: {task.key}, \"{task.summary}\", blocks {' and '.join(held)}. do-epic goes on once you close {task.key} in Jira."
|
|
|
|
|
|
def notifier_checkout(issues: list[EpicIssue], home: pathlib.Path) -> str | None:
|
|
"""The checkout of the first of issues whose repo has a git checkout and a project config that names a notifier: where the DM finds that notifier, since the window runs in no checkout. None where there is none."""
|
|
for issue in issues:
|
|
pick = recorded(issue)
|
|
found = by_name(pick.name, home) if isinstance(pick, Repo) else None
|
|
if isinstance(found, ProjectConfig) and found.notifier and (found.top / ".git").exists():
|
|
return str(found.top)
|
|
return None
|
|
|
|
|
|
def awaited(task: EpicIssue, epic: str, store: EpicStore, window: tmux.Window, poll: int, home: pathlib.Path, effects: Effects) -> None:
|
|
"""Waits in window for the human to close the human's task `task` of the epic called epic, and returns once it no longer holds up any issue. A read of the epic that fails is said once and tried again."""
|
|
tmux.park(window, AWAITS, effects.tmux)
|
|
told, said = False, None
|
|
while True:
|
|
issues = store.read()
|
|
if isinstance(issues, Failure):
|
|
if issues.reason != said:
|
|
say(f"cannot read the epic, so cannot tell whether {task.key} is closed; trying again every {poll} seconds: {issues.reason}", effects, stdout=True)
|
|
said = issues.reason
|
|
elif not (held := holds_up(task.key, issues)):
|
|
return
|
|
elif not told:
|
|
text = message(task, epic, held)
|
|
say(text, effects, stdout=True)
|
|
# The issues it holds up come first; any other issue of the epic may do where they have no repo with a notifier.
|
|
where = notifier_checkout(sorted(issues, key=lambda issue: issue.key not in held), home)
|
|
if where is None:
|
|
say("no DM went out: no issue of the epic has a repo whose project config names a notifier and whose checkout exists", effects, stdout=True)
|
|
told = where is None or tell(text, effects, where)
|
|
effects.sleep(poll)
|