diff --git a/.gitignore b/.gitignore index 9f5b4fa9..e19d954b 100644 --- a/.gitignore +++ b/.gitignore @@ -57,10 +57,13 @@ !bin/deploy_release/ !bin/deploy_release/*.py !bin/do-epic +!bin/do-epic-e2e !bin/do_epic/ !bin/do_epic/*.py !bin/do_epic/backend/ !bin/do_epic/backend/*.py +!bin/do_epic/e2e/ +!bin/do_epic/e2e/*.py !bin/failure.py !bin/forge/ !bin/forge/*.py diff --git a/README.md b/README.md index 48979d0b..98081c9d 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,7 @@ Install puts only `loop-claude` on `PATH`; call the others by their path, such a - `compress-archives` gzips each transcript in loop-claude's archive and each file in the review folders 30 days after it last changed, keeping all of them; Install's crontab line runs it daily. - `deploy-release [--cut] []` moves `~/.claude`, or the checkout named, to the newest release, as Update says; with `--cut`, run in the development clone, it first cuts the next release, as Release says. - `do-epic ` runs the chunks of one Jira epic whose items span repos, or have none, each in its own window of the tmux session `epic-`, on the same cycle, judge and interrupt as loop-claude. The epic's description is its record: goals, audience, scoring and decisions. An open issue runs once a label names its repo, in Jira's backlog order, as many at once as a line such as `**Max parallel chunks:** 2` in the epic's description allows, 1 without it, and as each repo's `parallel-chunks` allows. A chunk whose pull request waits on a gate parks. do-epic closes the epic only once you confirm every goal. Install → "Set up a project" says how to set it up; `do-epic --help` states the flags and the contract. +- `do-epic-e2e` drives `do-epic`'s `--file`, `--record-decision` and `--close-epic` against an epic it makes in a sandbox project on a real Jira site and deletes when it ends, as Test says. It isn't part of `run-tests`. - `judge-replay [--offline]` replays loop-claude's judge on archived verdicts of the projects run on the model. It prints what each rung a review climbs to misses, and the lowest rung below those that returns every verdict, as each candidate above it does. With `--offline` it doesn't call a judge: it counts what a "?" rule, a decision table and a classifier trained on the archive decide of each verdict's kind from the worker's final message, and how many interrupts each misses. `judge-replay --help` states both replays. - `larv-review ` holds larv-review's mechanical rules: the chunk's review folder, the paired ruling's base and vote count, each pass's round count and the opening of its round commit's title, the scoring run's trigger and setup; the comprehension history's format, its latest places and its count of how often seats agree; and each reviewer's prompt header. `larv-review --help` states them. `settings.json` → `hooks` runs `larv-review reviewer-context` as each subagent starts. A reviewer under `agents/` whose frontmatter has `omitClaudeMd: true` starts without `CLAUDE.md`, and the command hands it that file's "Shared goal", "Prose" and "Vocabulary" sections. - `loop-claude ` runs a project's next chunks, each in its own tmux window, cycle after cycle; `loop-claude --help` states its contract. @@ -224,7 +225,7 @@ Install puts only `loop-claude` on `PATH`; call the others by their path, such a - `branch_chunks/` cuts a default branch into chunks and finds, for each, the items it shipped and the later chunks that fixed its code, for `score_outcomes` and `routing_figures`. It imports `reading_cost`'s record path. - `command_lines/` reads a shell command: its simple commands, and a docker call's subcommand and memory limits, for `command_patterns`, `reading_cost`, `routing_figures`, `rule_hook` and `run_heavy`. - `deploy_release` imports `release_plan`'s `release_standing`. -- `do_epic/` holds `do-epic`, the tool that drives one Jira epic. `main.py` starts the epic's chunks and runs one chunk's cycles in its window, through `agent_cycle`. `scheduler.py` says which issues run now: as many as the epic's cap and each repo's `parallel-chunks` allow, and none in a repo where loop-claude runs chunks. `gates.py` reads once whether a chunk's pull request waits on its checks, an approval or a new review, and `parked.py` keeps each chunk that does in a state file, so it takes no running slot while it waits and goes on waiting after a reboot. `chunk_prompt.py` words what a chunk's worker reads. Through `findings.py` that worker files a finding as an issue of the epic, with `--file`, and appends a decision to its description, with `--record-decision`. `finish.py` ends an epic that holds no open issue: its finishing chunk files a release review of each repo the epic changed, has the human confirm each goal, and with `--close-epic` posts the summary on the epic and closes it. `config.py` reads its machine-wide settings, importing `project_config`'s settings and `model_routing`'s ladder file, and `epic_record.py` reads and writes an epic's description by section, language line and cap line, through `record_store`'s `EpicDescription`. `repos.py` finds a repo's checkout and project config by its name, and clones a missing checkout from the host and owner of the epic's other repos, through `forge`'s `url_beside`. `repo_pick.py` asks claude, by `repo-rule.md`, where an epic item is worked, and records the answer as a label on the issue. `split.py` has claude predict from the code which repos an item changes, and files a sub-task per repo under it where there are several. `ranking.py` orders an epic's items by score over one chain of goals, the epic's, then code health, then the item's repo's, through `plan`'s ranking, and re-ranks the tracker's backlog to that order through `backend/`. `backend/` reads, files and ranks the epic's issues, adds the labels `repo_pick.py` and `split.py` record, posts comments, moves a finished chunk's issue to done, and closes the epic. +- `do_epic/` holds `do-epic`, the tool that drives one Jira epic. `main.py` starts the epic's chunks and runs one chunk's cycles in its window, through `agent_cycle`. `scheduler.py` says which issues run now: as many as the epic's cap and each repo's `parallel-chunks` allow, and none in a repo where loop-claude runs chunks. `gates.py` reads once whether a chunk's pull request waits on its checks, an approval or a new review, and `parked.py` keeps each chunk that does in a state file, so it takes no running slot while it waits and goes on waiting after a reboot. `chunk_prompt.py` words what a chunk's worker reads. Through `findings.py` that worker files a finding as an issue of the epic, with `--file`, and appends a decision to its description, with `--record-decision`. `finish.py` ends an epic that holds no open issue: its finishing chunk files a release review of each repo the epic changed, has the human confirm each goal, and with `--close-epic` posts the summary on the epic and closes it. `config.py` reads its machine-wide settings, importing `project_config`'s settings and `model_routing`'s ladder file, and `epic_record.py` reads and writes an epic's description by section, language line and cap line, through `record_store`'s `EpicDescription`. `repos.py` finds a repo's checkout and project config by its name, and clones a missing checkout from the host and owner of the epic's other repos, through `forge`'s `url_beside`. `repo_pick.py` asks claude, by `repo-rule.md`, where an epic item is worked, and records the answer as a label on the issue. `split.py` has claude predict from the code which repos an item changes, and files a sub-task per repo under it where there are several. `ranking.py` orders an epic's items by score over one chain of goals, the epic's, then code health, then the item's repo's, through `plan`'s ranking, and re-ranks the tracker's backlog to that order through `backend/`. `backend/` reads, files and ranks the epic's issues, adds the labels `repo_pick.py` and `split.py` record, posts comments, moves a finished chunk's issue to done, and closes the epic. `e2e/` holds `do-epic-e2e`. - `forge/` tells a GitHub origin from a Gitea one, names a repo beside an origin's and runs that forge's CLI (`commands.py`), and finds a pull request and reads its checks, once or until they settle (`pulls.py`), for `chunk_preflight`, `chunk_pr`, `deploy_release`, `do_epic` and `larv_review`. - `git_cli/` runs git for every tool (`git_repo.py`), and reads the git calls in a shell command (`git_command.py`). - `judge_replay` imports `agent_cycle`'s judge and verdicts, and `loop_claude`'s premises, records and `ANSWER_DESTINATIONS`. @@ -252,6 +253,8 @@ Install puts only `loop-claude` on `PATH`; call the others by their path, such a Test on the host with `bin/run-tests`, which runs pytest through `uvx`, pinned in `bin/run_tests/main.py`. It prints each failing test with its error and pytest's summary line, and saves the whole output to a file whose path it prints. It passes on any test paths or pytest options it is given, such as `bin/run-tests bin/chunk_pr -x`. It needs `uv` on `PATH`, which fetches pytest, pytest-xdist and mypy, a Docker daemon, Linux, and a user other than root; a test that lacks one fails, naming it. pytest-xdist runs the tests in one worker per CPU of a heavy slot (`docs/decisions.md` → `bin/: the tools support only the latest stable Python, …`). `python3 -m unittest discover -s bin -p 'test_*.py'` runs the same tests one at a time, with the same needs. Test the bench's scripts with `python3 -m unittest discover -s workflow-bench -p 'test_*.py'`. +`bin/do-epic-e2e` checks `do-epic` against a real Jira site, which `run-tests` never reaches. Set `DO_EPIC_E2E_JIRA_URL` to the sandbox site's address, `DO_EPIC_E2E_PROJECT` to its project's key and `DO_EPIC_E2E_ENV_FILE` to the absolute path of the file holding the sandbox account's `ATLASSIAN_EMAIL` and `ATLASSIAN_API_TOKEN`. The account must be able to create, transition, comment on and delete issues there. It fails, naming each setting that is missing; it makes an epic with issues, runs `do-epic` against it in a home folder of its own, and deletes the epic and its issues when it ends. It needs a Docker daemon, as `run-tests` does. + CI runs `ci.sh` on every pull request, and `main` doesn't merge anything until it passes. The script runs the same tests on the committed HEAD in a pinned Python container. Run it under `run-heavy` to reproduce a CI failure, with Docker on `PATH` and its socket at `/var/run/docker.sock`, which the container shares. To mutation-test the lines a branch changes, run `~/.claude/bin/run-heavy ~/.claude/bin/mutate-changes --prefix 'uvx --python "$(command -v python3)" --with pytest==9.2.0 --from "mutmut>=3,<4"'` in its checkout. diff --git a/bin/do-epic-e2e b/bin/do-epic-e2e new file mode 100755 index 00000000..01856292 --- /dev/null +++ b/bin/do-epic-e2e @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 +import os +import pathlib +import sys + +LAUNCHER = pathlib.Path(__file__).resolve() +sys.path.insert(0, str(LAUNCHER.parent)) + +from do_epic.e2e import main + +sys.exit(main.main(sys.argv[1:], os.environ, LAUNCHER.parent / "do-epic")) diff --git a/bin/do_epic/e2e/__init__.py b/bin/do_epic/e2e/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bin/do_epic/e2e/main.py b/bin/do_epic/e2e/main.py new file mode 100644 index 00000000..f6c58e43 --- /dev/null +++ b/bin/do_epic/e2e/main.py @@ -0,0 +1,40 @@ +"""The do-epic-e2e command: runs do-epic's commands that need no tmux session and no worker against a sandbox epic on a real Jira site (docs/decisions.md → `do-epic: drives one Jira epic …`). It is no part of `run-tests`.""" +import pathlib +import sys +import typing + +from do_epic.e2e import sandbox, scenario, settings +from failure import Failure + +HELP = f"""usage: do-epic-e2e + +Makes an epic in the Jira sandbox, with issues and sub-tasks, drives do-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.""" + + +def main(argv: list[str], env: typing.Mapping[str, str], launcher: pathlib.Path) -> int: + 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"do-epic-e2e cannot start: {found.reason}", file=sys.stderr) + return 1 + try: + with sandbox.epic_made(found, "do-epic e2e sandbox epic") as key: + if isinstance(key, Failure): + print(f"do-epic-e2e: {key.reason}", file=sys.stderr) + return 1 + print(f"Sandbox epic {key} in {found.jira_url}") + report = scenario.run(found, key, launcher) + except sandbox.Leftover as left: + print(f"do-epic-e2e: {left}", file=sys.stderr) + return 1 + 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; the sandbox epic is deleted") + return 1 if report.broke else 0 diff --git a/bin/do_epic/e2e/sandbox.py b/bin/do_epic/e2e/sandbox.py new file mode 100644 index 00000000..0eddfb72 --- /dev/null +++ b/bin/do_epic/e2e/sandbox.py @@ -0,0 +1,74 @@ +"""The sandbox epic the e2e command makes on the real Jira site and deletes when it ends.""" +import contextlib +import typing + +from adf_codec.convert import markdown_to_adf +from atlassian.client import basic_auth, Http, jira_json, ToAdf +from do_epic.e2e.settings import Sandbox +from failure import Failure + +EPIC_TYPE = "Epic" +# How a team-managed project's workflow moves an issue to done, as do-epic's config names it. +FINISH_TRANSITION = "Done" +FINISH_STATUS = "Done" +DESCRIPTION = """**Language:** English + +## Goals + +1. **Achieve:** Prove do-epic's commands against a real Jira site. + +## Audience + +The person who runs do-epic's e2e command. +""" + + +class Leftover(Exception): + """The sandbox epic could not be deleted, so it and its issues are still 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.""" + children = http("POST", api(sandbox, "search/jql"), authorization, {"jql": f"parent = {key}", "fields": ["summary"], "maxResults": 100}) + if isinstance(children, Failure): + return children + found = children.get("issues") if isinstance(children, dict) else None + if not isinstance(found, list): + return Failure(f"Jira answered the search for {key}'s issues with {children!r}, which holds no issues") + for each in [item.get("key") for item in found if isinstance(item, dict)] + [key]: + deleted = http("DELETE", api(sandbox, f"issue/{each}?deleteSubtasks=true"), authorization, None) + if isinstance(deleted, Failure): + return deleted + return None diff --git a/bin/do_epic/e2e/scenario.py b/bin/do_epic/e2e/scenario.py new file mode 100644 index 00000000..18a472ca --- /dev/null +++ b/bin/do_epic/e2e/scenario.py @@ -0,0 +1,118 @@ +"""What the e2e command does to a sandbox epic: runs the do-epic commands that need no tmux session and no worker, each as a process of its own against the real Jira site, and checks what the site holds after each.""" +import dataclasses +import os +import pathlib +import subprocess +import tempfile + +from do_epic import epic_record, findings, finish +from do_epic.backend.jira import JiraEpic +from do_epic.backend.protocol import EpicIssue +from do_epic.e2e.sandbox import FINISH_STATUS, FINISH_TRANSITION +from do_epic.e2e.settings import Sandbox +from do_epic.repo_pick import CLAUDE_LABEL, HUMAN_LABEL +from failure import Failure +from model_routing.ladder_file import read_ladder, SHIPPED +from project_config.settings import Epic, Finish +from record_store.jira_description import EpicDescription + +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) -> bool: + (self.held if holds else self.broke).append(what) + return holds + + +@dataclasses.dataclass(frozen=True) +class Machine: + """A home folder of its own, holding the do-epic settings that point at the sandbox, so that nothing of the real home is read or written.""" + home: pathlib.Path + launcher: pathlib.Path + + def do_epic(self, *args: str, stdin: str = "") -> subprocess.CompletedProcess[str]: + env = {**os.environ, "HOME": str(self.home)} + return subprocess.run([str(self.launcher), *args], input=stdin, env=env, capture_output=True, text=True, timeout=300, check=False) + + +def machine_made(home: pathlib.Path, sandbox: Sandbox, launcher: pathlib.Path) -> Machine | Failure: + folder = home / ".claude-projects" / "do-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) + + +def run(sandbox: Sandbox, epic_key: str, launcher: pathlib.Path) -> Report: + """Drives do-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) + if isinstance(machine, Failure): + report.check(False, f"set up do-epic's settings: {machine.reason}") + return report + items_filed(report, store, epic_key) + finding_filed(report, machine, store, epic_key) + decision_recorded(report, machine, record, epic_key) + closed(report, machine, store, epic_key) + return report + + +def read(report: Report, store: JiraEpic, 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: JiraEpic, epic_key: str) -> None: + """The epic's own issues, which a person files in Jira: an item, its sub-task and a second item.""" + 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: Machine, store: JiraEpic, epic_key: str) -> None: + done = machine.do_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"`do-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 decision_recorded(report: Report, machine: Machine, record: epic_record.EpicRecord, epic_key: str) -> None: + done = machine.do_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"`do-epic {findings.RECORD_DECISION}` records the decision and says so (exit {done.returncode}: {done.stdout.strip() or done.stderr.strip()})") + decisions = record.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 = record.section("Goals") + report.check(isinstance(goals, str) and "Prove do-epic's commands" in goals, f"the decision leaves the description's ## Goals as it was (found {goals!r})") + + +def closed(report: Report, machine: Machine, store: JiraEpic, epic_key: str) -> None: + early = machine.do_epic(epic_key, finish.CLOSE, stdin="Too early.\n") + report.check(early.returncode == 1 and " open" in early.stderr and store.closed() is False, f"`do-epic {finish.CLOSE}` refuses while issues are open and leaves the epic open (exit {early.returncode}: {early.stderr.strip()})") + 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.do_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"`do-epic {finish.CLOSE}` posts the summary and closes the epic (exit {late.returncode}: {late.stdout.strip() or late.stderr.strip()})") + report.check(store.closed() is True, "Jira counts the epic as done") diff --git a/bin/do_epic/e2e/settings.py b/bin/do_epic/e2e/settings.py new file mode 100644 index 00000000..446d70e2 --- /dev/null +++ b/bin/do_epic/e2e/settings.py @@ -0,0 +1,47 @@ +"""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 (docs/decisions.md → `do-epic: drives one Jira epic …`).""" +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 = "DO_EPIC_E2E_JIRA_URL" +ENV_FILE = "DO_EPIC_E2E_ENV_FILE" +PROJECT = "DO_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, 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 misspelt, and what the sandbox's env-file lacks.""" + 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 + 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 values[ENV_FILE] and env_file is not None and not env_file.is_absolute() else []), + ] + if not faults and env_file is not None: + login = basic_auth(env_file) + 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) diff --git a/bin/do_epic/e2e/test_main.py b/bin/do_epic/e2e/test_main.py new file mode 100644 index 00000000..87f40f63 --- /dev/null +++ b/bin/do_epic/e2e/test_main.py @@ -0,0 +1,32 @@ +import contextlib +import io +import pathlib +import sys +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) + +from do_epic.e2e import main +from do_epic.e2e.settings import ENV_FILE, JIRA_URL, PROJECT + + +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("do-epic")) + 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) + code, out, err = self.run_main(["--status"]) + self.assertEqual((code, out), (1, "")) + self.assertIn("usage: do-epic-e2e", err) diff --git a/bin/do_epic/e2e/test_sandbox.py b/bin/do_epic/e2e/test_sandbox.py new file mode 100644 index 00000000..13c2e61f --- /dev/null +++ b/bin/do_epic/e2e/test_sandbox.py @@ -0,0 +1,68 @@ +import pathlib +import sys +import tempfile +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) + +from do_epic.e2e.sandbox import epic_made, Leftover +from do_epic.e2e.settings import Sandbox +from failure import Failure + + +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.refuses = refuses + + def __call__(self, method: str, url: str, authorization: str, body: object) -> object: + self.requests.append((method, url.split("/rest/api/3/")[1])) + 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 {"issues": [{"key": "SBX-2"}, {"key": "SBX-3"}]} + 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.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_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_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")]) diff --git a/bin/do_epic/e2e/test_settings.py b/bin/do_epic/e2e/test_settings.py new file mode 100644 index 00000000..e7464022 --- /dev/null +++ b/bin/do_epic/e2e/test_settings.py @@ -0,0 +1,50 @@ +import pathlib +import sys +import tempfile +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) + +from do_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(ENV_FILE, self.failure({**self.env, ENV_FILE: " "})) + + 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_a_project_that_is_not_a_project_key(self) -> None: + self.assertIn(PROJECT, self.failure({**self.env, PROJECT: "sbx-1"})) + + def test_refuses_an_env_file_that_cannot_be_read_saying_why(self) -> None: + self.assertIn("does not exist", self.failure({**self.env, ENV_FILE: str(self.env_file.parent / "gone.env")})) + self.env_file.chmod(0o644) + self.assertIn("only its owner may read it", 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"}))