Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(owners): add --repo-root to the JSON resolver entrypoint (#97411)

Julian Bezea32cc91eded
diff --git a/tools/owners/README.md b/tools/owners/README.md
index 4194ffc9f202..eaab450b07d0 100644
--- a/tools/owners/README.md
+++ b/tools/owners/README.md
@@ -13,3 +13,30 @@ uvx --from "git+https://github.com/PostHog/posthog#subdirectory=tools/owners" ow
```
Pin to a commit for CI so the resolver semantics can't shift under you: append `@<sha>` to the URL (`...posthog@<sha>#subdirectory=tools/owners`).
+
+### From a consumer that is not Python
+
+`python -m posthog_owners` answers the same question as JSON, with no click and no project sync. Stdlib plus pyyaml is enough.
+It reads repo-relative paths from stdin or argv and writes a single JSON object keyed by path:
+
+```bash
+echo "posthog/models/team.py" | PYTHONPATH=/fetched/tools/owners python3 -m posthog_owners --repo-root /fetched
+```
+
+`PYTHONPATH` names the directory holding the `posthog_owners` package. `--repo-root` names the tree holding the ownership files; the example fetched both into one scratch directory, but they are independent.
+Any Python 3.10+ interpreter with pyyaml works, so `uv run --no-project --with pyyaml python` is enough if you would rather not install it.
+
+```json
+{
+ "posthog/models/team.py": {
+ "owners": ["team-x"],
+ "status": "active",
+ "slack": "#team-x",
+ "source": "posthog/owners.yaml"
+ }
+}
+```
+
+Without `--repo-root` the resolver locates the repo with `git rev-parse`, so it needs a real worktree.
+Pass the flag when the ownership files sit in a directory that is not one: a sparse fetch, an export, or a scratch copy of just the `owners.yaml` / `product.yaml` files.
+Add `--purpose notifications` to resolve `slack` to each team's automation channel instead of its people channel.
diff --git a/tools/owners/posthog_owners/__main__.py b/tools/owners/posthog_owners/__main__.py
index 2ab4da2ae99f..f87f165f8001 100644
--- a/tools/owners/posthog_owners/__main__.py
+++ b/tools/owners/posthog_owners/__main__.py
@@ -8,6 +8,11 @@ JSON object keyed by normalized path to stdout::
``--purpose notifications`` resolves ``slack`` to the team's automation channel
(falls back to the people channel); the default is the people channel.
+``--repo-root`` names the directory holding the ownership files. Without it the
+resolver locates the repo with ``git rev-parse``, which needs a real worktree; a
+consumer that fetched only the ``owners.yaml`` / ``product.yaml`` files into a
+scratch directory passes the flag instead.
+
Kept off click on purpose (stdlib + pyyaml only) so a workflow can run it with
``python -m posthog_owners`` after installing just pyyaml — no hogli, no
project sync. The click CLI (``hogli owners:resolve --json``) emits the identical
@@ -19,6 +24,7 @@ from __future__ import annotations
import sys
import json
import argparse
+from pathlib import Path
from typing import cast
from .matcher import normalize_path
@@ -28,11 +34,22 @@ from .resolver import DEFAULT_PURPOSE, OwnersResolver, Purpose, read_stdin_paths
def main() -> None:
parser = argparse.ArgumentParser(prog="python -m posthog_owners")
parser.add_argument("--purpose", choices=["slack", "notifications"], default=DEFAULT_PURPOSE)
+ parser.add_argument(
+ "--repo-root",
+ default=None,
+ help="Directory holding the ownership files; default: the enclosing git worktree",
+ )
parser.add_argument("paths", nargs="*")
ns = parser.parse_args()
+ # A root that is not a directory reads as a repo with no ownership files, so every path
+ # answers unowned. An empty value is Path("."), which an unset "$VAR" would resolve
+ # against the working directory instead of failing, so reject it before the conversion.
+ if ns.repo_root is not None and not (ns.repo_root and Path(ns.repo_root).is_dir()):
+ parser.error(f"--repo-root {ns.repo_root!r} is not a directory")
+ repo_root = Path(ns.repo_root) if ns.repo_root is not None else None
paths = ns.paths or read_stdin_paths()
- resolver = OwnersResolver(purpose=cast("Purpose", ns.purpose))
+ resolver = OwnersResolver(repo_root=repo_root, purpose=cast("Purpose", ns.purpose))
result = {normalize_path(path): resolution_to_wire(resolver.resolve(path)) for path in paths}
json.dump(result, sys.stdout)
diff --git a/tools/owners/tests/test_owners.py b/tools/owners/tests/test_owners.py
index eccd6fa002d9..64e447c7ed5c 100644
--- a/tools/owners/tests/test_owners.py
+++ b/tools/owners/tests/test_owners.py
@@ -1,5 +1,7 @@
from __future__ import annotations
+import sys
+import json
import subprocess
from pathlib import Path
@@ -755,3 +757,57 @@ def test_first_team_owner_skips_handles() -> None:
assert first_team_owner(["@someone", "team-a"]) == "team-a"
assert first_team_owner(["@someone"]) == ""
assert first_team_owner(None) == ""
+
+
+def _run_entrypoint(repo: Path, *args: str, stdin: str = "") -> subprocess.CompletedProcess[str]:
+ return subprocess.run(
+ [sys.executable, "-m", "posthog_owners", *args],
+ cwd=repo,
+ input=stdin,
+ capture_output=True,
+ text=True,
+ )
+
+
+def test_json_entrypoint_resolves_against_an_explicit_repo_root(registry_repo: Path) -> None:
+ # The fixture must have no .git, or the git rev-parse default could answer instead of the flag.
+ assert not (registry_repo / ".git").exists()
+
+ result = _run_entrypoint(registry_repo, "--repo-root", str(registry_repo), "reg/x.py")
+
+ assert result.returncode == 0, result.stderr
+ assert json.loads(result.stdout) == {
+ "reg/x.py": {
+ "owners": ["team-registry"],
+ "status": "active",
+ "slack": "#registry-chan",
+ "source": "reg/owners.yaml",
+ }
+ }
+
+
+def test_json_entrypoint_repo_root_reads_stdin_paths_and_honors_purpose(registry_repo: Path) -> None:
+ result = _run_entrypoint(
+ registry_repo,
+ "--repo-root",
+ str(registry_repo),
+ "--purpose",
+ "notifications",
+ stdin="split/x.py\nderive/x.py\n",
+ )
+
+ assert result.returncode == 0, result.stderr
+ wire = json.loads(result.stdout)
+ assert wire["split/x.py"]["slack"] == "#split-bots"
+ assert wire["derive/x.py"]["slack"] == "#team-nonreg"
+
+
[email protected]("root", ["nope", ""], ids=["missing", "empty"])
+def test_json_entrypoint_rejects_a_repo_root_that_is_not_a_directory(registry_repo: Path, root: str) -> None:
+ # An empty root is the unset "$VAR" case: Path("") is Path("."), which would
+ # otherwise resolve against the working directory rather than fail.
+ result = _run_entrypoint(registry_repo, "--repo-root", str(registry_repo / root) if root else "", "reg/x.py")
+
+ assert result.returncode == 2
+ assert "--repo-root" in result.stderr
+ assert result.stdout == ""