Add a fast repository hygiene check

Add hygiene.yml and scripts/audit/docs_hygiene.py: fail on merge-conflict
markers, broken relative Markdown links and invalid workflow YAML, and
warn (advisory) when source changes lack a changelog entry.

Signed-off-by: Arths17 <Atharvranjan8@gmail.com>
This commit is contained in:
atharvranjan committed 2026-11-25 04:24:36 -06:00
1 parent b71db3c25a
commit 5c0c82b3f9
3 files changed
+160

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
name: Repository hygiene
# Fast checks on tracked files that need no Rust toolchain and no network:
# merge-conflict markers, broken relative Markdown links and invalid workflow
# YAML (scripts/audit/docs_hygiene.py). A second, advisory step warns when
# source or script changes arrive without a CHANGELOG.md update; it never fails.
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
concurrency:
group: hygiene-${{ github.ref }}
cancel-in-progress: true
jobs:
hygiene:
name: Repository hygiene
runs-on: ubuntu-24.04-arm
timeout-minutes: 5
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
fetch-depth: 0
- name: Install PyYAML
run: python3 -c 'import yaml' 2>/dev/null || python3 -m pip install --user --break-system-packages pyyaml
- name: Check documentation and workflow files
run: python3 scripts/audit/docs_hygiene.py --require-yaml
- name: Remind about the changelog (advisory)
if: github.event_name == 'pull_request'
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
run: |
changed="$(git diff --name-only "$BASE_SHA"...HEAD || true)"
if printf '%s\n' "$changed" | grep -Eq '^(crates|scripts|shaders|fixtures)/' \
&& ! printf '%s\n' "$changed" | grep -qx 'CHANGELOG.md'; then
echo "::warning title=Changelog::Source or script files changed without a CHANGELOG.md update."
fi
+3
View File
@@ -9,6 +9,9 @@ exact revision and known limitations.
Guest execution uses original, purpose-built test programs. Commercial Xbox game execution
remains unsupported.
### Added
- Added a fast "Repository hygiene" check (`.github/workflows/hygiene.yml`, `scripts/audit/docs_hygiene.py`). It runs on Ubuntu without a Rust toolchain or network access and fails only on merge-conflict markers, broken relative Markdown links and invalid workflow YAML in tracked files. A separate advisory step warns, without failing, when source or script changes have no changelog entry. A local run over the 626 tracked files found no problems, and planted failures of each kind were caught; it has not yet run on GitHub.
### Removed
- Removed the "PR template" check (`.github/workflows/pr-template.yml`, `scripts/pr_template_check.py` and its tests) and its CI test step. Also removed `.github/PULL_REQUEST_TEMPLATE.md`. Pull request descriptions are no longer validated or pre-filled; CONTRIBUTING.md still asks for the same information and the claim-first rule still applies.
+114
View File
@@ -0,0 +1,114 @@
#!/usr/bin/env python3
"""Fast, deterministic documentation and repository hygiene checks.
Checks only tracked files:
1. no merge-conflict markers in tracked text files;
2. every relative Markdown link points at a tracked file or directory;
3. every workflow file under .github/workflows parses as YAML.
External URLs and in-page anchors are not fetched or validated, so the result
does not depend on the network. Exit status is 1 when a problem is found. Pass --require-yaml to fail, not skip,
when PyYAML is missing.
"""
import re
import subprocess
import sys
from pathlib import Path, PurePosixPath
from urllib.parse import unquote
ROOT = Path(__file__).resolve().parents[2]
MARKER = re.compile(r"^(<{7}( |$)|={7}$|>{7}( |$))")
LINK = re.compile(r"(?<!\!)\[[^\]]*\]\(([^)\s]+)(?:\s+\"[^\"]*\")?\)")
FENCE = re.compile(r"^\s*(```|~~~)")
def tracked():
out = subprocess.run(["git", "ls-files", "-z"], cwd=ROOT, check=True,
capture_output=True).stdout.decode("utf-8", "replace")
return [p for p in out.split("\0") if p and (ROOT / p).is_file()]
def text(path):
data = (ROOT / path).read_bytes()
if b"\0" in data:
return None
return data.decode("utf-8", "replace")
def check_markers(files, problems):
for path in files:
if path.endswith((".patch", ".diff")):
continue
body = text(path)
if body is None:
continue
for number, line in enumerate(body.splitlines(), 1):
if MARKER.match(line):
problems.append(f"{path}:{number}: merge conflict marker")
def check_links(files, problems):
known = set(files)
dirs = {str(d) for f in files for d in PurePosixPath(f).parents}
for path in files:
if not path.endswith(".md") or path.startswith(("artifacts/", "third_party/")):
continue
in_fence = False
for number, line in enumerate((text(path) or "").splitlines(), 1):
if FENCE.match(line):
in_fence = not in_fence
continue
if in_fence:
continue
line = re.sub(r"`[^`]*`", "", line)
for target in LINK.findall(line):
if re.match(r"^([a-z][a-z0-9+.-]*:|#|//)", target, re.I):
continue
target = unquote(target.split("#", 1)[0].split("?", 1)[0])
if not target:
continue
base = PurePosixPath(path).parent
resolved = PurePosixPath(target.lstrip("/")) if target.startswith("/") else base / target
parts = []
for part in resolved.parts:
if part == "..":
if parts:
parts.pop()
elif part != ".":
parts.append(part)
norm = "/".join(parts)
if norm and norm not in known and norm not in dirs:
problems.append(f"{path}:{number}: broken relative link '{target}'")
def check_workflows(files, problems, require_yaml):
try:
import yaml
except ImportError:
if require_yaml:
problems.append("PyYAML is not installed but --require-yaml was given")
else:
print("note: PyYAML not installed; workflow YAML parse check skipped", file=sys.stderr)
return
for path in files:
if path.startswith(".github/workflows/") and path.endswith((".yml", ".yaml")):
try:
yaml.safe_load((ROOT / path).read_text(encoding="utf-8"))
except yaml.YAMLError as err:
problems.append(f"{path}: invalid YAML: {str(err).splitlines()[0]}")
def main():
files = tracked()
problems = []
check_markers(files, problems)
check_links(files, problems)
check_workflows(files, problems, "--require-yaml" in sys.argv[1:])
for problem in problems:
print(problem)
print(f"docs hygiene: {len(files)} tracked files checked, {len(problems)} problem(s)")
return 1 if problems else 0
if __name__ == "__main__":
sys.exit(main())