-
-
Notifications
You must be signed in to change notification settings - Fork 4
feat(ci): validate embed source_page references before build (Phase 4) #641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+138
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
62ed327
feat(ci): validate embed source_page references before build
jmservera e64ae1e
fix: address #641 review — require data page target, use python
jmservera a40cc75
fix: use Path default for --root in embed source check (#641 review)
jmservera d2996ea
Merge main (#642) into #641
jmservera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| #!/usr/bin/env python3 | ||
| """Validate that every embed page's source_page resolves to an existing data page. | ||
|
|
||
| A `content/embeds/*` page renders a chart from another page referenced by its | ||
| `source_page` front matter. If that target page is missing, the Hugo build aborts | ||
| (`layouts/embeds/single.html` and `layouts/shortcodes/observatory-chart.html` | ||
| call `errorf`). This check catches a dangling reference before the build, so the | ||
| failure surfaces in CI rather than in the production deploy (see issue #627). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import re | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| ROOT = Path(__file__).resolve().parent.parent | ||
|
|
||
| SOURCE_PAGE_PATTERN = re.compile( | ||
| r"""^\s*source_page\s*[:=]\s*["']?(?P<value>[^"'\n]+?)["']?\s*$""", | ||
| re.MULTILINE, | ||
| ) | ||
|
|
||
|
|
||
| def _source_page(document: str) -> str | None: | ||
| match = SOURCE_PAGE_PATTERN.search(document) | ||
| return match.group("value").strip() if match else None | ||
|
|
||
|
|
||
| def _resolves(content_root: Path, source_page: str) -> bool: | ||
| relative = source_page.strip("/") | ||
| # NFR-012: an embed's source_page must be a data page under content/data/. | ||
| if relative != "data" and not relative.startswith("data/"): | ||
| return False | ||
| target = content_root / relative | ||
| return (target / "index.md").is_file() or (target / "_index.md").is_file() | ||
|
|
||
|
|
||
| def check_embed_sources(root: Path = ROOT) -> list[str]: | ||
| """Return a list of human-readable problems, empty when all embeds resolve.""" | ||
| content_root = root / "content" | ||
| problems: list[str] = [] | ||
|
|
||
| for embed in sorted(content_root.glob("embeds/*/index.md")): | ||
| source_page = _source_page(embed.read_text(encoding="utf-8")) | ||
| relative_embed = embed.relative_to(root).as_posix() | ||
| if not source_page: | ||
| problems.append(f"{relative_embed}: missing source_page") | ||
| continue | ||
| if not _resolves(content_root, source_page): | ||
| problems.append( | ||
| f"{relative_embed}: source_page does not resolve to a content/data page: " | ||
| f"{source_page}" | ||
| ) | ||
|
|
||
| return problems | ||
|
|
||
|
|
||
| def main(argv: list[str] | None = None) -> int: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("--root", default=ROOT, type=Path, help="Repository root") | ||
| args = parser.parse_args(argv) | ||
|
|
||
| problems = check_embed_sources(Path(args.root)) | ||
| for problem in problems: | ||
| print(f"::error::{problem}", file=sys.stderr) | ||
| if problems: | ||
| return 1 | ||
| print("All embed source_page references resolve.") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
| from scripts.check_embed_sources import check_embed_sources | ||
|
|
||
| ROOT = Path(__file__).resolve().parent.parent | ||
|
|
||
|
|
||
| def test_repository_embed_sources_all_resolve() -> None: | ||
| assert check_embed_sources(ROOT) == [] | ||
|
|
||
|
|
||
| def _write(path: Path, text: str) -> None: | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| path.write_text(text, encoding="utf-8") | ||
|
|
||
|
|
||
| def test_missing_source_page_is_reported() -> None: | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| root = Path(tmp) | ||
| _write(root / "content/embeds/chart/index.md", '+++\ntitle = "Chart"\n+++\n') | ||
| problems = check_embed_sources(root) | ||
| assert any("missing source_page" in problem for problem in problems) | ||
|
|
||
|
|
||
| def test_dangling_source_page_is_reported() -> None: | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| root = Path(tmp) | ||
| _write( | ||
| root / "content/embeds/chart/index.md", | ||
| '+++\nsource_page = "/data/does-not-exist/"\n+++\n', | ||
| ) | ||
| problems = check_embed_sources(root) | ||
| assert any("does not resolve" in problem for problem in problems) | ||
|
|
||
|
|
||
| def test_resolving_source_page_passes() -> None: | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| root = Path(tmp) | ||
| _write( | ||
| root / "content/embeds/chart/index.md", | ||
| '+++\nsource_page = "/data/live/"\n+++\n', | ||
| ) | ||
| _write(root / "content/data/live/index.md", '+++\ntitle = "Live"\n+++\n') | ||
| assert check_embed_sources(root) == [] | ||
|
|
||
|
|
||
| def test_non_data_source_page_is_reported() -> None: | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| root = Path(tmp) | ||
| _write( | ||
| root / "content/embeds/chart/index.md", | ||
| '+++\nsource_page = "/weekly/2026/w22/"\n+++\n', | ||
| ) | ||
| # The referenced page exists, but it is not a content/data page. | ||
| _write(root / "content/weekly/2026/w22/index.md", '+++\ntitle = "W22"\n+++\n') | ||
| problems = check_embed_sources(root) | ||
| assert any("does not resolve to a content/data page" in problem for problem in problems) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.