93 lines
4.2 KiB
Python
93 lines
4.2 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_blocker, no_child, no_parent, no_words, 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 read, filing, label, comment, rank, issue finish and closing fails with it as the reason. comments holds each issue's comments, and the epic's, by key. done says whether the epic is closed."""
|
|
epic_key: str
|
|
issues: list[EpicIssue] = dataclasses.field(default_factory=list)
|
|
comments: dict[str, list[str]] = dataclasses.field(default_factory=dict)
|
|
fails: str | None = None
|
|
done: bool = False
|
|
|
|
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, ...], blocked_by: str | None = None) -> str | Failure:
|
|
if self.fails:
|
|
return Failure(self.fails)
|
|
if refused := unfit(summary, labels):
|
|
return refused
|
|
children = {issue.key for issue in self.issues if issue.parent == self.epic_key}
|
|
if parent != self.epic_key and parent not in children:
|
|
return no_parent(self.epic_key, parent)
|
|
if blocked_by is not None and blocked_by not in {issue.key for issue in self.issues if issue.parent == self.epic_key or issue.parent in children}:
|
|
return no_blocker(self.epic_key, blocked_by)
|
|
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, blockers=(blocked_by,) if blocked_by else (), 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, "labelled")
|
|
self.change(key, labels=tuple(sorted({*child.labels, label})))
|
|
return None
|
|
|
|
def rank(self, key: str, before: str) -> 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, before) if each not in children), None):
|
|
return Failure(f"{stray} is not one of the child issues of the epic {self.epic_key}, so {key} is not ranked")
|
|
self.issues.remove(children[key])
|
|
self.issues.insert(self.issues.index(children[before]), children[key])
|
|
return None
|
|
|
|
def comment(self, key: str, text: str) -> None | Failure:
|
|
if self.fails:
|
|
return Failure(self.fails)
|
|
if refused := no_words(text):
|
|
return refused
|
|
if not any(issue.key == key and issue.parent == self.epic_key for issue in self.issues):
|
|
return no_child(self.epic_key, key, "commented on")
|
|
self.comments.setdefault(key, []).append(text.strip())
|
|
return None
|
|
|
|
def closed(self) -> bool | Failure:
|
|
return Failure(self.fails) if self.fails else self.done
|
|
|
|
def close(self, summary: str) -> None | Failure:
|
|
if self.fails:
|
|
return Failure(self.fails)
|
|
if refused := no_words(summary):
|
|
return refused
|
|
self.comments.setdefault(self.epic_key, []).append(summary.strip())
|
|
self.done = True
|
|
return None
|
|
|
|
def finish_issue(self, key: str) -> None | Failure:
|
|
if self.fails:
|
|
return Failure(self.fails)
|
|
children = {issue.key for issue in self.issues if issue.parent == self.epic_key}
|
|
child = next((issue for issue in self.issues if issue.key == key and (issue.parent == self.epic_key or issue.parent in children)), None)
|
|
if child is None:
|
|
return no_child(self.epic_key, key, "finished")
|
|
if not child.done:
|
|
self.change(key, status="Done", done=True)
|
|
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]
|