diff --git a/app_errors/README.md b/app_errors/README.md index b07eee6..7f002d3 100644 --- a/app_errors/README.md +++ b/app_errors/README.md @@ -26,7 +26,7 @@ The ingest skips `kartka` in RTDB, because kartka still runs its own ingest. `ui.snackbar`, `perf.slow`, ...), `error`, `stack` (list of frames), plus these optional fields: -- `repo` (`owner/name`, default `kuhyx/`) +- `repo` (`owner/name`; default: the `origin` of `~/src/` when it is on GitHub, else `kuhyx/`) - `mark` (the substring that marks the app's own frames: `package:todo/`, `leetcode_guard/`, `res://`) - `route` diff --git a/app_errors/app_errors/ingest.py b/app_errors/app_errors/ingest.py index 4530efb..c046892 100644 --- a/app_errors/app_errors/ingest.py +++ b/app_errors/app_errors/ingest.py @@ -13,11 +13,12 @@ its ``kartka`` adapter, and two readers of ``/kartka/errors`` would race. from __future__ import annotations from dataclasses import dataclass +import functools import os from pathlib import Path from typing import TYPE_CHECKING -from app_errors import spool, store +from app_errors import origin, spool, store from app_errors.rtdb import RtdbError if TYPE_CHECKING: @@ -34,6 +35,8 @@ class Paths: state: Path reports: Path spool: Path + #: Checkouts: ``/``'s origin names the repo of a report without one. + src_root: Path @dataclass(frozen=True) @@ -45,12 +48,13 @@ class Summary: def default_paths() -> Paths: - """Return the paths under ``$XDG_STATE_HOME`` (default ``~/.local/state``).""" + """Return the state paths (``$XDG_STATE_HOME``, default ``~/.local/state``) and ``~/src``.""" base = Path(os.environ.get("XDG_STATE_HOME") or Path.home() / ".local" / "state") return Paths( base / "app-errors-ingest" / "state.json", base / "app-errors", base / "app-errors-spool", + Path.home() / "src", ) @@ -75,6 +79,7 @@ def _rtdb_entries( def run(paths: Paths, db: Rtdb | None, *, keep: bool = False) -> Summary: """Ingest everything once; RTDB failures are returned, the spool still goes in.""" state = store.load(paths.state) + repo_of = functools.partial(origin.repo_slug, paths.src_root, owner=store.OWNER) merged = 0 rtdb_error = None deletes: dict[str, dict[str, object]] = {} @@ -83,8 +88,9 @@ def run(paths: Paths, db: Rtdb | None, *, keep: bool = False) -> Summary: found, deletes = _rtdb_entries(db) except RtdbError as e: found, rtdb_error = [], str(e) - merged += sum(store.merge(state, app, key, entry) for app, key, entry in found) - merged += sum(store.merge(state, line.app, line.id, line.entry) for line in spool.drain(paths.spool)) + merged += sum(store.merge(state, a, k, e, repo_of=repo_of) for a, k, e in found) + lines = spool.drain(paths.spool) + merged += sum(store.merge(state, ln.app, ln.id, ln.entry, repo_of=repo_of) for ln in lines) store.save(paths.state, state) store.write_reports(paths.reports, state) if db is not None and not keep: diff --git a/app_errors/app_errors/origin.py b/app_errors/app_errors/origin.py new file mode 100644 index 0000000..2c58728 --- /dev/null +++ b/app_errors/app_errors/origin.py @@ -0,0 +1,61 @@ +# Copyright (c) 2026 Krzysztof Rudnicki +"""Which GitHub repo a local checkout is, read from its ``origin`` remote. + +A folder name says nothing about the owner: ``~/src/cleanup-plate`` is +``Sepci0/cleanup-plate`` and ``~/src/todo`` is ``kuhyx/todo-app``. Guessing +``/`` filed tasks and error tickets under repos that do not exist, so the guess is +only the fallback for a folder with no checkout or no GitHub origin. +""" + +from __future__ import annotations + +import re +import shutil +import subprocess +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from pathlib import Path + +GIT_TIMEOUT = 5.0 +# https://[user@]github.com/o/n[.git], ssh://git@github.com[:port]/o/n[.git] +# and git@github.com:o/n[.git]. Other hosts (a local mirror, a gitea) are not +# the GitHub slug the board works with, so they fall back to the guess. +_GITHUB = re.compile( + r"^(?:https?://(?:[^@/]+@)?github\.com/|ssh://git@github\.com(?::\d+)?/" + r"|git@github\.com:)(?P[^/\s]+/[^/\s]+?)(?:\.git)?/?$" +) + + +def slug_from_url(url: str) -> str | None: + """Return ``owner/name`` of a GitHub remote URL, or None for any other URL.""" + match = _GITHUB.match(url.strip()) + return match["slug"] if match else None + + +def origin_slug(checkout: Path) -> str | None: + """Return the GitHub ``owner/name`` of ``checkout``'s origin, or None. + + None when ``checkout`` is not a git checkout (a ``.git`` dir, or file for a + worktree), has no ``origin``, git is missing or hangs, or origin is not + GitHub. Checking ``.git`` first keeps git from walking up to a parent repo. + """ + git = shutil.which("git") + if git is None or not (checkout / ".git").exists(): + return None + try: + out = subprocess.run( + [git, "-C", str(checkout), "remote", "get-url", "origin"], + capture_output=True, + text=True, + check=False, + timeout=GIT_TIMEOUT, + ) + except OSError, subprocess.SubprocessError: + return None + return None if out.returncode else slug_from_url(out.stdout) + + +def repo_slug(src_root: Path, folder: str, owner: str) -> str: + """Return the slug of ``src_root/folder``'s origin, else ``owner/folder``.""" + return origin_slug(src_root / folder) or f"{owner}/{folder}" diff --git a/app_errors/app_errors/store.py b/app_errors/app_errors/store.py index 6bed8c3..03b0c5d 100644 --- a/app_errors/app_errors/store.py +++ b/app_errors/app_errors/store.py @@ -24,13 +24,20 @@ from typing import TYPE_CHECKING, NotRequired, TypedDict, cast from app_errors.signature import SLOW_KIND, mark_of, signature, strings, top_frame if TYPE_CHECKING: - from collections.abc import Mapping + from collections.abc import Callable, Mapping #: Entry ids remembered for dedup (oldest dropped first). MAX_SEEN = 5000 #: An app name the board accepts (the contract's ``app`` pattern). APP_NAME = re.compile(r"^[a-z0-9][a-z0-9_-]*$") _REPO = re.compile(r"^[^/\s]+/[^/\s]+$") +#: The owner guessed for an app whose report names no repo and has no checkout. +OWNER = "kuhyx" + + +def guess_repo(app: str) -> str: + """Return ``OWNER/app``: the fallback when nothing better names the repo.""" + return f"{OWNER}/{app}" class Resolved(TypedDict): @@ -109,8 +116,18 @@ def _reopens(resolved: Resolved, at: str, build: int) -> bool: return at > resolved["at"] -def merge(state: State, app: str, entry_id: str, raw: object) -> bool: - """Count one report ``raw`` of ``app`` under its signature; False when seen or not a report.""" +def merge( + state: State, + app: str, + entry_id: str, + raw: object, + *, + repo_of: Callable[[str], str] = guess_repo, +) -> bool: + """Count one report ``raw`` of ``app`` under its signature; False when seen or not a report. + + A new record takes the report's ``repo``, else ``repo_of(app)``. + """ if not isinstance(raw, dict): return False entry = cast("Mapping[str, object]", raw) @@ -131,7 +148,7 @@ def merge(state: State, app: str, entry_id: str, raw: object) -> bool: repo = _str(entry.get("repo")) rec = recs[sig.id] = { "signature": sig.id, - "repo": repo if repo and _REPO.match(repo) else f"kuhyx/{app}", + "repo": repo if repo and _REPO.match(repo) else repo_of(app), "kind": kind, "message": sig.message, "top_frame": top_frame(strings(entry.get("stack")), mark_of(entry, app)), diff --git a/app_errors/app_errors/tests/checkout.py b/app_errors/app_errors/tests/checkout.py new file mode 100644 index 0000000..fc393b8 --- /dev/null +++ b/app_errors/app_errors/tests/checkout.py @@ -0,0 +1,20 @@ +"""A real git checkout in a temp dir, built without kuhy's git config.""" + +from __future__ import annotations + +import os +from pathlib import Path +import shutil +import subprocess + +GIT = shutil.which("git") or "git" + + +def make_checkout(path: Path, origin: str | None) -> Path: + """Init a repo at ``path`` with remote ``origin`` (none when None).""" + env = {"PATH": os.environ["PATH"], "HOME": str(path.parent), "GIT_CONFIG_NOSYSTEM": "1"} + path.mkdir(parents=True) + subprocess.run([GIT, "init", "-q", str(path)], check=True, env=env) + if origin is not None: + subprocess.run([GIT, "-C", str(path), "remote", "add", "origin", origin], check=True, env=env) + return path diff --git a/app_errors/app_errors/tests/test_ingest.py b/app_errors/app_errors/tests/test_ingest.py index 66d2958..e1e2270 100644 --- a/app_errors/app_errors/tests/test_ingest.py +++ b/app_errors/app_errors/tests/test_ingest.py @@ -9,6 +9,7 @@ import pytest from app_errors import cli, ingest, spool from app_errors.rtdb import RtdbError +from app_errors.tests.checkout import make_checkout class FakeRtdb: @@ -64,6 +65,7 @@ def dirs(tmp_path: Path) -> ingest.Paths: state=tmp_path / "ingest" / "state.json", reports=tmp_path / "app-errors", spool=tmp_path / "app-errors-spool", + src_root=tmp_path / "src", ) @@ -105,6 +107,18 @@ def test_rtdb_down_still_ingests_spool(dirs: ingest.Paths) -> None: assert list(dirs.spool.glob("*")) == [] # the spool is consumed +def test_report_without_repo_takes_the_checkouts_origin(dirs: ingest.Paths) -> None: + # Regression: a report with no repo was filed under a guessed kuhyx/; + # the checkout's origin names the real one (here Sepci0/cleanup-plate). + make_checkout(dirs.src_root / "cleanup-plate", "git@github.com:Sepci0/cleanup-plate.git") + spool.append(dirs.spool, "cleanup-plate", {"at": "2026-11-17T10:00:00Z", "kind": "k", "error": "e"}) + spool.append(dirs.spool, "kartka", {"at": "2026-11-17T10:00:00Z", "kind": "k", "error": "e"}) + assert ingest.run(dirs, None).merged == 2 + repos = {app: json.loads((dirs.reports / f"{app}.json").read_text())["errors"][0]["repo"] + for app in ("cleanup-plate", "kartka")} # fmt: skip + assert repos == {"cleanup-plate": "Sepci0/cleanup-plate", "kartka": "kuhyx/kartka"} + + def test_no_rtdb_at_all(dirs: ingest.Paths) -> None: assert ingest.run(dirs, None).merged == 0 diff --git a/app_errors/app_errors/tests/test_origin.py b/app_errors/app_errors/tests/test_origin.py new file mode 100644 index 0000000..eb59216 --- /dev/null +++ b/app_errors/app_errors/tests/test_origin.py @@ -0,0 +1,74 @@ +"""Origin lookup: every remote URL form, and every way a checkout has none.""" + +from __future__ import annotations + +from pathlib import Path +import shutil +import subprocess + +import pytest + +from app_errors import origin as _origin +from app_errors.origin import origin_slug, repo_slug, slug_from_url +from app_errors.tests.checkout import make_checkout + + +@pytest.mark.parametrize( + "url", + [ + "https://github.com/Sepci0/cleanup-plate", + "https://github.com/Sepci0/cleanup-plate.git", + "https://github.com/Sepci0/cleanup-plate/\n", + "https://kuhy@github.com/Sepci0/cleanup-plate.git", + "git@github.com:Sepci0/cleanup-plate.git", + "ssh://git@github.com/Sepci0/cleanup-plate", + "ssh://git@github.com:22/Sepci0/cleanup-plate.git", + ], +) +def test_github_urls_give_the_slug(url: str) -> None: + assert slug_from_url(url) == "Sepci0/cleanup-plate" + + +@pytest.mark.parametrize( + "url", + [ + "/home/someone/mirror/cleanup-plate", + "file:///srv/git/cleanup-plate.git", + "https://gitea.example.org/Sepci0/cleanup-plate.git", + "https://github.com/Sepci0", + "https://github.com/Sepci0/cleanup-plate/extra", + "", + ], +) +def test_other_urls_give_none(url: str) -> None: + assert slug_from_url(url) is None + + +def test_checkout_origin_is_read(tmp_path: Path) -> None: + repo = make_checkout(tmp_path / "todo", "https://github.com/kuhyx/todo-app.git") + assert origin_slug(repo) == "kuhyx/todo-app" + + +def test_no_checkout_or_no_origin_is_none(tmp_path: Path) -> None: + (tmp_path / "plain").mkdir() + assert origin_slug(tmp_path / "missing") is None + assert origin_slug(tmp_path / "plain") is None + assert origin_slug(make_checkout(tmp_path / "bare", None)) is None + + +def test_missing_or_hung_git_is_none(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + repo = make_checkout(tmp_path / "todo", "https://github.com/kuhyx/todo-app") + + def hang(*args: object, **kwargs: object) -> None: + raise subprocess.TimeoutExpired("git", _origin.GIT_TIMEOUT) + + monkeypatch.setattr(subprocess, "run", hang) + assert origin_slug(repo) is None + monkeypatch.setattr(shutil, "which", lambda _: None) + assert origin_slug(repo) is None + + +def test_repo_slug_falls_back_to_the_owner_guess(tmp_path: Path) -> None: + make_checkout(tmp_path / "todo", "git@github.com:kuhyx/todo-app.git") + assert repo_slug(tmp_path, "todo", "kuhyx") == "kuhyx/todo-app" + assert repo_slug(tmp_path, "museum", "kuhyx") == "kuhyx/museum" diff --git a/app_errors/app_errors/tests/test_rtdb.py b/app_errors/app_errors/tests/test_rtdb.py index 96493de..0c46ed2 100644 --- a/app_errors/app_errors/tests/test_rtdb.py +++ b/app_errors/app_errors/tests/test_rtdb.py @@ -118,7 +118,7 @@ class _PatchFails: def test_delete_failure_is_reported_after_save(tmp_path: Path) -> None: - paths = ingest.Paths(tmp_path / "s.json", tmp_path / "r", tmp_path / "sp") + paths = ingest.Paths(tmp_path / "s.json", tmp_path / "r", tmp_path / "sp", tmp_path / "src") summary = ingest.run(paths, _PatchFails()) assert summary == ingest.Summary(1, "patch down") assert (tmp_path / "r" / "todo.json").exists() diff --git a/app_errors/pyproject.toml b/app_errors/pyproject.toml index 48a24c4..ff1252d 100644 --- a/app_errors/pyproject.toml +++ b/app_errors/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "app-errors" -version = "0.1.1" +version = "0.1.2" description = "Shared app error and slow-wait ingest: RTDB and local spool reports become todoImplementer error tickets" requires-python = ">=3.14" dependencies = [ diff --git a/app_errors/uv.lock b/app_errors/uv.lock index cf7f461..9220a52 100644 --- a/app_errors/uv.lock +++ b/app_errors/uv.lock @@ -8,7 +8,7 @@ resolution-markers = [ [[package]] name = "app-errors" -version = "0.1.1" +version = "0.1.2" source = { virtual = "." } dependencies = [ { name = "crdt-sync" }, diff --git a/feedback_inbox/README.md b/feedback_inbox/README.md index 3996de2..b4cf079 100644 --- a/feedback_inbox/README.md +++ b/feedback_inbox/README.md @@ -6,7 +6,9 @@ kuhy saves with F12 is never forgotten. A game saves `~/data/_binaries/feedback//` (`report.json`, screenshots, logs) and appends a ready-to-run Claude prompt to `feedback-.md` beside it. This package imports each complete bundle once -as a task in the todo app for `kuhyx/`. +as a task in the todo app for the GitHub repo named by the `origin` of +`~/src/` (`kuhyx/` only when there is no such checkout or it is not +on GitHub). ## Task shape diff --git a/feedback_inbox/feedback_inbox/_cli.py b/feedback_inbox/feedback_inbox/_cli.py index 1da10a6..2e8e982 100644 --- a/feedback_inbox/feedback_inbox/_cli.py +++ b/feedback_inbox/feedback_inbox/_cli.py @@ -42,7 +42,7 @@ def _run(wait: float, client: TodoClient | None) -> int: fcntl.flock(lock, fcntl.LOCK_EX) results = run_settled( paths.data_root, - Importer(todo, OWNER), + Importer(todo, OWNER, paths.src_root), Waiter(wait, time.time, time.sleep), ) failed = 0 diff --git a/feedback_inbox/feedback_inbox/_inbox.py b/feedback_inbox/feedback_inbox/_inbox.py index 7121f4c..97453ba 100644 --- a/feedback_inbox/feedback_inbox/_inbox.py +++ b/feedback_inbox/feedback_inbox/_inbox.py @@ -24,6 +24,7 @@ from feedback_inbox._entry import ( note_from_entry, read_report, ) +from feedback_inbox._origin import repo_slug from feedback_inbox._todo import TodoError if TYPE_CHECKING: @@ -122,10 +123,11 @@ def _read_marker(bundle: Path) -> dict[str, object] | None: @dataclass(frozen=True) class Importer: - """Who files the tasks (``client``) under which GitHub owner.""" + """Who files the tasks (``client``), where checkouts live, the fallback owner.""" client: TodoClient owner: str + src_root: Path settle: float = SETTLE_SECONDS def import_bundle(self, feedback_dir: Path, bundle: Path, now: float) -> Result: @@ -140,7 +142,8 @@ class Importer: return Result(bundle, Outcome.PENDING, detail="report.json settling") repo = repo_name(feedback_dir) draft = build_task(repo, bundle.name, entry, read_report(report_path)) - return self._file(bundle, f"{self.owner}/{repo}", draft) + slug = repo_slug(self.src_root, repo, self.owner) + return self._file(bundle, slug, draft) def _file(self, bundle: Path, repo: str, draft: TaskDraft) -> Result: """Create the task, write the marker, then fix the title.""" diff --git a/feedback_inbox/feedback_inbox/_origin.py b/feedback_inbox/feedback_inbox/_origin.py new file mode 100644 index 0000000..53eb1cc --- /dev/null +++ b/feedback_inbox/feedback_inbox/_origin.py @@ -0,0 +1,61 @@ +# Copyright (c) 2026 Krzysztof Rudnicki +"""Which GitHub repo a local checkout is, read from its ``origin`` remote. + +A folder name says nothing about the owner: ``~/src/cleanup-plate`` is +``Sepci0/cleanup-plate`` and ``~/src/todo`` is ``kuhyx/todo-app``. Guessing +``/`` filed tasks under repos that do not exist, so the guess is +only the fallback for a folder with no checkout or no GitHub origin. +""" + +from __future__ import annotations + +import re +import shutil +import subprocess +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from pathlib import Path + +GIT_TIMEOUT = 5.0 +# https://[user@]github.com/o/n[.git], ssh://git@github.com[:port]/o/n[.git] +# and git@github.com:o/n[.git]. Other hosts (a local mirror, a gitea) are not +# the GitHub slug the board works with, so they fall back to the guess. +_GITHUB = re.compile( + r"^(?:https?://(?:[^@/]+@)?github\.com/|ssh://git@github\.com(?::\d+)?/" + r"|git@github\.com:)(?P[^/\s]+/[^/\s]+?)(?:\.git)?/?$" +) + + +def slug_from_url(url: str) -> str | None: + """Return ``owner/name`` of a GitHub remote URL, or None for any other URL.""" + match = _GITHUB.match(url.strip()) + return match["slug"] if match else None + + +def origin_slug(checkout: Path) -> str | None: + """Return the GitHub ``owner/name`` of ``checkout``'s origin, or None. + + None when ``checkout`` is not a git checkout (a ``.git`` dir, or file for a + worktree), has no ``origin``, git is missing or hangs, or origin is not + GitHub. Checking ``.git`` first keeps git from walking up to a parent repo. + """ + git = shutil.which("git") + if git is None or not (checkout / ".git").exists(): + return None + try: + out = subprocess.run( + [git, "-C", str(checkout), "remote", "get-url", "origin"], + capture_output=True, + text=True, + check=False, + timeout=GIT_TIMEOUT, + ) + except OSError, subprocess.SubprocessError: + return None + return None if out.returncode else slug_from_url(out.stdout) + + +def repo_slug(src_root: Path, folder: str, owner: str) -> str: + """Return the slug of ``src_root/folder``'s origin, else ``owner/folder``.""" + return origin_slug(src_root / folder) or f"{owner}/{folder}" diff --git a/feedback_inbox/feedback_inbox/_paths.py b/feedback_inbox/feedback_inbox/_paths.py index b92c78f..6a4726a 100644 --- a/feedback_inbox/feedback_inbox/_paths.py +++ b/feedback_inbox/feedback_inbox/_paths.py @@ -24,6 +24,8 @@ class Paths: lock_file: Path unit_dir: Path mcp_url: str + # Game checkouts: ``/``'s origin names the GitHub repo. + src_root: Path @classmethod def under(cls, root: Path) -> Paths: @@ -34,6 +36,7 @@ class Paths: lock_file=root / "state" / "lock", unit_dir=root / "units", mcp_url=DEAD_URL, + src_root=root / "src", ) @@ -48,4 +51,5 @@ def resolve_paths(given: Paths | None = None) -> Paths: lock_file=home / ".local" / "share" / "feedback-inbox" / "lock", unit_dir=home / ".config" / "systemd" / "user", mcp_url=MCP_URL, + src_root=home / "src", ) diff --git a/feedback_inbox/feedback_inbox/tests/test_inbox.py b/feedback_inbox/feedback_inbox/tests/test_inbox.py index d0190d4..f12740a 100644 --- a/feedback_inbox/feedback_inbox/tests/test_inbox.py +++ b/feedback_inbox/feedback_inbox/tests/test_inbox.py @@ -16,7 +16,12 @@ from feedback_inbox._inbox import ( run_once, run_settled, ) -from feedback_inbox.tests.test_support import STAMP, FakeTodo, make_bundle +from feedback_inbox.tests.test_support import ( + STAMP, + FakeTodo, + make_bundle, + make_checkout, +) if TYPE_CHECKING: from pathlib import Path @@ -29,7 +34,21 @@ MANUAL = "MANUAL WORK REQUIRED: " def _run(paths: Paths, todo: FakeTodo) -> list[Outcome]: - return [r.outcome for r in run_once(paths.data_root, Importer(todo, OWNER), NOW)] + return [ + r.outcome + for r in run_once(paths.data_root, Importer(todo, OWNER, paths.src_root), NOW) + ] + + +def test_task_goes_to_the_checkouts_origin_not_a_guessed_owner(paths: Paths) -> None: + # Regression: the board showed a phantom kuhyx/cleanup-plate; the real + # repo is the origin of the checkout, Sepci0/cleanup-plate. + origin = "https://github.com/Sepci0/cleanup-plate" + make_checkout(paths.src_root / "cleanup-plate", origin) + make_bundle(paths.data_root) + todo = FakeTodo() + assert _run(paths, todo) == [Outcome.IMPORTED] + assert [repo for repo, _, _ in todo.calls] == ["Sepci0/cleanup-plate"] def test_complete_bundle_becomes_one_task(paths: Paths) -> None: @@ -64,7 +83,7 @@ def test_incomplete_bundles_wait(paths: Paths) -> None: todo = FakeTodo() assert _run(paths, todo) == [Outcome.PENDING] * 3 assert todo.calls == [] - later = run_once(paths.data_root, Importer(todo, OWNER), NOW + 10) + later = run_once(paths.data_root, Importer(todo, OWNER, paths.src_root), NOW + 10) assert [r.outcome for r in later] == [ Outcome.PENDING, Outcome.PENDING, @@ -126,7 +145,7 @@ def test_title_uses_first_line_and_caps_length() -> None: def test_todo_failure_leaves_no_marker(paths: Paths) -> None: bundle = make_bundle(paths.data_root) todo = FakeTodo(fail=True) - results = run_once(paths.data_root, Importer(todo, OWNER), NOW) + results = run_once(paths.data_root, Importer(todo, OWNER, paths.src_root), NOW) assert [r.outcome for r in results] == [Outcome.FAILED] assert "todo down" in results[0].detail assert not (bundle / MARKER).exists() @@ -135,7 +154,9 @@ def test_todo_failure_leaves_no_marker(paths: Paths) -> None: def test_merge_is_flagged_but_marked(paths: Paths) -> None: make_bundle(paths.data_root) - results = run_once(paths.data_root, Importer(FakeTodo(merged=True), OWNER), NOW) + results = run_once( + paths.data_root, Importer(FakeTodo(merged=True), OWNER, paths.src_root), NOW + ) assert results[0].outcome is Outcome.IMPORTED assert "merged" in results[0].detail @@ -143,12 +164,16 @@ def test_merge_is_flagged_but_marked(paths: Paths) -> None: def test_marker_write_failure_is_reported(paths: Paths) -> None: bundle = make_bundle(paths.data_root) (bundle / MARKER).mkdir() - results = run_once(paths.data_root, Importer(FakeTodo(), OWNER), NOW) + results = run_once( + paths.data_root, Importer(FakeTodo(), OWNER, paths.src_root), NOW + ) assert results[0].outcome is Outcome.SKIPPED (bundle / MARKER).rmdir() bundle.chmod(0o500) try: - results = run_once(paths.data_root, Importer(FakeTodo(), OWNER), NOW) + results = run_once( + paths.data_root, Importer(FakeTodo(), OWNER, paths.src_root), NOW + ) finally: bundle.chmod(0o700) assert results[0].outcome is Outcome.FAILED @@ -166,7 +191,9 @@ def test_run_settled_waits_then_gives_up(paths: Paths, tmp_path: Path) -> None: todo = FakeTodo() results = run_settled( - paths.data_root, Importer(todo, OWNER), Waiter(5.0, lambda: clock[0], sleep) + paths.data_root, + Importer(todo, OWNER, paths.src_root), + Waiter(5.0, lambda: clock[0], sleep), ) assert [r.outcome for r in results] == [Outcome.IMPORTED] assert slept == [1.0, 1.0] @@ -176,7 +203,9 @@ def test_run_settled_waits_then_gives_up(paths: Paths, tmp_path: Path) -> None: make_bundle(other, report=None) slept.clear() results = run_settled( - other, Importer(todo, OWNER), Waiter(3.0, lambda: clock[0], sleep) + other, + Importer(todo, OWNER, paths.src_root), + Waiter(3.0, lambda: clock[0], sleep), ) assert [r.outcome for r in results] == [Outcome.PENDING] assert slept == [1.0, 1.0, 1.0] diff --git a/feedback_inbox/feedback_inbox/tests/test_inbox_repair.py b/feedback_inbox/feedback_inbox/tests/test_inbox_repair.py index 484687a..eed30af 100644 --- a/feedback_inbox/feedback_inbox/tests/test_inbox_repair.py +++ b/feedback_inbox/feedback_inbox/tests/test_inbox_repair.py @@ -28,7 +28,10 @@ MANUAL = "MANUAL WORK REQUIRED: " def _run(paths: Paths, todo: FakeTodo) -> list[Outcome]: - return [r.outcome for r in run_once(paths.data_root, Importer(todo, OWNER), NOW)] + return [ + r.outcome + for r in run_once(paths.data_root, Importer(todo, OWNER, paths.src_root), NOW) + ] def _marker(bundle: Path) -> dict[str, object]: @@ -63,7 +66,7 @@ def test_failed_update_keeps_marker_and_retries_without_a_second_task( ) -> None: bundle = make_bundle(paths.data_root) todo = FakeTodo(fail_update=True) - results = run_once(paths.data_root, Importer(todo, OWNER), NOW) + results = run_once(paths.data_root, Importer(todo, OWNER, paths.src_root), NOW) assert [r.outcome for r in results] == [Outcome.FAILED] assert "update refused" in results[0].detail assert "format" not in _marker(bundle) @@ -138,7 +141,9 @@ def test_repair_reports_an_unwritable_marker(paths: Paths) -> None: ) bundle.chmod(0o500) try: - results = run_once(paths.data_root, Importer(FakeTodo(), OWNER), NOW) + results = run_once( + paths.data_root, Importer(FakeTodo(), OWNER, paths.src_root), NOW + ) finally: bundle.chmod(0o700) assert results[0].outcome is Outcome.FAILED diff --git a/feedback_inbox/feedback_inbox/tests/test_origin.py b/feedback_inbox/feedback_inbox/tests/test_origin.py new file mode 100644 index 0000000..a58dfe3 --- /dev/null +++ b/feedback_inbox/feedback_inbox/tests/test_origin.py @@ -0,0 +1,80 @@ +# Copyright (c) 2026 Krzysztof Rudnicki +"""Origin lookup: every remote URL form, and every way a checkout has none.""" + +from __future__ import annotations + +import subprocess +from typing import TYPE_CHECKING + +import pytest + +from feedback_inbox import _origin +from feedback_inbox._origin import origin_slug, repo_slug, slug_from_url +from feedback_inbox.tests.test_support import make_checkout + +if TYPE_CHECKING: + from pathlib import Path + + +@pytest.mark.parametrize( + "url", + [ + "https://github.com/Sepci0/cleanup-plate", + "https://github.com/Sepci0/cleanup-plate.git", + "https://github.com/Sepci0/cleanup-plate/\n", + "https://kuhy@github.com/Sepci0/cleanup-plate.git", + "git@github.com:Sepci0/cleanup-plate.git", + "ssh://git@github.com/Sepci0/cleanup-plate", + "ssh://git@github.com:22/Sepci0/cleanup-plate.git", + ], +) +def test_github_urls_give_the_slug(url: str) -> None: + assert slug_from_url(url) == "Sepci0/cleanup-plate" + + +@pytest.mark.parametrize( + "url", + [ + "/home/someone/mirror/cleanup-plate", + "file:///srv/git/cleanup-plate.git", + "https://gitea.example.org/Sepci0/cleanup-plate.git", + "https://github.com/Sepci0", + "https://github.com/Sepci0/cleanup-plate/extra", + "", + ], +) +def test_other_urls_give_none(url: str) -> None: + assert slug_from_url(url) is None + + +def test_checkout_origin_is_read(tmp_path: Path) -> None: + repo = make_checkout(tmp_path / "todo", "https://github.com/kuhyx/todo-app.git") + assert origin_slug(repo) == "kuhyx/todo-app" + + +def test_no_checkout_or_no_origin_is_none(tmp_path: Path) -> None: + (tmp_path / "plain").mkdir() + assert origin_slug(tmp_path / "missing") is None + assert origin_slug(tmp_path / "plain") is None + assert origin_slug(make_checkout(tmp_path / "bare", None)) is None + + +def test_missing_or_hung_git_is_none( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo = make_checkout(tmp_path / "todo", "https://github.com/kuhyx/todo-app") + + def hang(*args: object, **kwargs: object) -> None: + msg = "git" + raise subprocess.TimeoutExpired(msg, _origin.GIT_TIMEOUT) + + monkeypatch.setattr(_origin.subprocess, "run", hang) + assert origin_slug(repo) is None + monkeypatch.setattr(_origin.shutil, "which", lambda _: None) + assert origin_slug(repo) is None + + +def test_repo_slug_falls_back_to_the_owner_guess(tmp_path: Path) -> None: + make_checkout(tmp_path / "todo", "git@github.com:kuhyx/todo-app.git") + assert repo_slug(tmp_path, "todo", "kuhyx") == "kuhyx/todo-app" + assert repo_slug(tmp_path, "museum", "kuhyx") == "kuhyx/museum" diff --git a/feedback_inbox/feedback_inbox/tests/test_support.py b/feedback_inbox/feedback_inbox/tests/test_support.py index 857bc0b..3fe48b6 100644 --- a/feedback_inbox/feedback_inbox/tests/test_support.py +++ b/feedback_inbox/feedback_inbox/tests/test_support.py @@ -6,6 +6,8 @@ from __future__ import annotations from dataclasses import dataclass, field import json import os +import shutil +import subprocess from typing import TYPE_CHECKING from feedback_inbox._todo import TaskRef, TodoError @@ -93,3 +95,21 @@ class FakeTodo: raise TodoError(msg) self.updated.append(task_id) self.stored[task_id] = f"{title}\n\n{body}" + + +GIT = shutil.which("git") or "git" + + +def make_checkout(path: Path, origin: str | None) -> Path: + """A real git repo at ``path`` (origin ``origin`` unless None), no real config.""" + env = { + "PATH": os.environ["PATH"], + "HOME": str(path.parent), + "GIT_CONFIG_NOSYSTEM": "1", + } + path.mkdir(parents=True) + subprocess.run([GIT, "init", "-q", str(path)], check=True, env=env) + if origin is not None: + git = [GIT, "-C", str(path), "remote", "add", "origin", origin] + subprocess.run(git, check=True, env=env) + return path