Pick each epic item's repo by repo-rule.md and record the pick as a label on the issue
CI / tests (pull_request) Failing after 4m10s
CI / tests (pull_request) Failing after 4m10s
This commit is contained in:
@@ -86,3 +86,24 @@ class EpicStoreContract:
|
||||
self.assertEqual(store.file(store.epic_key, "Count the runs.", "", ("two words",)), Failure("the label 'two words' is empty or holds white space"))
|
||||
self.assertEqual(store.file(store.epic_key, "Count\nthe runs.", "", ()), Failure("the summary must be one line of text"))
|
||||
self.assertEqual(self.issues(store), [])
|
||||
|
||||
def test_adds_a_label_to_a_child_issue_and_keeps_the_others(self) -> None:
|
||||
store = self.new_store()
|
||||
key = self.filed(store, store.epic_key, "Count the runs.", labels=("api",))
|
||||
other = self.filed(store, store.epic_key, "Ship the later thing.")
|
||||
self.assertEqual([store.add_label(key, "repo:log"), store.add_label(key, "repo:log")], [None, None])
|
||||
read = {issue.key: issue for issue in self.issues(store)}
|
||||
self.assertEqual((read[key].labels, read[other].labels), (("api", "repo:log"), ()))
|
||||
|
||||
def test_refuses_to_label_a_sub_task_an_issue_outside_the_epic_or_a_label_with_white_space(self) -> None:
|
||||
store = self.new_store()
|
||||
child = self.filed(store, store.epic_key, "Count the runs.")
|
||||
task = self.filed(store, child, "Count them in the api.")
|
||||
for key, label, reason in (
|
||||
(task, "repo:log", f"{task} is not one of the child issues of the epic {store.epic_key}, so it cannot be labelled"),
|
||||
(child, "two words", "the label 'two words' is empty or holds white space"),
|
||||
):
|
||||
with self.subTest(key=key, label=label):
|
||||
self.assertEqual(store.add_label(key, label), Failure(reason))
|
||||
self.assertIsInstance(store.add_label("ZZZ-1", "repo:log"), Failure)
|
||||
self.assertEqual([issue.labels for issue in self.issues(store)], [(), ()])
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""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_parent, unfit
|
||||
from do_epic.backend.protocol import EpicIssue, no_child, no_parent, unfit
|
||||
from failure import Failure
|
||||
|
||||
|
||||
@@ -28,6 +28,17 @@ class MemoryEpic:
|
||||
self.issues.append(EpicIssue(key, summary, detail.strip(), tuple(sorted(labels)), None, "To Do", False, parent))
|
||||
return key
|
||||
|
||||
def add_label(self, key: str, label: str) -> None | Failure:
|
||||
if self.fails:
|
||||
return Failure(self.fails)
|
||||
if refused := unfit("-", (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 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]
|
||||
|
||||
@@ -6,7 +6,7 @@ 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 project_config.settings import Epic
|
||||
from do_epic.backend.protocol import EpicIssue, no_parent, unfit
|
||||
from do_epic.backend.protocol import EpicIssue, no_child, no_parent, unfit
|
||||
from failure import Failure
|
||||
from plan_store.jira_epic import ISSUE_KEY, ISSUE_TYPE, project_of, searched
|
||||
|
||||
@@ -76,19 +76,38 @@ class JiraEpic:
|
||||
return Failure(f"Jira answered the new issue without an issue key; {PERHAPS_FILED.format(parent=parent)}")
|
||||
return key
|
||||
|
||||
def add_label(self, key: str, label: str) -> None | Failure:
|
||||
authorization = basic_auth(self.env_file)
|
||||
if isinstance(authorization, Failure):
|
||||
return authorization
|
||||
if refused := unfit("-", (label,)):
|
||||
return refused
|
||||
above = self.parent_of(key, authorization, "to label it")
|
||||
if isinstance(above, Failure):
|
||||
return above
|
||||
if above != self.epic_key:
|
||||
return no_child(self.epic_key, key)
|
||||
edited = self.http("PUT", f"{self.epic.site}/rest/api/3/issue/{key}", authorization, {"update": {"labels": [{"add": label}]}})
|
||||
return Failure(f"cannot label {key}: {edited.reason}") if isinstance(edited, Failure) 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:
|
||||
return None
|
||||
if not ISSUE_KEY.fullmatch(parent):
|
||||
return no_parent(self.epic_key, parent)
|
||||
# A search may not show an issue filed a moment ago, so the parent is asked for by its key.
|
||||
answered = self.http("GET", f"{self.epic.site}/rest/api/3/issue/{parent}?fields=parent", authorization, None)
|
||||
above = self.parent_of(parent, authorization, "to file under it")
|
||||
return above if isinstance(above, Failure) else None if above == self.epic_key else no_parent(self.epic_key, parent)
|
||||
|
||||
def parent_of(self, key: str, authorization: str, why: str) -> str | None | Failure:
|
||||
"""The key of the issue above key; None where key is no issue key or has none. why ends the message for a read that fails."""
|
||||
if not ISSUE_KEY.fullmatch(key):
|
||||
return None
|
||||
# A search may not show an issue filed a moment ago, so the issue is asked for by its key.
|
||||
answered = self.http("GET", f"{self.epic.site}/rest/api/3/issue/{key}?fields=parent", authorization, None)
|
||||
if isinstance(answered, Failure):
|
||||
return Failure(f"cannot read {parent} to file under it: {answered.reason}")
|
||||
return Failure(f"cannot read {key} {why}: {answered.reason}")
|
||||
fields = answered.get("fields") if isinstance(answered, dict) else None
|
||||
above = fields.get("parent") if isinstance(fields, dict) else None
|
||||
return None if isinstance(above, dict) and above.get("key") == self.epic_key else no_parent(self.epic_key, parent)
|
||||
return above.get("key") if isinstance(above, dict) and isinstance(above.get("key"), str) else None
|
||||
|
||||
def found(self, authorization: str) -> list[Found] | Failure:
|
||||
"""Each child issue, each followed by its sub-tasks."""
|
||||
|
||||
@@ -43,6 +43,9 @@ class EpicStore(typing.Protocol):
|
||||
def file(self, parent: str, summary: str, detail: str, labels: tuple[str, ...]) -> str | Failure:
|
||||
"""Files an open issue under parent and answers its key: a child issue where parent is the epic, a sub-task where parent is one of its child issues. detail is markdown, and may be empty."""
|
||||
|
||||
def add_label(self, key: str, label: str) -> None | Failure:
|
||||
"""Adds label to the epic's child issue named key, leaving its other labels as they are; adding one it already has changes nothing. A sub-task, or an issue outside the epic, is refused."""
|
||||
|
||||
|
||||
def unfit(summary: str, labels: tuple[str, ...]) -> Failure | None:
|
||||
"""Why no tracker can file an issue with this summary and these labels; None where one can."""
|
||||
@@ -56,3 +59,8 @@ def unfit(summary: str, labels: tuple[str, ...]) -> Failure | None:
|
||||
def no_parent(epic_key: str, parent: str) -> Failure:
|
||||
"""What every backend says when asked to file under parent, which is neither the epic nor one of its child issues."""
|
||||
return Failure(f"{parent} is neither the epic {epic_key} nor one of its child issues, so nothing can be filed under it")
|
||||
|
||||
|
||||
def no_child(epic_key: str, key: str) -> Failure:
|
||||
"""What every backend says when asked to label key, which is not one of the epic's child issues."""
|
||||
return Failure(f"{key} is not one of the child issues of the epic {epic_key}, so it cannot be labelled")
|
||||
|
||||
@@ -59,6 +59,11 @@ class FakeJira:
|
||||
return {"key": held["key"], "fields": {"parent": held["fields"]["parent"]}} # type: ignore[index]
|
||||
if method == "POST" and url == ISSUES:
|
||||
return self.created(body)
|
||||
if method == "PUT" and (held := next((issue for issue in self.issues if url == f"{ISSUES}/{issue['key']}"), None)):
|
||||
assert isinstance(body, dict)
|
||||
added = [change["add"] for change in body["update"]["labels"]]
|
||||
held["fields"]["labels"] = sorted({*held["fields"]["labels"], *added}) # type: ignore[index]
|
||||
return None
|
||||
return Failure(f"Jira answered 404 for {method} {url}")
|
||||
|
||||
def page(self, body: object) -> object:
|
||||
@@ -181,6 +186,14 @@ class Jira(Case):
|
||||
self.assertEqual(store.file("ABC-78", "Count more.", "", ()), Failure("ABC-78 is neither the epic ABC-12 nor one of its child issues, so nothing can be filed under it"))
|
||||
self.assertEqual([call for call in self.jira.calls if call[0] == "POST" and call[1] == ISSUES], [])
|
||||
|
||||
def test_a_label_jira_refuses_is_not_added(self) -> None:
|
||||
store = self.new_store()
|
||||
key = store.file("ABC-12", "Count the runs.", "", ())
|
||||
self.jira.answers[("PUT", f"{ISSUES}/{key}")] = Failure("Jira answered 403")
|
||||
self.assertEqual(store.add_label(str(key), "repo:log"), Failure(f"cannot label {key}: Jira answered 403"))
|
||||
self.jira.answers[("GET", f"{ISSUES}/{key}?fields=parent")] = Failure("Jira answered 500")
|
||||
self.assertEqual(store.add_label(str(key), "repo:log"), Failure(f"cannot read {key} to label it: Jira answered 500"))
|
||||
|
||||
def test_an_epic_the_account_cannot_see_fails_the_read(self) -> None:
|
||||
store = JiraEpic(Epic(SITE, "ABC-99"), self.env_file, self.jira, to_markdown, to_adf)
|
||||
self.assertEqual(store.read(), Failure(f"cannot read the epic ABC-99: Jira answered 404 for GET {SITE}/rest/api/3/issue/ABC-99?fields=summary"))
|
||||
|
||||
Reference in New Issue
Block a user