Coverage for packages/workshop/src/livery/workshop/_govern_entry.py: 83%

Measured by coverage.py 7.16.2 on CPython 3.14 while the suites of tests, packages/workshop/tests/test_workshop_isolated.py ran.

1"""Isolated Tier 1 entry point for the ``govern`` job.
2
3Invoked as ``python3 -I -m livery.workshop._govern_entry [--if-changed]``
4inside an isolated environment holding pinned ``livery-workshop`` alone.
5Never imports ``tasks.py``, never runs ``apply_cascade`` (never folds
6``.repo.env``), and never installs non-privileged workspace members.
7Reads ``workshop.toml``, ``packages/*/workshop.toml``, and ``CODEOWNERS``
8strictly as data and asserts repository settings through ``livery.forge``.
9"""
10
11from __future__ import annotations(empty)
12
13import argparse(empty)
14import os(empty)
15import sys(empty)
16from collections.abc import Sequence(empty)
17from pathlib import Path(empty)
18
19from livery.footman import Failed, fail(empty)
20from livery.workshop._contract import load_contract(empty)
21from livery.workshop._tokens import admin_token(empty)
22from livery.workshop._workflow_tasks import assert_configuration, contract_changed(empty)
23
24
25def _in_ci() -> bool:(empty)
26 """Whether this process runs inside a CI runner."""
27 return bool(
28 os.environ.get("GITHUB_ACTIONS")
29 or os.environ.get("GITEA_ACTIONS")
30 or os.environ.get("GITLAB_CI")
31 )
32
33
34def run_govern((empty)
35 root: Path,
36 *,
37 if_changed: bool = False,
38 in_ci: bool | None = None,
39) -> None:
40 """Reconcile repository settings from *root*'s contracts in isolation.
41
42 In CI, refuses to fall back to ``FORGE_TOKEN``: ``FORGE_ADMIN_TOKEN``
43 (or its host-qualified form) must be present in the ``govern``
44 environment. Outside CI, falls back to the normal admin ladder.
45 """
46 if if_changed:
47 changed = contract_changed(root)(empty)
48 if not changed:48 ↛ 51line 48 didn't jump to line 51 because the condition on line 48 was always true(empty)
49 print(" no contract or owners path changed in HEAD; nothing to reconcile")(empty)
50 return(empty)
51 print(f" governed paths changed: {', '.join(changed)}")
52
53 running_in_ci = _in_ci() if in_ci is None else in_ci
54 if running_in_ci:54 ↛ 65line 54 didn't jump to line 65 because the condition on line 54 was always true
55 contract = load_contract(root / "workshop.toml")
56 forge_table = contract.get("forge") or {}
57 kind = str(forge_table.get("kind", ""))
58 url = str(forge_table.get("url", ""))
59 token, var = admin_token(kind, url)
60 if not token or not var.startswith("FORGE_ADMIN_TOKEN"):60 ↛ 65line 60 didn't jump to line 65 because the condition on line 60 was always true
61 fail(
62 "isolated govern refuses to fall back to FORGE_TOKEN in CI:"
63 " set FORGE_ADMIN_TOKEN in the govern environment"
64 )
65 assert_configuration(root)
66
67
68def main(argv: Sequence[str] | None = None) -> int:(empty)
69 """CLI entry point for ``python3 -I -m livery.workshop._govern_entry``."""
70 parser = argparse.ArgumentParser((empty)
71 prog="livery.workshop._govern_entry",
72 description=(
73 "Reconcile forge repository settings without entering the workspace."
74 ),
75 )
76 parser.add_argument((empty)
77 "--root",
78 default=".",
79 help="workspace root directory (default: current directory)",
80 )
81 parser.add_argument((empty)
82 "--if-changed",
83 action="store_true",
84 help="reconcile only when HEAD changed a governed contract or owners file",
85 )
86 parsed = parser.parse_args(list(argv) if argv is not None else None)(empty)
87 try:(empty)
88 run_govern(Path(parsed.root).resolve(), if_changed=parsed.if_changed)(empty)
89 except Failed as error:
90 print(f"govern: {error}", file=sys.stderr)
91 return 1
92 return 0(empty)
93
94
95if __name__ == "__main__":(empty)
96 raise SystemExit(main())(empty)