Answer stability round 1: 1 🔴, 4 🟡 (0 misread); keep credentials out of questions, stop a clone that waits, ask where a file is in the way

This commit is contained in:
2026-11-18 13:19:55 +01:00
parent 59e176743f
commit 901a7d698f
5 changed files with 63 additions and 42 deletions
+24 -21
View File
@@ -1,6 +1,6 @@
"""Where an epic's repo is on this machine: its checkout and its project config, found by the repo's name.
A missing checkout is cloned from the place of the epic's other repos on their host (docs/decisions.md → `do-epic: drives one Jira epic …`). Where that place is unclear, the answer is an Unsure, a question for the human."""
A missing checkout is cloned from the host and owner of the epic's other repos (docs/decisions.md → `do-epic: drives one Jira epic …`). Where that is unclear, the answer is an Unsure."""
import dataclasses
import pathlib
import re
@@ -9,17 +9,14 @@ from do_epic.config import DoEpicConfig
from failure import Failure
from forge.commands import url_beside
from git_cli.git_repo import Git
from project_config.settings import CONFIG, named, ProjectConfig, PROJECTS
from project_config.settings import by_name, CONFIG, ProjectConfig, PROJECTS
# A name that is one folder name, and no git option.
# One folder name, which can't be a path or a git option.
REPO_NAME = r"\w[\w.-]*"
# The addresses a clone may use: https, ssh, or scp-style `user@host:path`. Not ext::, file: or a local path, which run a program or read the machine's files from an address another checkout's config holds.
# The addresses a clone may use: https, ssh, or scp-style `user@host:path`. Not ext:: or file:, which run a program or read this machine's files, and an origin can spell either.
CLONE_ADDRESS = rf"https://[\w.-]+(:\d+)?/\S+|ssh://[\w.@-]+(:\d+)?/\S+|\w[\w.-]*@[\w.-]+:[\w./~-]+"
def shown(url: str) -> str:
"""url without any `user:token@` after its scheme, since an Unsure may be posted where the team reads it."""
return re.sub(r"^(\w+://)[^/@]*@", r"\1", url)
# A clone that waits longer is stopped, as on a prompt for a password or a host key.
CLONE_SECONDS = 600
@dataclasses.dataclass(frozen=True)
@@ -37,10 +34,11 @@ class Unsure(Failure):
def locate(name: str, others: list[pathlib.Path], config: DoEpicConfig, home: pathlib.Path, git: Git) -> Located | Failure:
"""The checkout and project config of the repo called name; others are the checkouts of the epic's other repos.
A project config names the repo's checkout. Where that checkout is missing, it is cloned. Where no config names the repo, it is cloned into the clone folder, if it isn't there, and the human is asked to write the config: the repo's plan, goals and records are theirs to place. Every other fault is a plain Failure."""
A project config names the repo's checkout, and it is cloned where missing. Where no config names the repo, it is cloned into the clone folder, if it isn't there, and the human is asked to write the config, since the repo's plan, goals and records are theirs to place.
A name that is no repo's name, or a config that can't be read, is a plain Failure. Any other fault is an Unsure."""
if not re.fullmatch(REPO_NAME, name):
return Failure(f"{name!r} is not a repo's name: one folder name that starts with a letter, digit or underscore")
found = named(name, home)
found = by_name(name, home)
if isinstance(found, Failure):
return found
checkout = found.top if found else config.clone_folder / name
@@ -53,22 +51,27 @@ def locate(name: str, others: list[pathlib.Path], config: DoEpicConfig, home: pa
def ensure(name: str, checkout: pathlib.Path, others: list[pathlib.Path], git: Git) -> bool | Failure:
"""Whether checkout was cloned just now: False where it is a git checkout already, else it is cloned there from the place of the others."""
"""Whether checkout was cloned just now: False where it is a git checkout already. Else it is cloned there from the host and owner of the others, or an Unsure says why not."""
if (checkout / ".git").exists():
return False
if checkout.exists() and any(checkout.iterdir()):
if checkout.exists() and (not checkout.is_dir() or any(checkout.iterdir())):
return Unsure(f"{checkout} exists and is not a git checkout, so {name} cannot be cloned there. Where should it be cloned?")
url = place_of(name, others, git)
url = clone_url(name, others, git)
if isinstance(url, Failure):
return url
cloned = git("clone", "--", url, str(checkout))
cloned = git("clone", "--", url, str(checkout), timeout=CLONE_SECONDS)
if isinstance(cloned, Failure):
return Unsure(f"cannot clone {url} to {checkout}: {cloned.reason}. Where does {name} live?")
return True
def place_of(name: str, others: list[pathlib.Path], git: Git) -> str | Failure:
"""The address of the repo called name beside the others: all of them on one host and owner. An Unsure otherwise."""
def without_credentials(url: str) -> str:
"""url without a `user:token@` after its scheme, so that a clone and a question for the team never carry one. The clone then uses git's own credentials."""
return re.sub(r"^(https?://)[^/@]*@", r"\1", url)
def clone_url(name: str, others: list[pathlib.Path], git: Git) -> str | Failure:
"""The address of the repo called name, under the host and owner all the others share. An Unsure where they share none or an address can't be built."""
if not others:
return Unsure(f"{name} is missing and no other repo of the epic shows what host to clone it from. Where does it live?")
urls = set()
@@ -76,13 +79,13 @@ def place_of(name: str, others: list[pathlib.Path], git: Git) -> str | Failure:
origin = git("remote", "get-url", "origin", cwd=str(other))
if isinstance(origin, Failure):
return Unsure(f"cannot read the origin of {other}: {origin.reason}. Where does {name} live?")
beside = url_beside(origin.strip(), name)
beside = url_beside(without_credentials(origin.strip()), name)
if beside is None:
return Unsure(f"{other} has an origin, {shown(origin.strip())}, with no place to put another repo in. Where does {name} live?")
return Unsure(f"{other} has an origin, {without_credentials(origin.strip())}, with no host and owner to put another repo under. Where does {name} live?")
urls.add(beside)
if len(urls) > 1:
return Unsure(f"The epic's other repos sit in more than one place, so {name} could be at {' or '.join(sorted(urls))}. Which holds it?")
return Unsure(f"The epic's other repos sit under more than one host or owner, so {name} could be at {' or '.join(sorted(urls))}. Which holds it?")
url = next(iter(urls))
if not re.fullmatch(CLONE_ADDRESS, url):
return Unsure(f"{shown(url)} is not an https, ssh or scp-style address. Where does {name} live?")
return Unsure(f"{url} is not an https, ssh or scp-style address. Where does {name} live?")
return url
+26 -8
View File
@@ -17,6 +17,7 @@ class FakeGit:
def __init__(self, origins: dict[str, str | Failure], refused: str | None = None) -> None:
self.origins, self.refused = origins, refused
self.cloned: list[tuple[str, str]] = []
self.clone_timeouts: list[float | None] = []
def __call__(self, *args: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False, errors: str = "strict") -> str | Failure:
if args[:3] == ("remote", "get-url", "origin"):
@@ -25,6 +26,7 @@ class FakeGit:
if args[2] == self.refused:
return Failure("git clone: repository not found")
self.cloned.append((args[2], args[3]))
self.clone_timeouts.append(timeout)
pathlib.Path(args[3], ".git").mkdir(parents=True)
return ""
@@ -71,7 +73,8 @@ class LocateTest(unittest.TestCase):
asked = self.unsure(git)
clone = self.home / "clones" / "ledger"
self.assertEqual(git.cloned, [("git@github.com:acme/ledger", str(clone))])
self.assertEqual(asked, f"{clone} was cloned, but no project config names it. Write {self.projects}/<key>/config.toml with `checkout = \"{clone}\"`, as `project-config --help` shows, and run do-epic again.")
self.assertIn(f"{clone} was cloned, but no project config names it", asked)
self.assertIn(f"{self.projects}/<key>/config.toml with `checkout = \"{clone}\"`", asked)
self.assertEqual(self.unsure(git), asked.replace("was cloned", "holds the repo"), "the second ask finds the clone and clones nothing")
self.assertEqual(len(git.cloned), 1)
@@ -86,7 +89,7 @@ class LocateTest(unittest.TestCase):
def test_asks_where_the_other_repos_differ_only_by_owner(self) -> None:
self.configure()
self.origins[str(self.payments)] = "git@github.com:other/payments"
self.assertIn("more than one place", self.unsure(FakeGit(self.origins)))
self.assertIn("more than one host or owner", self.unsure(FakeGit(self.origins)))
def test_asks_where_no_other_repo_shows_where_to_clone_from(self) -> None:
self.configure()
@@ -99,7 +102,7 @@ class LocateTest(unittest.TestCase):
self.origins[str(self.billing)] = Failure("git remote: no such remote 'origin'")
self.assertIn(f"cannot read the origin of {self.billing}: git remote: no such remote 'origin'", self.unsure(FakeGit(self.origins)))
self.origins[str(self.billing)] = "billing"
self.assertIn(f"{self.billing} has an origin, billing, with no place to put another repo in", self.unsure(FakeGit(self.origins)))
self.assertIn(f"{self.billing} has an origin, billing, with no host and owner to put another repo under", self.unsure(FakeGit(self.origins)))
def test_asks_where_the_clone_fails_naming_the_url_and_the_reason(self) -> None:
self.configure()
@@ -131,13 +134,28 @@ class LocateTest(unittest.TestCase):
self.assertIn("is not an https, ssh or scp-style address", self.unsure(git))
self.assertEqual(git.cloned, [])
def test_leaves_the_credentials_of_an_origin_out_of_a_question(self) -> None:
def test_clones_and_asks_without_the_credentials_an_origin_holds(self) -> None:
self.configure()
self.origins[str(self.billing)] = "https://bot:s3cret@gitea.larvit.se/acme/billing"
self.origins[str(self.payments)] = "https://bot:s3cret@gitea.larvit.se/acme/payments"
asked = self.unsure(FakeGit(self.origins))
self.assertIn("https://gitea.larvit.se/acme/ledger is not an https", asked)
self.assertNotIn("s3cret", asked)
self.origins[str(self.payments)] = "https://other:t0ken@gitea.larvit.se/acme/payments.git"
git = FakeGit(self.origins)
self.assertIsInstance(self.locate(git), Located)
self.assertEqual(git.cloned, [("https://gitea.larvit.se/acme/ledger", str(self.home / "code" / "ledger"))])
self.origins[str(self.payments)] = "https://other:t0ken@gitea.larvit.se/acme/payments"
self.origins[str(self.billing)] = "https://bot:s3cret@gitea.larvit.se"
self.assertNotIn("s3cret", self.unsure(FakeGit(self.origins), "stock"))
def test_stops_a_clone_that_waits_for_a_prompt(self) -> None:
self.configure()
git = FakeGit(self.origins)
self.locate(git)
self.assertEqual(git.clone_timeouts, [600])
def test_asks_where_the_place_to_clone_into_is_a_file(self) -> None:
self.configure()
(self.home / "code").mkdir()
(self.home / "code" / "ledger").write_text("x", encoding="utf-8")
self.assertIn("exists and is not a git checkout", self.unsure(FakeGit(self.origins)))
def test_fails_with_the_config_failure_where_a_config_cannot_be_read(self) -> None:
(self.projects / "broken").mkdir(parents=True)
+3 -3
View File
@@ -1,4 +1,4 @@
"""Which forge a checkout's origin is on, and running that forge's CLI in the checkout."""
"""Which forge a checkout's origin is on, a repo's URL beside that origin's, and running the forge's CLI in the checkout."""
import json
import re
import subprocess
@@ -18,9 +18,9 @@ def forge_of(origin_url: str) -> Forge:
def url_beside(origin_url: str, name: str) -> str | None:
"""The origin URL of the repo called name in the same place, host and owner, as origin_url's repo; None where origin_url has no place, such as a bare name. The URL has no `.git`, since gh and tea clone without it."""
"""The origin URL of the repo called name under the same host and owner as origin_url's repo; None where origin_url has no place, such as a bare name. The URL has no `.git`, which git clones without."""
place = re.fullmatch(r"(.+[/:])[^/:]+", origin_url.rstrip("/"))
# A scheme's own `//` leaves no repo to put a name beside.
# An origin of only a scheme and a host, such as `https://host`, has no owner folder to put a name in.
return place.group(1) + name if place and not place.group(1).endswith("//") else None
+2 -2
View File
@@ -350,8 +350,8 @@ def held(folder: pathlib.Path, home: pathlib.Path) -> ProjectConfig | Failure |
return config if isinstance(config, Failure) else load(config[0], config[0], home)
def named(name: str, home: pathlib.Path) -> ProjectConfig | Failure | None:
"""The config of the project whose main checkout is a folder called name, which may not exist yet, loaded as `held` does; None where no config names one. A config that cannot be read fails every name, since it may be the one asked for."""
def by_name(name: str, home: pathlib.Path) -> ProjectConfig | Failure | None:
"""The config of the project whose main checkout is a folder called name, though that folder may not exist yet, loaded as `held` does; None where no config names one. A config that cannot be read fails every name, since it may be the one asked for."""
files = []
for file in configs(home):
config = read(file, home)
+8 -8
View File
@@ -9,7 +9,7 @@ import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from project_config.settings import env_files, Epic, Finish, held, JiraScoring, load, named, NOTIFIERS, OUTSIDE, Page, ProjectConfig, SETTINGS, Unnamed
from project_config.settings import by_name, env_files, Epic, Finish, held, JiraScoring, load, NOTIFIERS, OUTSIDE, Page, ProjectConfig, SETTINGS, Unnamed
from failure import Failure
from test_env import isolated_env
from project_config.main import located, title_templates
@@ -598,30 +598,30 @@ class HeldTest(ConfigCase):
self.assertIsInstance(held(target, self.home), ProjectConfig, "git names the folder by its resolved path")
class NamedTest(ConfigCase):
class ByNameTest(ConfigCase):
def test_finds_the_config_of_the_project_whose_main_checkout_has_the_name_though_the_checkout_is_missing(self) -> None:
folder = self.write("acme", CLIENT)
found = named("acme-billing", self.home)
found = by_name("acme-billing", self.home)
assert isinstance(found, ProjectConfig), found
self.assertEqual((found.folder, found.top), (folder, self.checkout))
def test_finds_none_where_no_config_names_a_checkout_of_that_name(self) -> None:
self.write("acme", CLIENT)
self.assertIsNone(named("billing", self.home), "a part of a name is not the name")
self.assertIsNone(named("acme", self.home), "a config's key is not a repo's name")
self.assertIsNone(named("do-epic", self.home))
self.assertIsNone(by_name("billing", self.home), "a part of a name is not the name")
self.assertIsNone(by_name("acme", self.home), "a config's key is not a repo's name")
self.assertIsNone(by_name("do-epic", self.home))
def test_fails_where_two_configs_name_a_checkout_of_that_name(self) -> None:
self.write("acme", CLIENT)
self.write("acme-two", CLIENT.replace("~/code/acme-billing", "~/other/acme-billing"))
found = named("acme-billing", self.home)
found = by_name("acme-billing", self.home)
assert isinstance(found, Failure), found
self.assertIn("acme/config.toml and ", found.reason)
self.assertIn("acme-two/config.toml each name a checkout called acme-billing", found.reason)
def test_fails_where_a_config_cannot_be_read_since_it_may_be_the_one_asked_for(self) -> None:
self.write("acme", "this is not toml")
self.assertIsInstance(named("acme-billing", self.home), Failure)
self.assertIsInstance(by_name("acme-billing", self.home), Failure)
class EnvFilesTest(ConfigCase):