161 lines
7.2 KiB
Python
161 lines
7.2 KiB
Python
"""An epic held in memory, for do-epic's tests and its `--dry-run`, held to contract.py like every backend."""
|
|
import dataclasses
|
|
|
|
from do_epic.backend.protocol import EpicIssue, no_blocker, no_child, no_parent, no_scores, 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, scoring, issue start, 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. writes says each write that succeeded, in order."""
|
|
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
|
|
writes: list[str] = dataclasses.field(default_factory=list)
|
|
# The display name of the account the epic's issues are started as.
|
|
account: str = "do-epic"
|
|
|
|
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 = self.next_key()
|
|
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))
|
|
self.writes.append(f"file {key} under {parent}: {summary}" + (f", labelled {', '.join(sorted(labels))}" if labels else "") + (f", blocked by {blocked_by}" if blocked_by else ""))
|
|
return key
|
|
|
|
def next_key(self) -> str:
|
|
"""The key of the next issue filed."""
|
|
return f"{self.epic_key.rpartition('-')[0]}-{len(self.issues) + 100}"
|
|
|
|
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})))
|
|
self.writes.append(f"label {key} with {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])
|
|
self.writes.append(f"rank {key} before {before}")
|
|
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())
|
|
self.writes.append(f"comment on {key}: {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
|
|
self.writes.append(f"close the epic: {summary.strip()}")
|
|
return None
|
|
|
|
def set_scores(self, key: str, scores: dict[str, int], moves: str, comment: str) -> None | Failure:
|
|
if self.fails:
|
|
return Failure(self.fails)
|
|
if refused := no_words(comment):
|
|
return refused
|
|
child = self.within(key)
|
|
if child is None:
|
|
return no_child(self.epic_key, key, "scored")
|
|
fields = no_scores(key, scores, moves, child.scores)
|
|
if isinstance(fields, Failure):
|
|
return fields
|
|
self.change(key, scores=fields.scores, moves=fields.moves)
|
|
self.comments.setdefault(key, []).append(comment.strip())
|
|
self.writes.append(f"score {key} {', '.join(f'{name} {value}' for name, value in scores.items())}, Moves {moves or 'none'}, with the comment: {comment.strip()}")
|
|
return None
|
|
|
|
def start_issue(self, key: str) -> str | None | Failure:
|
|
if self.fails:
|
|
return Failure(self.fails)
|
|
child = self.within(key)
|
|
if child is None:
|
|
return no_child(self.epic_key, key, "started")
|
|
self.writes.append(f"start {key}")
|
|
if child.assignee is None:
|
|
self.change(key, assignee=self.account)
|
|
return None if child.assignee in (None, self.account) else child.assignee
|
|
|
|
def move_issue(self, key: str, status: str) -> None | Failure:
|
|
if self.fails:
|
|
return Failure(self.fails)
|
|
child = self.within(key)
|
|
if child is None:
|
|
return no_child(self.epic_key, key, "moved")
|
|
if child.status.casefold() != status.casefold():
|
|
self.change(key, status=status, done=status.casefold() == "done")
|
|
self.writes.append(f"move {key} to {status}")
|
|
return None
|
|
|
|
def move_issue_by(self, key: str, transition: str, to: tuple[str, ...]) -> str | Failure:
|
|
# The fake holds no workflow, so it cannot tell where a transition leads: each leads to the first status asked for.
|
|
if self.fails:
|
|
return Failure(self.fails)
|
|
issue = self.within(key)
|
|
if issue is None:
|
|
return no_child(self.epic_key, key, "moved")
|
|
if issue.status.casefold() in {name.casefold() for name in to}:
|
|
return issue.status
|
|
self.change(key, status=to[0], done=to[0].casefold() == "done")
|
|
self.writes.append(f"move {key} by {transition}")
|
|
return to[0]
|
|
|
|
def finish_issue(self, key: str) -> None | Failure:
|
|
if self.fails:
|
|
return Failure(self.fails)
|
|
child = self.within(key)
|
|
if child is None:
|
|
return no_child(self.epic_key, key, "finished")
|
|
if not child.done:
|
|
self.change(key, status="Done", done=True)
|
|
self.writes.append(f"finish {key}")
|
|
return None
|
|
|
|
def within(self, key: str) -> EpicIssue | None:
|
|
"""The epic's child issue named key, or the sub-task of one; None where the epic holds neither."""
|
|
children = {issue.key for issue in self.issues if issue.parent == self.epic_key}
|
|
return next((issue for issue in self.issues if issue.key == key and (issue.parent == self.epic_key or issue.parent in children)), 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]
|