Rename do-epic to build-epic, with no alias, and its config folder with it
CI / tests (pull_request) Successful in 4m34s

This commit is contained in:
2026-12-03 22:35:13 +01:00
parent e6b025caaa
commit cded873730
100 changed files with 919 additions and 910 deletions
View File
+48
View File
@@ -0,0 +1,48 @@
"""The build-epic-e2e command: runs build-epic's commands that need no tmux session and no worker against a sandbox epic on a real Jira site (docs/decisions.md → `build-epic: drives one Jira epic …`)."""
import pathlib
import sys
import typing
from failure import Failure
from build_epic.e2e import sandbox, scenario, settings
HELP = f"""usage: build-epic-e2e
Makes an epic in the Jira sandbox, with issues and sub-tasks, drives build-epic against it and deletes it, whatever the outcome.
It reads three environment variables and fails, naming each that is missing:
{chr(10).join(f' {name}: {meaning}' for name, meaning in settings.MEANING.items())}
It prints a line per check, and exits 0 only where every check held."""
EpicMade = typing.Callable[[settings.Sandbox, str], typing.ContextManager[str | Failure]]
Run = typing.Callable[[settings.Sandbox, str, pathlib.Path, typing.Mapping[str, str]], scenario.Report]
def main(argv: list[str], env: typing.Mapping[str, str], launcher: pathlib.Path, epic_made: EpicMade, run: Run) -> int:
"""Runs build-epic-e2e: epic_made makes the sandbox epic, as sandbox.epic_made does, and run drives build-epic against it, as scenario.run does."""
if argv:
print(HELP, file=sys.stdout if argv in (["-h"], ["--help"]) else sys.stderr)
return 0 if argv in (["-h"], ["--help"]) else 1
found = settings.read(env)
if isinstance(found, Failure):
print(f"build-epic-e2e cannot start: {found.reason}", file=sys.stderr)
return 1
report = scenario.Report()
left: sandbox.Leftover | None = None
try:
with epic_made(found, "build-epic e2e sandbox epic") as key:
if isinstance(key, Failure):
print(f"build-epic-e2e: {key.reason}", file=sys.stderr)
return 1
print(f"Sandbox epic {key} in {found.jira_url}")
report = run(found, key, launcher, env)
except sandbox.Leftover as failure:
left = failure
# The checks are printed even where the epic could not be deleted.
for line in report.held:
print(f"ok {line}")
for line in report.broke:
print(f"FAIL {line}")
print(f"{len(report.held)} held, {len(report.broke)} broke")
if left:
print(f"build-epic-e2e: {left}", file=sys.stderr)
return 1 if report.broke or left else 0
+75
View File
@@ -0,0 +1,75 @@
"""The sandbox epic the e2e command makes on the real Jira site and deletes when it ends."""
import contextlib
import typing
from atlassian.client import basic_auth, Http, jira_json, ToAdf
from failure import Failure
from adf_codec.convert import markdown_to_adf
from build_epic.e2e.settings import Sandbox
from atlassian.jira import searched
EPIC_TYPE = "Epic"
# How a team-managed project's workflow moves an issue to done, as build-epic's `jira-finish-transition` and `jira-finish-status` name it.
FINISH_TRANSITION = "Done"
FINISH_STATUS = "Done"
DESCRIPTION = """**Language:** English
## Goals
1. **Achieve:** Prove build-epic's commands against a real Jira site.
## Audience
The person who runs build-epic's e2e command.
"""
class Leftover(Exception):
"""The sandbox epic or one of its issues could not be deleted, so some of them may still be on the site."""
def api(sandbox: Sandbox, path: str) -> str:
return f"{sandbox.jira_url}/rest/api/3/{path}"
@contextlib.contextmanager
def epic_made(sandbox: Sandbox, summary: str, http: Http = jira_json, to_adf: ToAdf = markdown_to_adf) -> typing.Iterator[str | Failure]:
"""Makes an epic in the sandbox project and yields its key; a Failure where it cannot be made. Deletes the epic with its issues and sub-tasks on the way out, however the block ends."""
authorization = basic_auth(sandbox.env_file)
if isinstance(authorization, Failure):
yield authorization
return
description = to_adf(DESCRIPTION)
if isinstance(description, Failure):
yield Failure(f"cannot convert the sandbox epic's description: {description.reason}")
return
fields = {"project": {"key": sandbox.project}, "issuetype": {"name": EPIC_TYPE}, "summary": summary, "description": description}
made = http("POST", api(sandbox, "issue"), authorization, {"fields": fields})
if isinstance(made, Failure):
yield Failure(f"cannot make the sandbox epic: {made.reason}")
return
key = made.get("key") if isinstance(made, dict) else None
if not isinstance(key, str):
yield Failure(f"Jira answered the epic's creation with {made!r}, which holds no key")
return
try:
yield key
finally:
deleted = deleted_with_children(sandbox, key, authorization, http)
if isinstance(deleted, Failure):
raise Leftover(f"the sandbox epic {key} and some of its issues may be left in {sandbox.project}: look for them and delete them by hand ({deleted.reason})")
def deleted_with_children(sandbox: Sandbox, key: str, authorization: str, http: Http) -> None | Failure:
"""Deletes the epic's child issues, with their sub-tasks, and then the epic: Jira deletes an epic's sub-tasks along with it, but not its child issues."""
found = searched(sandbox.jira_url, f"the issues of {key}", f"parent = {key}", {"fields": ["summary"]}, authorization, http)
# The epic is deleted all the same; the first failure is the one to report.
problem = found if isinstance(found, Failure) else None
entries = [] if isinstance(found, Failure) else found
children = [item["key"] for item in entries if isinstance(item, dict) and isinstance(item.get("key"), str)]
if len(children) < len(entries):
problem = Failure(f"the search for the issues of {key} answered an entry without a key, so that issue is not deleted")
for each in [*children, key]:
deleted = http("DELETE", api(sandbox, f"issue/{each}?deleteSubtasks=true"), authorization, None)
problem = problem or (deleted if isinstance(deleted, Failure) else None)
return problem
+147
View File
@@ -0,0 +1,147 @@
"""What the e2e command does to a sandbox epic: runs `build-epic --file`, `--record-decision` and `--close-epic`, each as a process of its own against the real Jira site, and checks what the site holds after each."""
import dataclasses
import pathlib
import subprocess
import tempfile
import typing
from build_epic.repo_pick import CLAUDE_LABEL, HUMAN_LABEL
from loop_state.paths import config_folder, without_xdg_folders
from project_config.settings import Epic, Finish
from build_epic import epic_record, findings, finish
from record_store.jira_description import EpicDescription
from build_epic.backend.protocol import EpicIssue, EpicStore
from failure import Failure
from build_epic.e2e.sandbox import FINISH_STATUS, FINISH_TRANSITION
from build_epic.backend.jira import JiraEpic
from model_routing.ladder_file import read_ladder, SHIPPED
from build_epic.e2e.settings import Sandbox
# Seconds one build-epic command may take.
TIMEOUT = 300
RULE = "A label that equals a repo's name names that repo.\n"
@dataclasses.dataclass
class Report:
"""What the scenario found: the lines of what held, and of what didn't."""
held: list[str] = dataclasses.field(default_factory=list)
broke: list[str] = dataclasses.field(default_factory=list)
def check(self, holds: bool, what: str) -> None:
(self.held if holds else self.broke).append(what)
class BuildEpic(typing.Protocol):
"""What the steps run build-epic through: a Machine, or a stand-in in a test."""
def build_epic(self, *args: str, stdin: str = "") -> subprocess.CompletedProcess[str]:
"""Runs `build-epic args`, feeding it stdin."""
@dataclasses.dataclass(frozen=True)
class Machine:
"""A home folder of its own, holding the build-epic settings that point at the sandbox, so that build-epic writes its state there. The sandbox's env-file stays where it is. build-epic runs in env with HOME set to the home folder, and is stopped after timeout seconds."""
home: pathlib.Path
launcher: pathlib.Path
env: typing.Mapping[str, str]
timeout: float = TIMEOUT
def build_epic(self, *args: str, stdin: str = "") -> subprocess.CompletedProcess[str]:
env = {**self.env, "HOME": str(self.home)}
try:
return subprocess.run([str(self.launcher), *args], input=stdin, env=env, capture_output=True, text=True, timeout=self.timeout, check=False)
except subprocess.TimeoutExpired:
return subprocess.CompletedProcess(args, 124, "", f"build-epic {' '.join(args)} ran over {self.timeout} seconds and was stopped")
def machine_made(home: pathlib.Path, sandbox: Sandbox, launcher: pathlib.Path, env: typing.Mapping[str, str]) -> Machine | Failure:
# So that build-epic keeps its config and state under home.
env = without_xdg_folders(env)
folder = config_folder(env, home) / "build-epic"
folder.mkdir(parents=True)
ladder = read_ladder(SHIPPED)
if isinstance(ladder, Failure):
return ladder
(folder / "config.toml").write_text(
f'jira-url = "{sandbox.jira_url}"\nenv-file = "{sandbox.env_file}"\nrepoless-model = "{ladder.rungs[0].model}"\n'
f'jira-finish-transition = "{FINISH_TRANSITION}"\njira-finish-status = "{FINISH_STATUS}"\n', encoding="utf-8")
(folder / "repo-rule.md").write_text(RULE, encoding="utf-8")
return Machine(home, launcher, env)
def run(sandbox: Sandbox, epic_key: str, launcher: pathlib.Path, env: typing.Mapping[str, str]) -> Report:
"""Drives build-epic against the epic epic_key in the sandbox, an epic with no issue yet."""
report = Report()
epic = Epic(sandbox.jira_url, epic_key)
store = JiraEpic(epic, sandbox.env_file, finish=Finish(FINISH_TRANSITION, FINISH_STATUS))
record = epic_record.EpicRecord(EpicDescription(epic, sandbox.env_file))
with tempfile.TemporaryDirectory() as home:
machine = machine_made(pathlib.Path(home).resolve(), sandbox, launcher, env)
if isinstance(machine, Failure):
report.check(False, f"set up build-epic's settings: {machine.reason}")
return report
items_filed(report, store, epic_key)
finding_filed(report, machine, store, epic_key)
follow_up_filed(report, machine, store, epic_key)
decision_recorded(report, machine, record.section, epic_key)
closed(report, machine, store, epic_key)
return report
def read(report: Report, store: EpicStore, what: str) -> list[EpicIssue]:
issues = store.read()
if isinstance(issues, Failure):
report.check(False, f"read the epic's issues {what}: {issues.reason}")
return []
return issues
def items_filed(report: Report, store: EpicStore, epic_key: str) -> None:
"""Files an item, its sub-task and a second item in the epic, and checks that a read answers them in the tracker's order."""
parent = store.file(epic_key, "Sandbox item with a sub-task", "The first item.", (HUMAN_LABEL,))
second = store.file(epic_key, "Sandbox second item", "", ())
child = store.file(parent, "Sandbox sub-task", "", ()) if isinstance(parent, str) else parent
for what, key in (("an item", parent), ("a second item", second), ("a sub-task", child)):
report.check(isinstance(key, str), f"file {what} in the epic" + (f": {key.reason}" if isinstance(key, Failure) else ""))
issues = read(report, store, "after filing")
report.check([issue.summary for issue in issues] == ["Sandbox item with a sub-task", "Sandbox sub-task", "Sandbox second item"], f"read the filed issues back in order, the sub-task after its parent (found {[issue.summary for issue in issues]})")
def finding_filed(report: Report, machine: BuildEpic, store: EpicStore, epic_key: str) -> None:
done = machine.build_epic(epic_key, findings.FILE, "Sandbox finding", findings.LABEL, CLAUDE_LABEL, stdin="Why the finding matters.\n")
report.check(done.returncode == 0 and " Filed " in done.stdout, f"`build-epic {findings.FILE}` files the finding and says so (exit {done.returncode}: {done.stdout.strip() or done.stderr.strip()})")
found = [issue for issue in read(report, store, "after the finding") if issue.summary == "Sandbox finding"]
report.check(len(found) == 1 and found[0].labels == (CLAUDE_LABEL,) and found[0].detail == "Why the finding matters." and found[0].by_loop, f"the finding is an issue of the epic with its label, its detail and the loop's mark (found {found})")
def follow_up_filed(report: Report, machine: BuildEpic, store: EpicStore, epic_key: str) -> None:
"""Files a follow-up finding that waits for the first finding, and checks that Jira reads the link in that direction."""
first = next((issue.key for issue in read(report, store, "before the follow-up") if issue.summary == "Sandbox finding"), "")
done = machine.build_epic(epic_key, findings.FILE, "Sandbox follow-up finding", findings.LABEL, CLAUDE_LABEL, findings.BLOCKED_BY, first, stdin="Waits for the first finding.\n")
report.check(done.returncode == 0 and f"It is blocked by {first}." in done.stdout, f"`build-epic {findings.FILE} {findings.BLOCKED_BY}` files the follow-up and says it is blocked (exit {done.returncode}: {done.stdout.strip() or done.stderr.strip()})")
issues = read(report, store, "after the follow-up")
waiting = [issue for issue in issues if issue.summary == "Sandbox follow-up finding"]
report.check(len(waiting) == 1 and waiting[0].blockers == (first,) and not any(issue.blockers for issue in issues if issue.key == first), f"the follow-up is blocked by the first finding, not the other way round (found {[(issue.key, issue.blockers) for issue in issues if issue.key == first or issue.summary == 'Sandbox follow-up finding']})")
def decision_recorded(report: Report, machine: BuildEpic, section: typing.Callable[[str], str | Failure], epic_key: str) -> None:
done = machine.build_epic(epic_key, findings.RECORD_DECISION, "Sandbox decision", stdin="The text of the decision.\n")
report.check(done.returncode == 0 and "Recorded the decision" in done.stdout, f"`build-epic {findings.RECORD_DECISION}` records the decision and says so (exit {done.returncode}: {done.stdout.strip() or done.stderr.strip()})")
decisions = section("Decisions")
report.check(isinstance(decisions, str) and "Sandbox decision" in decisions and "The text of the decision." in decisions, f"the epic's description holds the decision under ## Decisions (found {decisions!r})")
goals = section("Goals")
report.check(isinstance(goals, str) and "Prove build-epic's commands" in goals, f"the decision leaves the description's ## Goals as it was (found {goals!r})")
def closed(report: Report, machine: BuildEpic, store: EpicStore, epic_key: str) -> None:
early = machine.build_epic(epic_key, finish.CLOSE, stdin="Too early.\n")
still = store.closed()
report.check(early.returncode == 1 and " open" in early.stderr and still is False, f"`build-epic {finish.CLOSE}` refuses while issues are open and leaves the epic open (exit {early.returncode}: {early.stderr.strip()}; epic closed: {still!r})")
for issue in read(report, store, "before finishing them"):
done = store.finish_issue(issue.key)
report.check(done is None, f"finish {issue.key}" + (f": {done.reason}" if isinstance(done, Failure) else ""))
late = machine.build_epic(epic_key, finish.CLOSE, stdin="Everything the sandbox epic held is done.\n")
report.check(late.returncode == 0 and "closed it" in late.stdout, f"`build-epic {finish.CLOSE}` posts the summary and closes the epic (exit {late.returncode}: {late.stdout.strip() or late.stderr.strip()})")
now = store.closed()
report.check(now is True, f"Jira counts the epic as done (found {now!r})")
+48
View File
@@ -0,0 +1,48 @@
"""What the e2e command needs to know about its sandbox: three environment variables, so that neither the site nor the credentials' place is in the repo."""
import dataclasses
import pathlib
import re
import typing
from atlassian.client import basic_auth
from failure import Failure
from project_config.settings import SITE
JIRA_URL = "BUILD_EPIC_E2E_JIRA_URL"
ENV_FILE = "BUILD_EPIC_E2E_ENV_FILE"
PROJECT = "BUILD_EPIC_E2E_PROJECT"
PROJECT_KEY = r"[A-Z][A-Z0-9]+"
MEANING = {
JIRA_URL: "the sandbox Jira site's https address without a path, such as https://acme.atlassian.net",
ENV_FILE: "the absolute path of the file holding the sandbox account's ATLASSIAN_EMAIL and ATLASSIAN_API_TOKEN",
PROJECT: "the key of the sandbox project, a team-managed space where the account may create, edit, transition (to Done), comment on and delete issues, such as SBX",
}
@dataclasses.dataclass(frozen=True)
class Sandbox:
jira_url: str
env_file: pathlib.Path
project: str
def read(env: typing.Mapping[str, str]) -> Sandbox | Failure:
"""The sandbox that env names; a Failure naming each setting that is missing or malformed, and why the sandbox's env-file cannot be used."""
values = {name: env.get(name, "").strip() for name in MEANING}
url, project = values[JIRA_URL], values[PROJECT]
env_file = pathlib.Path(values[ENV_FILE]) if values[ENV_FILE] else None
usable = env_file if env_file is not None and env_file.is_absolute() else None
faults = [
*(f"{name} is missing: set it to {meaning}" for name, meaning in MEANING.items() if not values[name]),
*([f"{JIRA_URL} is {url!r}, not an https address without a path"] if url and not re.fullmatch(SITE, url) else []),
*([f"{PROJECT} is {project!r}, not a project key such as SBX"] if project and not re.fullmatch(PROJECT_KEY, project) else []),
*([f"{ENV_FILE} is {values[ENV_FILE]!r}, not an absolute path"] if env_file is not None and usable is None else []),
]
if usable is not None:
login = basic_auth(usable)
if isinstance(login, Failure):
faults.append(login.reason)
if faults:
return Failure("; ".join(faults))
assert env_file is not None
return Sandbox(url, env_file, project)
+93
View File
@@ -0,0 +1,93 @@
import contextlib
import io
import pathlib
import sys
import tempfile
import typing
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
from build_epic.e2e.settings import ENV_FILE, JIRA_URL, PROJECT
from failure import Failure
from build_epic.e2e import main, sandbox, scenario, settings
class MainTest(unittest.TestCase):
def run_main(self, argv: list[str]) -> tuple[int, str, str]:
out, err = io.StringIO(), io.StringIO()
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
code = main.main(argv, {}, pathlib.Path("build-epic"), sandbox.epic_made, scenario.run)
return code, out.getvalue(), err.getvalue()
def test_fails_naming_each_missing_setting_before_it_touches_anything(self) -> None:
code, out, err = self.run_main([])
self.assertEqual((code, out), (1, ""))
for name in (JIRA_URL, ENV_FILE, PROJECT):
self.assertIn(name, err)
def test_prints_its_help_for_help_and_refuses_other_words(self) -> None:
code, out, err = self.run_main(["--help"])
self.assertEqual((code, err), (0, ""))
self.assertIn(JIRA_URL, out)
self.assertEqual(self.run_main(["-h"])[:2], (0, out))
code, out, err = self.run_main(["--status"])
self.assertEqual((code, out), (1, ""))
self.assertIn(main.HELP, err)
class ReportedTest(unittest.TestCase):
def setUp(self) -> None:
made = tempfile.TemporaryDirectory()
self.addCleanup(made.cleanup)
env_file = pathlib.Path(made.name) / "jira.env"
env_file.write_text("ATLASSIAN_EMAIL=a@example.com\nATLASSIAN_API_TOKEN=secret\n", encoding="utf-8")
env_file.chmod(0o600)
self.env = {JIRA_URL: "https://acme.atlassian.net", ENV_FILE: str(env_file), PROJECT: "SBX"}
def run_scenario(self, deleted: bool, report: scenario.Report | None = None, made: str | Failure = "SBX-1") -> tuple[int, str, str]:
@contextlib.contextmanager
def epic_made(*args: object) -> typing.Iterator[str | Failure]:
self.made_with = args
yield made
if not deleted:
raise sandbox.Leftover("SBX-1 is still there")
out, err = io.StringIO(), io.StringIO()
report = report or scenario.Report(["a check held"], ["a check broke"])
self.ran_with = None
def ran(*args: object) -> scenario.Report:
self.ran_with = args
return report
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
code = main.main([], self.env, pathlib.Path("build-epic"), epic_made, ran)
return code, out.getvalue(), err.getvalue()
def test_prints_each_check_and_fails_where_one_broke(self) -> None:
code, out, err = self.run_scenario(True)
self.assertEqual((code, err), (1, ""))
self.assertIn("ok a check held\nFAIL a check broke\n1 held, 1 broke", out)
def test_prints_the_checks_and_names_the_epic_left_behind_when_it_cannot_be_deleted(self) -> None:
code, out, err = self.run_scenario(False)
self.assertEqual(code, 1)
self.assertIn("ok a check held", out)
self.assertIn("SBX-1 is still there", err)
def test_makes_the_epic_in_the_sandbox_and_runs_the_scenario_on_it(self) -> None:
code, out, err = self.run_scenario(True, scenario.Report(["a check held"]))
sandbox_set = settings.read(self.env)
self.assertEqual((code, err), (0, ""))
self.assertEqual(self.made_with, (sandbox_set, "build-epic e2e sandbox epic"))
self.assertEqual(self.ran_with, (sandbox_set, "SBX-1", pathlib.Path("build-epic"), self.env))
self.assertEqual(out, "Sandbox epic SBX-1 in https://acme.atlassian.net\nok a check held\n1 held, 0 broke\n")
def test_fails_without_a_scenario_where_the_epic_cannot_be_made(self) -> None:
code, out, err = self.run_scenario(True, made=Failure("Jira said no"))
self.assertEqual((code, out, self.ran_with), (1, "", None))
self.assertEqual(err, "build-epic-e2e: Jira said no\n")
def test_fails_where_every_check_held_but_the_epic_stays(self) -> None:
self.assertEqual(self.run_scenario(False, scenario.Report(["a check held"]))[0], 1)
+113
View File
@@ -0,0 +1,113 @@
import base64
import pathlib
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
from build_epic.e2e.sandbox import epic_made, Leftover
from failure import Failure
from build_epic.e2e.settings import Sandbox
class JiraStub:
"""Records each request, and answers as Jira does: a key for a new issue, the epic's children for a search."""
def __init__(self, refuses: str | None = None) -> None:
self.requests: list[tuple[str, str]] = []
self.authorizations: set[str] = set()
self.bodies: list[object] = []
self.refuses = refuses
self.search_answer: object = {"issues": [{"key": "SBX-2"}, {"key": "SBX-3"}]}
self.later_pages: list[object] = []
def __call__(self, method: str, url: str, authorization: str, body: object) -> object:
self.requests.append((method, url.split("/rest/api/3/")[1]))
self.authorizations.add(authorization)
self.bodies.append(body)
if self.refuses and url.endswith(self.refuses):
return Failure(f"{method} {url} answered 403")
if method == "POST" and url.endswith("/issue"):
return {"key": "SBX-1"}
if method == "POST":
return self.later_pages.pop(0) if self.later_pages and isinstance(body, dict) and "nextPageToken" in body else self.search_answer
return None
class EpicMadeTest(unittest.TestCase):
def setUp(self) -> None:
made = tempfile.TemporaryDirectory()
self.addCleanup(made.cleanup)
env_file = pathlib.Path(made.name) / "jira.env"
env_file.write_text("ATLASSIAN_EMAIL=a@example.com\nATLASSIAN_API_TOKEN=secret\n", encoding="utf-8")
env_file.chmod(0o600)
self.sandbox = Sandbox("https://acme.atlassian.net", env_file, "SBX")
def test_deletes_each_child_issue_and_then_the_epic_when_the_block_ends(self) -> None:
jira = JiraStub()
with epic_made(self.sandbox, "An epic", jira, lambda markdown: {"type": "doc"}) as key:
self.assertEqual(key, "SBX-1")
self.assertEqual(jira.authorizations, {"Basic " + base64.b64encode(b"a@example.com:secret").decode()})
self.assertEqual(jira.bodies[1], {"jql": "parent = SBX-1", "fields": ["summary"], "maxResults": 100})
self.assertEqual(jira.requests[1:], [
("POST", "search/jql"),
("DELETE", "issue/SBX-2?deleteSubtasks=true"),
("DELETE", "issue/SBX-3?deleteSubtasks=true"),
("DELETE", "issue/SBX-1?deleteSubtasks=true"),
])
def test_deletes_the_children_on_every_page_of_the_search(self) -> None:
jira = JiraStub()
jira.search_answer = {"issues": [{"key": "SBX-2"}], "nextPageToken": "more"}
jira.later_pages = [{"issues": [{"key": "SBX-3"}]}]
with epic_made(self.sandbox, "An epic", jira, lambda markdown: {"type": "doc"}):
pass
self.assertEqual(jira.bodies[2], {"jql": "parent = SBX-1", "fields": ["summary"], "maxResults": 100, "nextPageToken": "more"})
self.assertEqual([url for method, url in jira.requests if method == "DELETE"], [f"issue/SBX-{n}?deleteSubtasks=true" for n in (2, 3, 1)])
def test_deletes_the_rest_and_says_so_when_a_found_issue_has_no_key(self) -> None:
jira = JiraStub()
jira.search_answer = {"issues": [{"key": "SBX-2"}, {"summary": "no key"}]}
with self.assertRaises(Leftover) as left:
with epic_made(self.sandbox, "An epic", jira, lambda markdown: {"type": "doc"}):
pass
self.assertIn("without a key", str(left.exception))
self.assertEqual([url for method, url in jira.requests if method == "DELETE"], ["issue/SBX-2?deleteSubtasks=true", "issue/SBX-1?deleteSubtasks=true"])
def test_deletes_them_when_the_block_raises(self) -> None:
jira = JiraStub()
with self.assertRaises(ZeroDivisionError):
with epic_made(self.sandbox, "An epic", jira, lambda markdown: {"type": "doc"}):
1 / 0
self.assertEqual(jira.requests[-1], ("DELETE", "issue/SBX-1?deleteSubtasks=true"))
def test_names_the_epic_left_behind_when_a_delete_is_refused(self) -> None:
with self.assertRaises(Leftover) as left:
with epic_made(self.sandbox, "An epic", JiraStub(refuses="SBX-3?deleteSubtasks=true"), lambda markdown: {"type": "doc"}):
pass
self.assertIn("SBX-1", str(left.exception))
def test_deletes_the_epic_all_the_same_and_says_so_when_the_search_for_its_issues_answers_nothing_usable(self) -> None:
jira = JiraStub()
jira.search_answer = {"errorMessages": ["no"]}
with self.assertRaises(Leftover) as left:
with epic_made(self.sandbox, "An epic", jira, lambda markdown: {"type": "doc"}):
pass
self.assertIn("something other than a page of issues", str(left.exception))
self.assertEqual(jira.requests[1:], [("POST", "search/jql"), ("DELETE", "issue/SBX-1?deleteSubtasks=true")])
def test_deletes_the_epic_all_the_same_and_gives_the_reason_when_the_search_for_its_issues_is_refused(self) -> None:
jira = JiraStub(refuses="search/jql")
with self.assertRaises(Leftover) as left:
with epic_made(self.sandbox, "An epic", jira, lambda markdown: {"type": "doc"}):
pass
self.assertIn("POST https://acme.atlassian.net/rest/api/3/search/jql answered 403", str(left.exception))
self.assertNotIn("something other than a page of issues", str(left.exception))
self.assertEqual(jira.requests[1:], [("POST", "search/jql"), ("DELETE", "issue/SBX-1?deleteSubtasks=true")])
def test_yields_a_failure_and_deletes_nothing_when_the_epic_cannot_be_made(self) -> None:
jira = JiraStub(refuses="/issue")
with epic_made(self.sandbox, "An epic", jira, lambda markdown: {"type": "doc"}) as key:
self.assertIsInstance(key, Failure)
self.assertEqual(jira.requests, [("POST", "issue")])
+275
View File
@@ -0,0 +1,275 @@
import dataclasses
import pathlib
import subprocess
import sys
import tempfile
import typing
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
from build_epic.repo_pick import CLAUDE_LABEL
from failure import Failure
from build_epic import findings, finish
from build_epic.config import load
from build_epic.backend.fake import MemoryEpic
from build_epic.e2e.settings import Sandbox
from build_epic.e2e import scenario
EPIC = "SBX-1"
def done(code: int, out: str = "", err: str = "") -> subprocess.CompletedProcess[str]:
return subprocess.CompletedProcess([], code, out, err)
@dataclasses.dataclass
class Scripted:
"""A build-epic that answers each call with the next of its answers, each a function that may also change the epic as build-epic would, and notes the call."""
answers: list[typing.Callable[[], subprocess.CompletedProcess[str]]]
calls: list[tuple[tuple[str, ...], str]] = dataclasses.field(default_factory=list)
def build_epic(self, *args: str, stdin: str = "") -> subprocess.CompletedProcess[str]:
self.calls.append((args, stdin))
return self.answers.pop(0)()
class ReportTest(unittest.TestCase):
def test_sorts_each_check_into_what_held_and_what_broke(self) -> None:
report = scenario.Report()
report.check(True, "one")
report.check(False, "two")
self.assertEqual((report.held, report.broke), (["one"], ["two"]))
class ItemsFiledTest(unittest.TestCase):
def test_holds_where_the_epic_answers_the_issues_in_order(self) -> None:
report = scenario.Report()
scenario.items_filed(report, MemoryEpic(EPIC), EPIC)
self.assertEqual(report.broke, [])
self.assertEqual(len(report.held), 4)
def test_breaks_where_the_issues_come_back_in_another_order(self) -> None:
class Reversed(MemoryEpic):
def read(self) -> list[typing.Any] | Failure:
found = super().read()
return found if isinstance(found, Failure) else found[::-1]
report = scenario.Report()
scenario.items_filed(report, Reversed(EPIC), EPIC)
self.assertEqual(len(report.held), 3)
self.assertEqual(len(report.broke), 1)
self.assertIn("read the filed issues back in order", report.broke[0])
def test_breaks_naming_why_where_the_epic_refuses_every_call(self) -> None:
report = scenario.Report()
scenario.items_filed(report, MemoryEpic(EPIC, fails="Jira is down"), EPIC)
self.assertEqual(report.held, [])
self.assertEqual(report.broke[:3], [f"file {what} in the epic: Jira is down" for what in ("an item", "a second item", "a sub-task")])
self.assertEqual(report.broke[3], "read the epic's issues after filing: Jira is down")
self.assertIn("found []", report.broke[4])
class FindingFiledTest(unittest.TestCase):
def filing(self, store: MemoryEpic, labels: tuple[str, ...] = (CLAUDE_LABEL,), code: int = 0, out: str = "10:00 Filed SBX-100") -> typing.Callable[[], subprocess.CompletedProcess[str]]:
def answer() -> subprocess.CompletedProcess[str]:
store.file(EPIC, "Sandbox finding", "Why the finding matters.", labels)
return done(code, out)
return answer
def test_holds_where_build_epic_files_the_finding_with_its_label_and_detail(self) -> None:
store = MemoryEpic(EPIC)
machine = Scripted([self.filing(store)])
report = scenario.Report()
scenario.finding_filed(report, machine, store, EPIC)
self.assertEqual((report.broke, len(report.held)), ([], 2))
self.assertEqual(machine.calls, [((EPIC, findings.FILE, "Sandbox finding", findings.LABEL, CLAUDE_LABEL), "Why the finding matters.\n")])
def test_breaks_where_build_epic_fails_and_files_nothing(self) -> None:
report = scenario.Report()
scenario.finding_filed(report, Scripted([lambda: done(1, err="no")]), MemoryEpic(EPIC), EPIC)
self.assertEqual(report.held, [])
self.assertIn("exit 1: no", report.broke[0])
self.assertIn("found []", report.broke[1])
def test_breaks_where_the_finding_lacks_the_label_asked_for(self) -> None:
store = MemoryEpic(EPIC)
report = scenario.Report()
scenario.finding_filed(report, Scripted([self.filing(store, labels=())]), store, EPIC)
self.assertEqual(len(report.held), 1)
self.assertEqual(len(report.broke), 1)
self.assertIn("with its label", report.broke[0])
def test_breaks_where_build_epic_exits_cleanly_but_says_nothing_of_filing(self) -> None:
store = MemoryEpic(EPIC)
report = scenario.Report()
scenario.finding_filed(report, Scripted([self.filing(store, out="")]), store, EPIC)
self.assertEqual(len(report.held), 1)
self.assertIn("files the finding and says so", report.broke[0])
class FollowUpFiledTest(unittest.TestCase):
def store(self) -> MemoryEpic:
store = MemoryEpic(EPIC)
store.file(EPIC, "Sandbox finding", "", (CLAUDE_LABEL,))
return store
def following(self, store: MemoryEpic, flipped: bool = False) -> typing.Callable[[], subprocess.CompletedProcess[str]]:
def answer() -> subprocess.CompletedProcess[str]:
key = store.file(EPIC, "Sandbox follow-up finding", "", (CLAUDE_LABEL,), None if flipped else "SBX-100")
if flipped:
store.change("SBX-100", blockers=(str(key),))
return done(0, "Filed SBX-101 in the epic SBX-1. It is blocked by SBX-100.")
return answer
def test_holds_where_the_follow_up_is_blocked_by_the_first_finding(self) -> None:
store = self.store()
machine = Scripted([self.following(store)])
report = scenario.Report()
scenario.follow_up_filed(report, machine, store, EPIC)
self.assertEqual((report.broke, len(report.held)), ([], 2))
self.assertEqual(machine.calls, [((EPIC, findings.FILE, "Sandbox follow-up finding", findings.LABEL, CLAUDE_LABEL, findings.BLOCKED_BY, "SBX-100"), "Waits for the first finding.\n")])
def test_breaks_where_the_link_points_the_wrong_way(self) -> None:
store = self.store()
report = scenario.Report()
scenario.follow_up_filed(report, Scripted([self.following(store, flipped=True)]), store, EPIC)
self.assertEqual(len(report.broke), 1)
self.assertIn("not the other way round", report.broke[0])
def test_breaks_where_build_epic_exits_cleanly_but_says_nothing_of_the_blocker(self) -> None:
store = self.store()
report = scenario.Report()
scenario.follow_up_filed(report, Scripted([lambda: (self.following(store)(), done(0, "Filed SBX-101 in the epic SBX-1."))[1]]), store, EPIC)
self.assertEqual(len(report.broke), 1)
self.assertIn("says it is blocked", report.broke[0])
def test_breaks_where_build_epic_fails_and_files_nothing(self) -> None:
report = scenario.Report()
scenario.follow_up_filed(report, Scripted([lambda: done(1, err="no")]), self.store(), EPIC)
self.assertEqual(len(report.broke), 2)
self.assertIn("exit 1: no", report.broke[0])
class DecisionRecordedTest(unittest.TestCase):
SECTIONS: typing.ClassVar[dict[str, str | Failure]] = {"Decisions": "### Sandbox decision\nThe text of the decision.", "Goals": "1. Prove build-epic's commands work."}
def recorded(self, answer: subprocess.CompletedProcess[str], **sections: str | Failure) -> tuple[scenario.Report, Scripted]:
machine = Scripted([lambda: answer])
report = scenario.Report()
found = {**self.SECTIONS, **sections}
scenario.decision_recorded(report, machine, lambda name: found[name], EPIC)
return report, machine
def test_holds_where_the_decision_is_in_the_description_and_the_goals_are_as_they_were(self) -> None:
report, machine = self.recorded(done(0, "10:00 Recorded the decision"))
self.assertEqual((report.broke, len(report.held)), ([], 3))
self.assertEqual(machine.calls, [((EPIC, findings.RECORD_DECISION, "Sandbox decision"), "The text of the decision.\n")])
def test_breaks_where_the_decision_is_not_recorded(self) -> None:
report, _ = self.recorded(done(1, err="no"), Decisions="")
self.assertEqual(len(report.held), 1)
self.assertIn("exit 1: no", report.broke[0])
self.assertIn("holds the decision under ## Decisions", report.broke[1])
def test_breaks_where_only_half_of_the_decision_is_recorded(self) -> None:
report, _ = self.recorded(done(0, "Recorded the decision"), Decisions="### Sandbox decision")
self.assertEqual(len(report.broke), 1)
def test_breaks_where_the_goals_changed(self) -> None:
report, _ = self.recorded(done(0, "Recorded the decision"), Goals="")
self.assertEqual(len(report.broke), 1)
self.assertIn("leaves the description's ## Goals", report.broke[0])
def test_breaks_without_raising_where_the_description_cannot_be_read(self) -> None:
report, _ = self.recorded(done(0, "Recorded the decision"), Decisions=Failure("Jira is down"), Goals=Failure("Jira is down"))
self.assertEqual((len(report.held), len(report.broke)), (1, 2))
class ClosedTest(unittest.TestCase):
def closing(self, store: MemoryEpic, early: typing.Callable[[], subprocess.CompletedProcess[str]] | None = None, late: typing.Callable[[], subprocess.CompletedProcess[str]] | None = None) -> scenario.Report:
def closes() -> subprocess.CompletedProcess[str]:
store.close("Everything the sandbox epic held is done.")
return done(0, "10:00 Posted the summary and closed it")
store.file(EPIC, "An open issue", "", ())
report = scenario.Report()
self.machine = Scripted([early or (lambda: done(1, err="SBX-100 is still open")), late or closes])
scenario.closed(report, self.machine, store, EPIC)
return report
def test_holds_where_build_epic_refuses_while_an_issue_is_open_and_closes_the_epic_after(self) -> None:
store = MemoryEpic(EPIC)
report = self.closing(store)
self.assertEqual((report.broke, len(report.held)), ([], 4))
self.assertEqual(store.comments[EPIC], ["Everything the sandbox epic held is done."])
self.assertEqual(self.machine.calls, [((EPIC, finish.CLOSE), "Too early.\n"), ((EPIC, finish.CLOSE), "Everything the sandbox epic held is done.\n")])
self.assertTrue(all(issue.done for issue in store.issues))
def test_breaks_where_the_early_close_closes_the_epic(self) -> None:
store = MemoryEpic(EPIC)
def closes_early() -> subprocess.CompletedProcess[str]:
store.close("Too early.")
return done(1, err="SBX-100 is still open")
report = self.closing(store, early=closes_early)
self.assertEqual(len(report.broke), 1)
self.assertIn("refuses while issues are open", report.broke[0])
def test_breaks_where_the_late_close_exits_cleanly_but_leaves_the_epic_open(self) -> None:
report = self.closing(MemoryEpic(EPIC), late=lambda: done(0, "Posted the summary and closed it"))
self.assertEqual(len(report.broke), 1)
self.assertIn("Jira counts the epic as done (found False)", report.broke[0])
def test_breaks_where_the_late_close_fails(self) -> None:
report = self.closing(MemoryEpic(EPIC), late=lambda: done(1, err="refused"))
self.assertIn("exit 1: refused", report.broke[0])
def test_breaks_without_raising_where_the_epic_cannot_be_read(self) -> None:
report = self.closing(MemoryEpic(EPIC, fails="Jira is down"))
self.assertEqual(len(report.held), 1)
self.assertIn("read the epic's issues before finishing them: Jira is down", report.broke)
self.assertIn("Jira counts the epic as done (found Failure(reason='Jira is down'))", report.broke)
class MachineTest(unittest.TestCase):
def setUp(self) -> None:
made = tempfile.TemporaryDirectory()
self.addCleanup(made.cleanup)
self.folder = pathlib.Path(made.name).resolve()
self.env_file = self.folder / "jira.env"
self.env_file.write_text("ATLASSIAN_EMAIL=a@example.com\nATLASSIAN_API_TOKEN=secret\n", encoding="utf-8")
self.env_file.chmod(0o600)
self.sandbox = Sandbox("https://acme.atlassian.net", self.env_file, "SBX")
def script(self, body: str) -> pathlib.Path:
launcher = self.folder / "launcher"
launcher.write_text(f"#!/bin/sh\n{body}\n", encoding="utf-8")
launcher.chmod(0o755)
return launcher
def test_writes_settings_that_build_epic_loads_and_that_point_at_the_sandbox(self) -> None:
home = self.folder / "home"
machine = scenario.machine_made(home, self.sandbox, pathlib.Path("build-epic"), {"A": "b"})
assert not isinstance(machine, Failure), machine
self.assertEqual((machine.home, machine.launcher, machine.env), (home, pathlib.Path("build-epic"), {"A": "b"}))
config = load(home, {})
assert not isinstance(config, Failure), config
self.assertEqual((config.jira_url, config.env_file, config.repo_rule), ("https://acme.atlassian.net", self.env_file, scenario.RULE))
self.assertEqual((config.finish.transition, config.finish.status) if config.finish else None, ("Done", "Done"))
def test_runs_build_epic_in_the_given_environment_with_the_machines_home_and_the_words_and_stdin_given(self) -> None:
machine = scenario.Machine(self.folder, self.script('echo "$HOME $KEPT $*"; cat'), {"KEPT": "yes", "HOME": "/elsewhere"})
finished = machine.build_epic("ABC-1", "--file", stdin="detail")
self.assertEqual((finished.returncode, finished.stdout), (0, f"{self.folder} yes ABC-1 --file\ndetail"))
def test_answers_a_failing_build_epic_with_its_exit_and_output(self) -> None:
finished = scenario.Machine(self.folder, self.script("echo out; echo err >&2; exit 3"), {}).build_epic("ABC-1")
self.assertEqual((finished.returncode, finished.stdout, finished.stderr), (3, "out\n", "err\n"))
def test_stops_a_build_epic_that_runs_over_its_time_and_says_so(self) -> None:
finished = scenario.Machine(self.folder, self.script("exec sleep 30"), {}, timeout=0.2).build_epic("ABC-1")
self.assertEqual(finished.returncode, 124)
self.assertIn("ran over 0.2 seconds", finished.stderr)
self.assertIn("ABC-1", finished.stderr)
+54
View File
@@ -0,0 +1,54 @@
import pathlib
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
from build_epic.e2e.settings import ENV_FILE, JIRA_URL, PROJECT, read, Sandbox
from failure import Failure
class SettingsTest(unittest.TestCase):
def setUp(self) -> None:
made = tempfile.TemporaryDirectory()
self.addCleanup(made.cleanup)
self.env_file = pathlib.Path(made.name) / "jira.env"
self.env_file.write_text("ATLASSIAN_EMAIL=a@example.com\nATLASSIAN_API_TOKEN=secret\n", encoding="utf-8")
self.env_file.chmod(0o600)
self.env = {JIRA_URL: "https://acme.atlassian.net", ENV_FILE: str(self.env_file), PROJECT: "SBX"}
def failure(self, env: dict[str, str]) -> str:
found = read(env)
assert isinstance(found, Failure), found
return found.reason
def test_reads_the_site_the_env_file_and_the_project(self) -> None:
self.assertEqual(read(self.env), Sandbox("https://acme.atlassian.net", self.env_file, "SBX"))
def test_names_each_setting_that_is_missing(self) -> None:
reason = self.failure({})
for name in (JIRA_URL, ENV_FILE, PROJECT):
self.assertIn(name, reason)
self.assertNotIn(PROJECT, self.failure({k: v for k, v in self.env.items() if k != JIRA_URL}))
def test_treats_a_blank_setting_as_missing(self) -> None:
self.assertIn(f"{ENV_FILE} is missing", self.failure({**self.env, ENV_FILE: " "}))
def test_names_every_fault_at_once_separated_by_semicolons(self) -> None:
self.env_file.chmod(0o644)
reason = self.failure({**self.env, PROJECT: "sbx-1"})
self.assertTrue(reason.startswith(f"{PROJECT} is"))
self.assertIn(f"; {self.env_file} ", reason)
def test_refuses_a_site_that_is_not_an_https_address_without_a_path(self) -> None:
self.assertIn(JIRA_URL, self.failure({**self.env, JIRA_URL: "http://acme.atlassian.net/jira"}))
def test_refuses_an_env_file_that_cannot_be_read_naming_the_file(self) -> None:
gone = self.env_file.parent / "gone.env"
self.assertIn(str(gone), self.failure({**self.env, ENV_FILE: str(gone)}))
self.env_file.chmod(0o644)
self.assertIn(str(self.env_file), self.failure(self.env))
def test_refuses_an_env_file_that_is_not_an_absolute_path(self) -> None:
self.assertIn(ENV_FILE, self.failure({**self.env, ENV_FILE: "jira.env"}))