Files
claude-config/bin/do_epic/backend/fake.py
T

55 lines
2.5 KiB
Python

"""An epic held in memory, for do-epic's tests, held to contract.py like every backend."""
import dataclasses
from do_epic.backend.protocol import EpicIssue, no_child, no_parent, unfit, unfit_label
from failure import Failure
@dataclasses.dataclass
class MemoryEpic:
"""An epic whose issues rank in the order filed, until one is ranked. Where fails is set, every call fails with it as the reason."""
epic_key: str
issues: list[EpicIssue] = dataclasses.field(default_factory=list)
fails: str | None = None
def read(self) -> list[EpicIssue] | Failure:
if self.fails:
return Failure(self.fails)
return [issue for child in self.issues if child.parent == self.epic_key for issue in (child, *(task for task in self.issues if task.parent == child.key))]
def file(self, parent: str, summary: str, detail: str, labels: tuple[str, ...]) -> str | Failure:
if self.fails:
return Failure(self.fails)
if refused := unfit(summary, labels):
return refused
if parent != self.epic_key and parent not in {issue.key for issue in self.issues if issue.parent == self.epic_key}:
return no_parent(self.epic_key, parent)
key = f"{self.epic_key.rpartition('-')[0]}-{len(self.issues) + 100}"
self.issues.append(EpicIssue(key, summary, detail.strip(), tuple(sorted(labels)), None, "To Do", False, parent, by_loop=True))
return key
def add_label(self, key: str, label: str) -> None | Failure:
if self.fails:
return Failure(self.fails)
if refused := unfit_label(label):
return refused
child = next((issue for issue in self.issues if issue.key == key and issue.parent == self.epic_key), None)
if child is None:
return no_child(self.epic_key, key)
self.change(key, labels=tuple(sorted({*child.labels, label})))
return None
def rank(self, key: str, other: str, after: bool = False) -> None | Failure:
if self.fails:
return Failure(self.fails)
children = {issue.key: issue for issue in self.issues if issue.parent == self.epic_key}
if stray := next((each for each in (key, other) if each not in children), None):
return Failure(f"{stray} is not one of the child issues of the epic {self.epic_key}, so it cannot be ranked")
self.issues.remove(children[key])
self.issues.insert(self.issues.index(children[other]) + after, children[key])
return None
def change(self, key: str, **fields: object) -> None:
"""Changes the issue named key as a person would in the tracker, such as assignee="Ada Lovelace"."""
self.issues = [dataclasses.replace(issue, **fields) if issue.key == key else issue for issue in self.issues] # type: ignore[arg-type]