diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..b379900 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @WillieCubed diff --git a/.hypertext/formatters.json b/.hypertext/formatters.json index ffaf5c3..43ffb83 100644 --- a/.hypertext/formatters.json +++ b/.hypertext/formatters.json @@ -69,6 +69,11 @@ "kind": "non-formattable", "reason": "Exact license text is legal material, not prose to reflow." }, + { + "include": [".github/CODEOWNERS"], + "kind": "non-formattable", + "reason": "Line-oriented ownership patterns." + }, { "include": [".gitignore"], "kind": "non-formattable", diff --git a/README.md b/README.md index 532eaa2..7401835 100644 --- a/README.md +++ b/README.md @@ -85,12 +85,18 @@ is independent of candidate/promotion workflows and never deploys a website. ## Local commit formatting -Prepare the pinned formatting tools and local Git hooks from this repository's root: +Maintainers prepare the pinned formatting tools and local Git hooks from this +repository's root. Contributors need neither; their pull request checks decide +formatting. [The development guide](docs/DEVELOPMENT.md) describes both paths. ```sh -python3 .hypertext/formatting/install.py +python3 scripts/setup.py ``` Repeat this command after updating formatter pins or creating a checkout. Commits format staged source and configuration while retaining unstaged edits. Setup prepares dependencies; committing never downloads tools or starts build services. + +## License + +This repository is licensed under the [MIT License](LICENSE). diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md new file mode 100644 index 0000000..0034c85 --- /dev/null +++ b/docs/DEVELOPMENT.md @@ -0,0 +1,34 @@ +# Development + +## Quickstart + +These commands build and test a fresh clone with no accounts, sign-ins or +package tokens. + +Install Python 3.11 or newer (the shared workflows use 3.14.5) and a Swift 6.0 +or newer toolchain from Xcode on macOS 13 or newer. The native Sparkle test +generates an Ed25519 key, which the LibreSSL that ships with macOS cannot do, so +also install OpenSSL 3 or newer (`brew install openssl`) and put its `openssl` first on +`PATH`. Then, from the repository root: + +```sh +python3 scripts/setup.py +swift build --jobs 2 +swift test --jobs 2 +.venv/bin/python -m unittest discover -s tests +.venv/bin/python scripts/build-release.py +``` + +`scripts/setup.py` creates `.venv`, installs the Python package into it and +resolves the Swift package dependencies that the native Sparkle test reads. It +skips the maintainers' commit hooks and formatters and prints each step it +skipped. The pull request checks decide formatting and commit messages for +outside contributions. + +## Maintainers + +Setup decides by the effective Git committer email. A maintainer runs +`git config --local user.email YOU@hypertext.studio` and then +`python3 scripts/setup.py` again. Setup then also installs the commit hooks and +the pinned formatters, which download `@thehypertextstudio/format-config` from +GitHub Packages with the maintainer's GitHub sign-in. diff --git a/scripts/setup.py b/scripts/setup.py new file mode 100644 index 0000000..8ee3b45 --- /dev/null +++ b/scripts/setup.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 +"""Prepare a checkout for development. + +Every checkout gets the Python environment and the Swift package dependencies +that the tests need. A checkout whose effective committer email is at +@hypertext.studio also gets the commit hooks and pinned formatters, because +installing the formatters needs Studio package access. Any other checkout +skips those steps and the report names each one. +""" + +import argparse +from pathlib import Path +import re +import subprocess +import sys + +ROOT = Path(__file__).resolve().parents[1] +DETECT = object() +STUDIO_EMAIL = re.compile(r"[^@\s]+@hypertext\.studio", re.I | re.ASCII) + + +def committer_email(root): + """Return the email Git would record for a commit, or None when it has none.""" + result = subprocess.run( + ["git", "-C", str(root), "var", "GIT_COMMITTER_IDENT"], + capture_output=True, + text=True, + check=False, + ) + match = re.search(r"<([^<>]+)>", result.stdout) if result.returncode == 0 else None + return match.group(1) if match else None + + +def is_maintainer(email): + return bool(email and STUDIO_EMAIL.fullmatch(email)) + + +def venv_python(root): + return root / ".venv/bin/python" + + +def steps(root, python=sys.executable): + """Each step is (name, argv, needs_studio_access).""" + return [ + ("Python environment", [python, "-m", "venv", str(root / ".venv")], False), + ( + "Python dependencies", + [str(venv_python(root)), "-m", "pip", "install", str(root)], + False, + ), + ("Swift package dependencies", ["swift", "package", "resolve"], False), + ( + "Commit hooks and pinned formatters", + [python, str(root / ".hypertext/formatting/install.py")], + True, + ), + ] + + +def run(root=ROOT, email=DETECT, runner=subprocess.run, out=print): + root = Path(root) + email = committer_email(root) if email is DETECT else email + maintainer = is_maintainer(email) + skipped = [] + for name, argv, needs_studio_access in steps(root): + if needs_studio_access and not maintainer: + skipped.append(name) + continue + out("Running: " + name) + if runner(argv, cwd=root).returncode: + out("Failed: " + name) + return 1 + if skipped: + out( + ( + "Committer " + email + " is outside @hypertext.studio" + if email + else "Git has no committer email for this checkout" + ) + + ", so setup took the contributor path." + ) + for name in skipped: + out("Skipped (needs Studio package access): " + name) + out("Before pushing, run the build and test commands in docs/DEVELOPMENT.md.") + out("The pull request checks decide formatting and commit messages.") + out( + "Maintainers: run git config --local user.email YOUR@hypertext.studio, then run setup again." + ) + return 0 + + +if __name__ == "__main__": + argparse.ArgumentParser(description=__doc__).parse_args() + raise SystemExit(run()) diff --git a/tests/test_setup.py b/tests/test_setup.py new file mode 100644 index 0000000..fef207d --- /dev/null +++ b/tests/test_setup.py @@ -0,0 +1,133 @@ +import importlib.util +import pathlib +import subprocess +import tempfile +import unittest +from types import SimpleNamespace + +ROOT = pathlib.Path(__file__).resolve().parents[1] +spec = importlib.util.spec_from_file_location( + "checkout_setup", ROOT / "scripts/setup.py" +) +setup = importlib.util.module_from_spec(spec) +spec.loader.exec_module(setup) + + +class Recorder: + def __init__(self, failing=None): + self.calls = [] + self.failing = failing + + def __call__(self, argv, cwd=None): + self.calls.append(argv) + return SimpleNamespace(returncode=1 if argv[0] == self.failing else 0) + + +def run(email, runner=None): + runner = runner or Recorder() + lines = [] + code = setup.run(ROOT, email=email, runner=runner, out=lines.append) + return code, runner.calls, lines + + +def formatter_runs(calls): + return [ + c for c in calls if any(part.endswith("formatting/install.py") for part in c) + ] + + +class MaintainerDecisionTests(unittest.TestCase): + def test_studio_addresses_are_maintainers(self): + self.assertTrue(setup.is_maintainer("willie@hypertext.studio")) + self.assertTrue(setup.is_maintainer("Someone@Hypertext.Studio")) + + def test_other_addresses_are_not_maintainers(self): + for email in ( + "dev@example.com", + "x@hypertext.studio.evil.com", + "x@evil-hypertext.studio", + None, + "", + ): + self.assertFalse(setup.is_maintainer(email), email) + + def test_committer_email_reads_the_identity_git_would_record(self): + with tempfile.TemporaryDirectory() as folder: + subprocess.run(["git", "init", "--quiet", folder], check=True) + subprocess.run( + ["git", "-C", folder, "config", "--local", "user.name", "Dev"], + check=True, + ) + subprocess.run( + [ + "git", + "-C", + folder, + "config", + "--local", + "user.email", + "dev@example.com", + ], + check=True, + ) + self.assertEqual("dev@example.com", setup.committer_email(folder)) + + +class ContributorPathTests(unittest.TestCase): + def test_contributor_prepares_the_native_toolchain_and_skips_studio_steps(self): + code, calls, lines = run("dev@example.com") + self.assertEqual(0, code) + self.assertEqual([], formatter_runs(calls)) + self.assertIn(["swift", "package", "resolve"], calls) + self.assertEqual(3, len(calls)) + self.assertIn( + "Skipped (needs Studio package access): Commit hooks and pinned formatters", + lines, + ) + + def test_report_names_the_reason_and_the_next_steps(self): + _, _, lines = run("dev@example.com") + text = "\n".join(lines) + self.assertIn("Committer dev@example.com is outside @hypertext.studio", text) + self.assertIn("docs/DEVELOPMENT.md", text) + self.assertIn("pull request checks decide", text) + self.assertIn("git config --local user.email YOUR@hypertext.studio", text) + + def test_missing_identity_takes_the_contributor_path(self): + code, calls, lines = run(None) + self.assertEqual(0, code) + self.assertEqual([], formatter_runs(calls)) + self.assertIn( + "Git has no committer email for this checkout, so setup took the contributor path.", + lines, + ) + + def test_contributor_path_never_asks_for_a_token_or_exception(self): + _, calls, lines = run("dev@example.com") + text = " ".join(" ".join(c) for c in calls) + " ".join(lines) + for forbidden in ( + "NODE_AUTH_TOKEN", + "HYPERTEXT_ALLOW_NON_STUDIO_EMAIL", + "gh auth", + "gcloud", + ): + self.assertNotIn(forbidden, text) + + +class MaintainerPathTests(unittest.TestCase): + def test_maintainer_also_installs_hooks_and_formatters(self): + code, calls, lines = run("willie@hypertext.studio") + self.assertEqual(0, code) + self.assertEqual(1, len(formatter_runs(calls))) + self.assertEqual(4, len(calls)) + self.assertFalse([line for line in lines if line.startswith("Skipped")]) + + def test_failed_step_stops_setup(self): + code, calls, lines = run("willie@hypertext.studio", Recorder(failing="swift")) + self.assertEqual(1, code) + self.assertEqual([], formatter_runs(calls)) + self.assertIn("Failed: Swift package dependencies", lines) + + +if __name__ == "__main__": + unittest.main()