diff --git a/.gitea/workflows/main.yml b/.gitea/workflows/main.yml new file mode 100644 index 0000000..f74ac8b --- /dev/null +++ b/.gitea/workflows/main.yml @@ -0,0 +1,32 @@ +name: main-site-gate + +on: + push: + branches: [main] + +concurrency: + group: alkane-benchmarks-main + cancel-in-progress: false + +jobs: + validate-and-release: + runs-on: alkane-ci + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Test site and version guards + run: python3 -m unittest discover -s ci -p 'test_*.py' + - name: Validate published Pages snapshot + run: python3 ci/check_published.py + - name: Select release version + id: version + run: python3 ci/version.py main >> "$GITHUB_OUTPUT" + - name: Release validated site configuration + if: steps.version.outputs.release == 'true' + env: + GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: python3 ci/release.py "${{ steps.version.outputs.tag }}" main + diff --git a/.gitea/workflows/next.yml b/.gitea/workflows/next.yml new file mode 100644 index 0000000..7369449 --- /dev/null +++ b/.gitea/workflows/next.yml @@ -0,0 +1,29 @@ +name: next-site-candidate + +on: + push: + branches: [next] + +concurrency: + group: alkane-benchmarks-next + cancel-in-progress: false + +jobs: + validate-and-candidate: + runs-on: alkane-ci + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Validate published Pages snapshot + run: python3 ci/check_published.py + - name: Select candidate version + id: version + run: python3 ci/version.py next >> "$GITHUB_OUTPUT" + - name: Release candidate + if: steps.version.outputs.release == 'true' + env: + GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: python3 ci/release.py "${{ steps.version.outputs.tag }}" next diff --git a/README.md b/README.md index bf0ca7f..f84f52b 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,16 @@ These are host microbenchmarks. They do not show miner board performance or rele ## Branches -`main` holds this description. `next` is reserved for site changes before -they reach `main`. The `gh-pages` branch is generated output. +`main` validates the public Pages snapshot and runs the site guard tests. +`next` validates the snapshot before making a release candidate. The +`gh-pages` branch contains generated output only. Neither workflow copies +source repositories into Pages. + +CI creates `vX.Y.Z` releases from passing `main` runs and `-rc.N` releases +from passing `next` runs. Edit `ci/version-x.txt` to change X. Changes to +the site validator increment Y; documentation and CI fixes increment Z. +The release tags identify site configuration, while benchmark measurements +continue to update on `gh-pages`. ## License diff --git a/ci/__pycache__/test_validate_site.cpython-313.pyc b/ci/__pycache__/test_validate_site.cpython-313.pyc new file mode 100644 index 0000000..2c730a5 Binary files /dev/null and b/ci/__pycache__/test_validate_site.cpython-313.pyc differ diff --git a/ci/__pycache__/test_version.cpython-313.pyc b/ci/__pycache__/test_version.cpython-313.pyc new file mode 100644 index 0000000..f5ac732 Binary files /dev/null and b/ci/__pycache__/test_version.cpython-313.pyc differ diff --git a/ci/__pycache__/validate_site.cpython-313.pyc b/ci/__pycache__/validate_site.cpython-313.pyc new file mode 100644 index 0000000..87e7e04 Binary files /dev/null and b/ci/__pycache__/validate_site.cpython-313.pyc differ diff --git a/ci/__pycache__/version.cpython-313.pyc b/ci/__pycache__/version.cpython-313.pyc new file mode 100644 index 0000000..2d235f7 Binary files /dev/null and b/ci/__pycache__/version.cpython-313.pyc differ diff --git a/ci/check_published.py b/ci/check_published.py new file mode 100644 index 0000000..e7d555e --- /dev/null +++ b/ci/check_published.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +"""Validate the current public Pages branch in a disposable directory.""" + +import io +from pathlib import Path +import subprocess +import tarfile +from tempfile import TemporaryDirectory + +from validate_site import validate + + +def main() -> int: + subprocess.run(["git", "fetch", "--quiet", "origin", "gh-pages"], check=True) + archive = subprocess.check_output(["git", "archive", "FETCH_HEAD"]) + with TemporaryDirectory(prefix="alkane-benchmarks-site-") as directory: + with tarfile.open(fileobj=io.BytesIO(archive)) as source: + source.extractall(directory, filter="data") + errors = validate(Path(directory)) + for error in errors: + print(error) + if errors: + return 1 + print("Published benchmark pages are data-only and structurally valid") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/ci/release.py b/ci/release.py new file mode 100644 index 0000000..1ede7a8 --- /dev/null +++ b/ci/release.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +"""Create a scoped Gitea release for validated site configuration.""" + +import json +import os +import sys +import urllib.error +import urllib.request + + +def main() -> int: + if len(sys.argv) != 3: + raise SystemExit("usage: release.py TAG main|next") + tag, branch = sys.argv[1:] + if branch not in {"main", "next"}: + raise SystemExit("invalid branch") + token = os.environ["GITEA_TOKEN"] + api = os.environ.get("GITHUB_API_URL", "https://gitea.bluefla.re/api/v1").rstrip("/") + repo = os.environ["GITHUB_REPOSITORY"] + sha = os.environ["GITHUB_SHA"] + payload = json.dumps({ + "tag_name": tag, + "target_commitish": sha, + "name": tag, + "body": f"Validated public benchmark site configuration from {sha}.", + "prerelease": branch == "next", + }).encode() + request = urllib.request.Request( + f"{api}/repos/{repo}/releases", data=payload, method="POST", + headers={"Authorization": "token " + token, "Content-Type": "application/json"}, + ) + try: + with urllib.request.urlopen(request, timeout=60) as response: + result = json.load(response) + except urllib.error.HTTPError as error: + print(f"release creation failed: HTTP {error.code}", file=sys.stderr) + return 1 + print(f"Created {result['tag_name']}") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/ci/test_validate_site.py b/ci/test_validate_site.py new file mode 100644 index 0000000..417588b --- /dev/null +++ b/ci/test_validate_site.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +"""Privacy and shape checks for the generated Pages validator.""" + +import tempfile +import unittest +from pathlib import Path + +from validate_site import validate + + +INDEX = """Alkane benchmarks +

