diff --git a/.github/block/README.md b/.github/block/README.md index 1652a1e79e..900337da38 100644 --- a/.github/block/README.md +++ b/.github/block/README.md @@ -1,6 +1,6 @@ # Block CI in the shared repository -The block module stays in `seaweed-block/` with its own `go.mod` and unchanged module path. Root `block-*.yml` workflows run shell commands there; action inputs name the subdirectory explicitly. The imported subtree is unchanged: every port file lives under root `.github/`. Existing module scripts and data remain base-owned inputs; only the Git adapters and unit-group coordinator live in root `.github/block/scripts/`. The existing nested workflows are inert because GitHub only discovers root workflows. +The block module stays in `seaweed-block/` with its own `go.mod` and unchanged module path. Root `block-*.yml` workflows run shell commands there; action inputs name the subdirectory explicitly. The imported subtree is unchanged: every port file lives under root `.github/`. Existing module scripts and data remain base-owned inputs; only the Git adapters, unit-group coordinator and relative documentation link check live in root `.github/block/scripts/`. The existing nested workflows are inert because GitHub only discovers root workflows. PR and master-push workflows select `seaweed-block/**` and their root block workflow files. All jobs default to `ubuntu-latest`. Set the repository variable `BLOCK_RUNNER_LABEL` only after the corresponding dedicated runner label is registered and available; same-repository/trusted events of the test, spec-trace and known-bugs jobs then use that label. The post-merge-review paths check and the release always run hosted. Fork PRs always use GitHub-hosted runners. GitHub Actions cannot fall back after requesting an unavailable label, so an unset variable is the explicit safe default. Unset it again if the registered pool is withdrawn. Runner discovery would require runner-management API access that the read-only workflow token does not have; no new credential is requested. diff --git a/.github/block/scripts/block-ci-port-test.py b/.github/block/scripts/block-ci-port-test.py index 36f0c68dc4..9efddc002b 100644 --- a/.github/block/scripts/block-ci-port-test.py +++ b/.github/block/scripts/block-ci-port-test.py @@ -18,8 +18,8 @@ class PortTests(unittest.TestCase): self.assertEqual(text.count(marker), 1) step = text.split(marker, 1)[1].split("\n - name:", 1)[0] self.assertIn("if: matrix.suite == 'unit'", step) - self.assertIn("python3 -B .github/scripts/doc-links-check-test.py", step) - self.assertIn("python3 -I .github/scripts/doc-links-check.py .", step) + self.assertIn("python3 -B ../.github/block/scripts/doc-links-check-test.py", step) + self.assertIn("python3 -I ../.github/block/scripts/doc-links-check.py .", step) self.assertNotIn("continue-on-error", step) self.assertNotIn("|| true", step) diff --git a/.github/block/scripts/doc-links-check-test.py b/.github/block/scripts/doc-links-check-test.py new file mode 100644 index 0000000000..911ecc0f90 --- /dev/null +++ b/.github/block/scripts/doc-links-check-test.py @@ -0,0 +1,71 @@ +"""Relative Markdown links must resolve; code, anchors and URLs are not paths.""" +import contextlib +import importlib.util +import io +from pathlib import Path +import tempfile +import unittest + +spec = importlib.util.spec_from_file_location("links", Path(__file__).with_name("doc-links-check.py")) +links = importlib.util.module_from_spec(spec) +spec.loader.exec_module(links) + + +class DocLinksTests(unittest.TestCase): + def check(self, files): + with tempfile.TemporaryDirectory() as root: + for name, text in files.items(): + path = Path(root, name) + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(text) + return [(source, target) for source, _, target in links.broken_links(root)] + + def test_existing_targets_pass(self): + broken = self.check({ + "README.md": "[guide](docs/guide.md#start) ![d](docs/d.svg) [dir](docs/) [ref][r]\n\n[r]: \n", + "docs/guide.md": "[up](../README.md) [root](/docs/d.svg) [esc](a%20b.md)\n", + "docs/d.svg": "", + "docs/a b.md": "", + }) + self.assertEqual(broken, []) + + def test_missing_targets_fail(self): + broken = self.check({ + "README.md": "[gone](docs/gone.md) [nested [x]](docs/x.md#a)\n\n[r]: missing.md\n", + "docs/guide.md": "[up](../nope.md)\n", + }) + self.assertCountEqual(broken, [ + ("README.md", "docs/gone.md"), + ("README.md", "docs/x.md#a"), + ("README.md", "missing.md"), + ("docs/guide.md", "../nope.md"), + ]) + + def test_urls_anchors_and_code_are_not_checked(self): + broken = self.check({ + "README.md": "\n".join([ + "[web](https://example.invalid/x) [mail](mailto:a@example.invalid) [top](#top) [proto](//host/x)", + "`[code](gone.md)` and ``[code](gone.md)``", + "```", + "[fenced](gone.md)", + "```", + "~~~md", + "[tilde](gone.md)", + "~~~", + ]) + "\n", + }) + self.assertEqual(broken, []) + + def test_main_exit_status(self): + out = io.StringIO() + with tempfile.TemporaryDirectory() as root, contextlib.redirect_stdout(out), contextlib.redirect_stderr(out): + Path(root, "README.md").write_text("[gone](gone.md)\n") + self.assertEqual(links.main(["doc-links-check.py", root]), 1) + Path(root, "gone.md").write_text("") + self.assertEqual(links.main(["doc-links-check.py", root]), 0) + self.assertEqual(links.main(["doc-links-check.py"]), 2) + self.assertIn("README.md:1: broken relative link gone.md", out.getvalue()) + + +if __name__ == "__main__": + unittest.main() diff --git a/.github/block/scripts/doc-links-check.py b/.github/block/scripts/doc-links-check.py new file mode 100644 index 0000000000..068832b1f4 --- /dev/null +++ b/.github/block/scripts/doc-links-check.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +"""Fail when a Markdown file links to a relative path that does not exist. + +Usage: doc-links-check.py +""" +import os +import re +import sys +import urllib.parse + +SKIP_DIRS = {".git", "node_modules", "vendor"} +FENCE = re.compile(r"^\s*(```|~~~)") +INLINE_CODE = re.compile(r"(`+)(?:(?!\1).)+?\1") +INLINE_LINK = re.compile(r"!?\[(?:[^\[\]]|\[[^\[\]]*\])*\]\(\s*(<[^>]*>|[^\s)]+)") +REFERENCE = re.compile(r"^\s{0,3}\[[^\]]+\]:\s*(<[^>]*>|\S+)") +SCHEME = re.compile(r"^[A-Za-z][A-Za-z0-9+.-]*:") + + +def link_targets(text): + fence = None + for lineno, line in enumerate(text.splitlines(), 1): + m = FENCE.match(line) + if m: + if fence is None: + fence = m.group(1) + elif m.group(1) == fence: + fence = None + continue + if fence is not None: + continue + line = INLINE_CODE.sub("", line) + for m in INLINE_LINK.finditer(line): + yield lineno, m.group(1) + m = REFERENCE.match(line) + if m: + yield lineno, m.group(1) + + +def local_path(target): + if target.startswith("<") and target.endswith(">"): + target = target[1:-1] + if not target or target.startswith(("#", "//")) or SCHEME.match(target): + return None + path = urllib.parse.unquote(target.split("#", 1)[0].split("?", 1)[0]) + return path or None + + +def broken_links(root): + root = os.path.abspath(root) + for dirpath, dirnames, filenames in os.walk(root): + dirnames[:] = sorted(d for d in dirnames if d not in SKIP_DIRS) + for name in sorted(filenames): + if not name.endswith(".md"): + continue + source = os.path.join(dirpath, name) + with open(source, encoding="utf-8", errors="replace") as f: + text = f.read() + for lineno, target in link_targets(text): + path = local_path(target) + if path is None: + continue + if path.startswith("/"): + resolved = os.path.join(root, path.lstrip("/")) + else: + resolved = os.path.join(dirpath, path) + if not os.path.exists(resolved): + yield os.path.relpath(source, root), lineno, target + + +def main(argv): + if len(argv) != 2: + print(__doc__.strip(), file=sys.stderr) + return 2 + broken = list(broken_links(argv[1])) + for source, lineno, target in broken: + print(f"{source}:{lineno}: broken relative link {target}") + return 1 if broken else 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv))