Finish an epic: a finishing chunk files each repo's release review, has the human confirm each goal, and closes the epic with its summary
This commit is contained in:
@@ -140,6 +140,14 @@ class EpicStoreContract:
|
||||
self.assertIsInstance(store.comment("ZZZ-1", "Hello."), Failure)
|
||||
self.assertEqual([self.comments_on(store, key) for key in (child, task)], [[], []])
|
||||
|
||||
def test_closes_the_epic_once_its_summary_is_posted_and_refuses_a_blank_one(self) -> None:
|
||||
store = self.new_store()
|
||||
self.assertEqual(store.closed(), False)
|
||||
self.assertIsInstance(store.close(" \n"), Failure)
|
||||
self.assertEqual((store.closed(), self.comments_on(store, "ABC-12")), (False, []))
|
||||
self.assertEqual(store.close("## Shipped\n\nThe import.\n"), None)
|
||||
self.assertEqual((store.closed(), self.comments_on(store, "ABC-12")), (True, ["## Shipped\n\nThe import."]))
|
||||
|
||||
def test_reads_an_issue_without_scores_or_blockers_as_one_with_none(self) -> None:
|
||||
store = self.new_store()
|
||||
key = self.filed(store, store.epic_key, "Count the runs.")
|
||||
|
||||
@@ -7,11 +7,12 @@ 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 and rank fails with it as the reason. comments holds each issue's comments, by key."""
|
||||
"""An epic whose issues rank in the order filed, until one is ranked. Where fails is set, every read, filing, label, comment, rank 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:
|
||||
@@ -60,6 +61,18 @@ class MemoryEpic:
|
||||
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 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]
|
||||
|
||||
@@ -5,8 +5,8 @@ import typing
|
||||
|
||||
from adf_codec.convert import adfs_to_markdown, markdown_to_adf, Refused
|
||||
from atlassian.client import basic_auth, Http, jira_json, ToAdf, ToMarkdown
|
||||
from plan_store.jira_epic import blocked_by_links, FILED_BY, ISSUE_KEY, ISSUE_TYPE, project_of, PROPERTY, rank_refusal, scores, searched
|
||||
from project_config.settings import Epic
|
||||
from plan_store.jira_epic import blocked_by_links, FILED_BY, finishing_transition, ISSUE_KEY, ISSUE_TYPE, move_refusal, project_of, PROPERTY, rank_refusal, scores, searched
|
||||
from project_config.settings import Epic, Finish, JIRA_FINISH_STATUS, JIRA_FINISH_TRANSITION
|
||||
from do_epic.backend.protocol import EpicIssue, no_child, no_parent, no_words, unfit, unfit_label
|
||||
from failure import Failure
|
||||
|
||||
@@ -25,12 +25,13 @@ class Found:
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class JiraEpic:
|
||||
"""The issues of a Jira epic, read and filed with the credentials env_file holds."""
|
||||
"""The issues of a Jira epic, read and filed with the credentials env_file holds. finish says how the epic itself closes; None where the machine's settings don't say."""
|
||||
epic: Epic
|
||||
env_file: pathlib.Path
|
||||
http: Http = jira_json
|
||||
to_markdown: ToMarkdown = adfs_to_markdown
|
||||
to_adf: ToAdf = markdown_to_adf
|
||||
finish: Finish | None = None
|
||||
|
||||
@property
|
||||
def epic_key(self) -> str:
|
||||
@@ -116,6 +117,40 @@ class JiraEpic:
|
||||
posted = self.http("POST", f"{self.epic.site}/rest/api/3/issue/{key}/comment", authorization, {"body": body})
|
||||
return Failure(f"cannot comment on {key}: {posted.reason}") if isinstance(posted, Failure) else None
|
||||
|
||||
def closed(self) -> bool | Failure:
|
||||
authorization = basic_auth(self.env_file)
|
||||
if isinstance(authorization, Failure):
|
||||
return authorization
|
||||
answered = self.http("GET", f"{self.epic.site}/rest/api/3/issue/{self.epic_key}?fields=status", authorization, None)
|
||||
if isinstance(answered, Failure):
|
||||
return Failure(f"cannot read {self.epic_key}'s status: {answered.reason}")
|
||||
try:
|
||||
category = answered["fields"]["status"]["statusCategory"]["key"] # type: ignore[index]
|
||||
except (KeyError, TypeError) as failure:
|
||||
return Failure(f"{self.epic_key}'s status is not in the shape Jira Cloud's REST API version 3 documents: {failure!r}")
|
||||
return bool(category == "done")
|
||||
|
||||
def close(self, summary: str) -> None | Failure:
|
||||
if self.finish is None:
|
||||
return Failure(f"do-epic's settings name no transition to close an epic by: set `{JIRA_FINISH_TRANSITION}` and `{JIRA_FINISH_STATUS}`, as config.py's docstring shows")
|
||||
authorization = basic_auth(self.env_file)
|
||||
if isinstance(authorization, Failure):
|
||||
return authorization
|
||||
if refused := no_words(summary):
|
||||
return refused
|
||||
body = self.to_adf(summary)
|
||||
if isinstance(body, Failure):
|
||||
return Failure(f"cannot convert the summary to a Jira comment: {body.reason}")
|
||||
# Read before the comment is posted, so that an epic that cannot move gets no summary, and a second try posts no second one.
|
||||
transition = finishing_transition(self.epic.site, self.epic_key, self.finish, authorization, self.http)
|
||||
if isinstance(transition, Failure):
|
||||
return transition
|
||||
posted = self.http("POST", f"{self.epic.site}/rest/api/3/issue/{self.epic_key}/comment", authorization, {"body": body})
|
||||
if isinstance(posted, Failure):
|
||||
return Failure(f"cannot comment on {self.epic_key}: {posted.reason}")
|
||||
refused_move = move_refusal(self.epic.site, self.epic_key, transition, self.finish, authorization, self.http)
|
||||
return Failure(f"posted the summary on {self.epic_key}, but {refused_move}; close the epic in Jira rather than posting the summary again") if refused_move else None
|
||||
|
||||
def not_a_parent(self, parent: str, authorization: str) -> Failure | None:
|
||||
"""Why nothing can be filed under parent; None where it is the epic or one of its child issues."""
|
||||
if parent == self.epic_key:
|
||||
|
||||
@@ -64,6 +64,13 @@ class EpicStore(typing.Protocol):
|
||||
"""Posts text, markdown, as a comment on the epic's child issue named key, for the people who work the epic to read. A sub-task, an issue outside the epic, or an empty or blank text is refused."""
|
||||
|
||||
|
||||
def closed(self) -> bool | Failure:
|
||||
"""Whether the epic itself is in a status the tracker counts as done."""
|
||||
|
||||
def close(self, summary: str) -> None | Failure:
|
||||
"""Posts summary, markdown, as a comment on the epic, then moves the epic to a status the tracker counts as done. A blank summary is refused, and so is an epic the tracker cannot move so, before anything is posted. Where the move fails after the comment is posted, the Failure says so."""
|
||||
|
||||
|
||||
def unfit(summary: str, labels: tuple[str, ...]) -> Failure | None:
|
||||
"""Why no tracker can file an issue with this summary and these labels; None where a tracker can file it."""
|
||||
if not summary.strip() or "\n" in summary:
|
||||
|
||||
@@ -9,7 +9,7 @@ import unittest.mock
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
|
||||
|
||||
from project_config.settings import Epic
|
||||
from project_config.settings import Epic, Finish
|
||||
from do_epic.backend.contract import EpicStoreContract
|
||||
from failure import Failure
|
||||
from plan_store import jira_epic
|
||||
@@ -25,6 +25,10 @@ SEARCH = f"{SITE}/rest/api/3/search/jql"
|
||||
ISSUES = f"{SITE}/rest/api/3/issue"
|
||||
RANK = f"{SITE}/rest/agile/1.0/issue/rank"
|
||||
TO_DO = {"name": "To Do", "statusCategory": {"key": "new"}}
|
||||
DONE = {"name": "Done", "statusCategory": {"key": "done"}}
|
||||
TRANSITIONS = f"{ISSUES}/ABC-12/transitions"
|
||||
EPIC_COMMENT = f"{ISSUES}/ABC-12/comment"
|
||||
EPIC_STATUS = f"{ISSUES}/ABC-12?fields=status"
|
||||
TYPES = {"issueTypes": [{"id": "10001", "name": "Task", "subtask": False}, {"id": "10003", "name": "Subtask", "subtask": True}]}
|
||||
|
||||
|
||||
@@ -44,6 +48,8 @@ class FakeJira:
|
||||
answers: dict[tuple[str, str], object] = dataclasses.field(default_factory=dict)
|
||||
calls: list[tuple[str, str]] = dataclasses.field(default_factory=list)
|
||||
comments: dict[str, list[object]] = dataclasses.field(default_factory=dict)
|
||||
# The epic's own status, which the transition "Mark as done" moves to DONE.
|
||||
epic_status: dict[str, object] = dataclasses.field(default_factory=lambda: dict(TO_DO))
|
||||
|
||||
def __call__(self, method: str, url: str, authorization: str, body: object) -> object:
|
||||
self.calls.append((method, url))
|
||||
@@ -53,6 +59,18 @@ class FakeJira:
|
||||
return self.answers[method, url]
|
||||
if method == "GET" and url == EPIC_URL:
|
||||
return {"key": "ABC-12"}
|
||||
if method == "GET" and url == EPIC_STATUS:
|
||||
return {"key": "ABC-12", "fields": {"status": self.epic_status}}
|
||||
if method == "GET" and url == TRANSITIONS:
|
||||
return {"transitions": [] if self.epic_status == DONE else [{"id": "31", "name": "Mark as done", "to": DONE}, {"id": "21", "name": "Start", "to": {"name": "In Progress", "statusCategory": {"key": "indeterminate"}}}]}
|
||||
if method == "POST" and url == TRANSITIONS:
|
||||
assert body == {"transition": {"id": "31"}}, body
|
||||
self.epic_status = DONE
|
||||
return None
|
||||
if method == "POST" and url == EPIC_COMMENT:
|
||||
assert isinstance(body, dict)
|
||||
self.comments.setdefault("ABC-12", []).append(body["body"])
|
||||
return {"id": "1"}
|
||||
if method == "GET" and url == ISSUE_TYPES:
|
||||
return TYPES
|
||||
if method == "POST" and url == SEARCH:
|
||||
@@ -127,7 +145,7 @@ class Case(unittest.TestCase):
|
||||
self.jira = FakeJira()
|
||||
|
||||
def new_store(self) -> JiraEpic:
|
||||
return JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf)
|
||||
return JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf, Finish("Mark as done", "Done"))
|
||||
|
||||
|
||||
class JiraEpicContract(Case, EpicStoreContract):
|
||||
@@ -153,6 +171,41 @@ class JiraEpicContract(Case, EpicStoreContract):
|
||||
|
||||
|
||||
class Jira(Case):
|
||||
def test_closes_the_epic_by_the_transition_the_settings_name_after_the_summary(self) -> None:
|
||||
self.assertEqual(self.new_store().close("Shipped."), None)
|
||||
self.assertEqual(self.jira.calls[-3:], [("GET", TRANSITIONS), ("POST", EPIC_COMMENT), ("POST", TRANSITIONS)])
|
||||
|
||||
def test_posts_no_summary_on_an_epic_it_cannot_close(self) -> None:
|
||||
unnamed = JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf)
|
||||
wrong_status = JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf, Finish("Start", "Done"))
|
||||
no_such = JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf, Finish("Close", "Done"))
|
||||
unconverted = JiraEpic(EPIC, self.env_file, self.jira, to_markdown, lambda markdown: Failure("adf-codec refused it"), Finish("Mark as done", "Done"))
|
||||
for store, reason in (
|
||||
(unnamed, "do-epic's settings name no transition to close an epic by: set `jira-finish-transition` and `jira-finish-status`, as config.py's docstring shows"),
|
||||
(wrong_status, "ABC-12's transition 'Start' leads to the status 'In Progress', not to 'Done', which `jira-finish-status` names"),
|
||||
(unconverted, "cannot convert the summary to a Jira comment: adf-codec refused it"),
|
||||
):
|
||||
self.assertEqual(store.close("Shipped."), Failure(reason))
|
||||
refused = no_such.close("Shipped.")
|
||||
assert isinstance(refused, Failure)
|
||||
self.assertIn("ABC-12 has no transition 'Close' from its status", refused.reason)
|
||||
self.jira.answers[("GET", TRANSITIONS)] = Failure("Jira answered 503")
|
||||
self.assertEqual(self.new_store().close("Shipped."), Failure("cannot read ABC-12's transitions: Jira answered 503"))
|
||||
self.assertEqual((self.jira.comments, self.jira.epic_status), ({}, TO_DO))
|
||||
|
||||
def test_says_what_was_posted_where_the_comment_or_the_move_fails(self) -> None:
|
||||
self.jira.answers[("POST", EPIC_COMMENT)] = Failure("Jira answered 503")
|
||||
self.assertEqual(self.new_store().close("Shipped."), Failure("cannot comment on ABC-12: Jira answered 503"))
|
||||
self.assertEqual(self.jira.epic_status, TO_DO)
|
||||
del self.jira.answers[("POST", EPIC_COMMENT)]
|
||||
self.jira.answers[("POST", TRANSITIONS)] = Failure("Jira answered 503")
|
||||
self.assertEqual(self.new_store().close("Shipped."), Failure("posted the summary on ABC-12, but cannot move ABC-12 by 'Mark as done': Jira answered 503; close the epic in Jira rather than posting the summary again"))
|
||||
|
||||
def test_says_why_the_epics_status_cannot_be_read(self) -> None:
|
||||
for answer, reason in ((Failure("Jira answered 503"), "cannot read ABC-12's status: Jira answered 503"), ({"fields": {}}, "ABC-12's status is not in the shape Jira Cloud's REST API version 3 documents: KeyError('status')"), (["ABC-12"], "ABC-12's status is not in the shape Jira Cloud's REST API version 3 documents: TypeError('list indices must be integers or slices, not str')")):
|
||||
self.jira.answers[("GET", EPIC_STATUS)] = answer
|
||||
self.assertEqual(self.new_store().closed(), Failure(reason))
|
||||
|
||||
def test_reads_the_sub_tasks_of_more_children_than_one_search_names(self) -> None:
|
||||
store = self.new_store()
|
||||
children = [store.file("ABC-12", f"Item {n}.", "", ()) for n in range(60)]
|
||||
|
||||
Reference in New Issue
Block a user