"""Moves a parent issue whose sub-tasks a person filed, once those sub-tasks have landed or are done. Until it moves, such a parent blocks every issue it blocks, and nobody else moves it: the team's automation sees a pull request only on the sub-task that carries it. An item do-epic split closes in `main.split_items_closed`.""" import dataclasses from do_epic.backend.protocol import EpicIssue, EpicStore from do_epic.scheduler import unblocks from do_epic.split import SPLIT_LABEL from failure import Failure def moved(store: EpicStore, issues: list[EpicIssue], unblocking: tuple[str, ...], landed_by: str | None, finishes: bool) -> tuple[list[EpicIssue], list[str]]: """The issues as they read once each parent has moved, and why each move Jira refused was not made. A parent it could not move holds back only itself, so the others still move. A parent whose sub-tasks are all done moves to done, where finishes says the config names how. One whose sub-tasks have all landed, each done or at one of the unblocking statuses, moves by the transition landed_by names, where the config names one. A parent that is done, or has landed itself, never moves back. One without a sub-task, or with one still open, stays as it is.""" refused: list[str] = [] for parent in issues: tasks = [task for task in issues if task.parent == parent.key] # A sub-task do-epic filed under an item it didn't mark is a split that failed halfway, which the next run completes. if parent.done or SPLIT_LABEL in parent.labels or all(task.by_loop for task in tasks): continue if all(task.done for task in tasks): done = store.finish_issue(parent.key) if finishes else None if isinstance(done, Failure): refused.append(f"{parent.key}'s sub-tasks are all done, but it could not move to done: {done.reason}; close it in Jira") elif finishes: issues = [dataclasses.replace(each, done=True) if each.key == parent.key else each for each in issues] elif landed_by and not unblocks(parent.status, unblocking) and all(task.done or unblocks(task.status, unblocking) for task in tasks): status = store.move_issue_by(parent.key, landed_by) if isinstance(status, Failure): refused.append(f"{parent.key}'s sub-tasks have all landed, but it could not move by {landed_by!r}: {status.reason}; move it in Jira") else: issues = [dataclasses.replace(each, status=status) if each.key == parent.key else each for each in issues] return issues, refused