Alkane benchmarks

SDK +OSToolkit""" +PAGE = """Alkane benchmarks +

Alkane benchmarks

10 ns/op
""" + + +class SiteValidationTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name) + (self.root / "index.html").write_text(INDEX) + for repo in ("sdk", "os", "toolkit"): + (self.root / repo).mkdir() + (self.root / repo / "index.html").write_text(PAGE) + + def test_valid_data_only_site(self): + self.assertEqual(validate(self.root), []) + + def test_rejects_active_content(self): + (self.root / "os/index.html").write_text(PAGE + "") + self.assertTrue(any("forbidden tag" in error for error in validate(self.root))) + + def test_rejects_external_source_and_credential_marker(self): + (self.root / "sdk/index.html").write_text( + PAGE + 'GITEA_TOKEN' + ) + errors = validate(self.root) + self.assertTrue(any("external or active reference" in error for error in errors)) + self.assertTrue(any("credential marker" in error for error in errors)) + + def test_allows_only_pinned_source_commit_links(self): + commit = "a" * 40 + (self.root / "os/index.html").write_text( + PAGE + f'commit' + ) + self.assertEqual(validate(self.root), []) + (self.root / "os/index.html").write_text( + PAGE + 'commit' + ) + self.assertTrue(any("external or active reference" in error for error in validate(self.root))) + + def test_rejects_extra_files(self): + (self.root / "source.tar.gz").write_bytes(b"private source") + self.assertTrue(any("unexpected published files" in error for error in validate(self.root))) + + +if __name__ == "__main__": + unittest.main() diff --git a/ci/test_version.py b/ci/test_version.py new file mode 100644 index 0000000..55d0aeb --- /dev/null +++ b/ci/test_version.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +"""Check the site release schedule before tag creation.""" + +import unittest + +from version import release_tag + + +class VersionTests(unittest.TestCase): + def test_first_release_and_candidate(self): + self.assertEqual(release_tag("main", [], ["initial"], 0), "v0.1.0") + self.assertEqual(release_tag("next", [], ["initial"], 0), "v0.1.0-rc.1") + + def test_candidate_increments_and_merges_to_final(self): + tags = ["v0.1.0", "v0.1.1-rc.1", "v0.1.1-rc.2"] + self.assertEqual(release_tag("next", tags, ["README.md"], 0), "v0.1.1-rc.3") + self.assertEqual(release_tag("main", tags, ["README.md"], 0), "v0.1.1") + + def test_validator_change_bumps_minor(self): + self.assertEqual( + release_tag("main", ["v0.1.2"], ["ci/validate_site.py"], 0), + "v0.2.0", + ) + + def test_no_change_skips_release(self): + self.assertIsNone(release_tag("main", ["v0.1.0"], [], 0)) + + def test_major_cannot_move_backwards(self): + with self.assertRaises(ValueError): + release_tag("main", ["v2.0.0"], ["README.md"], 1) + + +if __name__ == "__main__": + unittest.main() diff --git a/ci/validate_site.py b/ci/validate_site.py new file mode 100644 index 0000000..e3dd2d6 --- /dev/null +++ b/ci/validate_site.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 +"""Validate the generated public benchmark pages before publication.""" + +import argparse +from html.parser import HTMLParser +from pathlib import Path +import re + + +PAGES = ("index.html", "sdk/index.html", "os/index.html", "toolkit/index.html") +FORBIDDEN_TAGS = {"script", "iframe", "object", "embed", "form"} +FORBIDDEN_TEXT = ("BEGIN PRIVATE KEY", "GITEA_TOKEN", "ghp_", "glpat-") +COMMIT_LINK = re.compile( + r"https://gitea\.bluefla\.re/blueflare/alkane-(?:sdk|os|toolkit)/commit/[0-9a-f]{40}" +) + + +class PageParser(HTMLParser): + def __init__(self): + super().__init__(convert_charrefs=True) + self.title = False + self.heading = False + self.tables = 0 + self.links = set() + self.errors = [] + + def handle_starttag(self, tag, attrs): + if tag in FORBIDDEN_TAGS: + self.errors.append(f"forbidden tag: {tag}") + if tag == "title": + self.title = True + if tag == "h1": + self.heading = True + if tag == "table": + self.tables += 1 + for name, value in attrs: + value = value or "" + if name.startswith("on"): + self.errors.append(f"event handler: {name}") + if name in {"href", "src", "action"}: + allowed_commit = name == "href" and COMMIT_LINK.fullmatch(value) + if not allowed_commit and value.lower().startswith( + ("http:", "https:", "//", "javascript:", "data:") + ): + self.errors.append(f"external or active reference: {value}") + if name == "href": + self.links.add(value) + +def validate_page(path: Path, relative: str) -> list[str]: + if not path.is_file() or path.is_symlink(): + return [f"{relative}: missing regular page"] + if path.stat().st_size > 2_000_000: + return [f"{relative}: page exceeds 2 MB"] + text = path.read_text(encoding="utf-8") + parser = PageParser() + parser.feed(text) + errors = [f"{relative}: {item}" for item in parser.errors] + if not text.lstrip().lower().startswith(""): + errors.append(f"{relative}: missing HTML doctype") + if not parser.title or not parser.heading: + errors.append(f"{relative}: missing title or heading") + if any(token in text for token in FORBIDDEN_TEXT): + errors.append(f"{relative}: credential marker in public output") + if re.search(r"url\s*\(|@import", text, re.IGNORECASE): + errors.append(f"{relative}: external CSS reference is not allowed") + if relative == "index.html": + if not {"sdk/", "os/", "toolkit/"}.issubset(parser.links): + errors.append("index.html: missing project links") + elif not parser.tables and "Waiting for a completed measured CI run." not in text: + errors.append(f"{relative}: no measurements or pending notice") + return errors + + +def validate(root: Path) -> list[str]: + if not root.is_dir() or root.is_symlink(): + return ["site root is not a directory"] + paths = {p.relative_to(root).as_posix() for p in root.rglob("*") if p.is_file()} + errors = [] + if any(p.is_symlink() for p in root.rglob("*")): + errors.append("published site contains a symlink") + for relative in PAGES: + errors.extend(validate_page(root / relative, relative)) + unexpected = paths - set(PAGES) + if unexpected: + errors.append("unexpected published files: " + ", ".join(sorted(unexpected))) + return errors + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("site", type=Path) + args = parser.parse_args() + errors = validate(args.site) + for error in errors: + print(error) + if errors: + return 1 + print(f"Validated {len(PAGES)} public benchmark pages") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/ci/version-x.txt b/ci/version-x.txt new file mode 100644 index 0000000..573541a --- /dev/null +++ b/ci/version-x.txt @@ -0,0 +1 @@ +0 diff --git a/ci/version.py b/ci/version.py new file mode 100644 index 0000000..eac20b9 --- /dev/null +++ b/ci/version.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +"""Select site configuration release tags; version X is edited by hand.""" + +import argparse +from pathlib import Path +import re +import subprocess + + +FINAL = re.compile(r"v(\d+)\.(\d+)\.(\d+)$") +RC = re.compile(r"v(\d+)\.(\d+)\.(\d+)-rc\.(\d+)$") + + +def git(*args: str) -> str: + return subprocess.check_output(["git", *args], text=True).strip() + + +def release_tag(branch: str, tags: list[str], changed: list[str], major: int) -> str | None: + finals = sorted((tuple(map(int, match.groups())), tag) + for tag in tags if (match := FINAL.fullmatch(tag))) + latest = finals[-1] if finals else None + if latest and latest[0][0] > major: + raise ValueError("manual major version is behind the latest release") + if not changed: + return None + if latest and latest[0][0] == major: + _, minor, patch = latest[0] + if "ci/validate_site.py" in changed: + minor, patch = minor + 1, 0 + else: + patch += 1 + else: + minor, patch = (1, 0) if major == 0 else (0, 0) + base = f"v{major}.{minor}.{patch}" + if branch == "main": + return base + numbers = [int(match.group(4)) for tag in tags + if (match := RC.fullmatch(tag)) and tag.startswith(base + "-rc.")] + return f"{base}-rc.{max(numbers, default=0) + 1}" + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("branch", choices=("main", "next")) + args = parser.parse_args() + major = int(Path(__file__).with_name("version-x.txt").read_text().strip()) + if major < 0: + raise ValueError("major version must be nonnegative") + tags = git("tag", "-l", "v*").splitlines() + finals = sorted((tuple(map(int, match.groups())), tag) + for tag in tags if (match := FINAL.fullmatch(tag))) + changed = git("diff", "--name-only", finals[-1][1], "HEAD").splitlines() if finals else ["initial"] + tag = release_tag(args.branch, tags, changed, major) + print(f"release={'true' if tag else 'false'}") + if tag: + print(f"tag={tag}") + + +if __name__ == "__main__": + main()