Repository navigation
ci: validate static site on pull requests #2
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
Merged
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| """Validate HTML entrypoints, local assets, CSS URLs and JavaScript syntax. | ||
|
|
||
| Remote URLs are deliberately not fetched: PR checks must work without production | ||
| services or credentials. Game assets are checked against the exported project. | ||
| """ | ||
| import argparse | ||
| from html.parser import HTMLParser | ||
| import json | ||
| from pathlib import Path | ||
| import re | ||
| import subprocess | ||
| from urllib.parse import unquote, urlsplit | ||
|
|
||
|
|
||
| class Page(HTMLParser): | ||
| def __init__(self): | ||
| super().__init__() | ||
| self.tags = set() | ||
| self.references = [] | ||
| self.scripts = [] | ||
| self.script = None | ||
| self.doctype = False | ||
| self.title = "" | ||
| self.in_title = False | ||
|
|
||
| def handle_decl(self, declaration): | ||
| self.doctype |= declaration.lower() == "doctype html" | ||
|
|
||
| def handle_starttag(self, tag, attributes): | ||
| self.tags.add(tag) | ||
| attributes = dict(attributes) | ||
| for key in ("src", "href", "poster"): | ||
| if attributes.get(key): | ||
| self.references.append(attributes[key]) | ||
| if tag == "title": | ||
| self.in_title = True | ||
| if tag == "script" and not attributes.get("src"): | ||
| script_type = attributes.get("type", "text/javascript") | ||
| if script_type in ("module", "text/javascript", "application/javascript", ""): | ||
| self.script = (script_type == "module", []) | ||
|
|
||
| def handle_endtag(self, tag): | ||
| if tag == "title": | ||
| self.in_title = False | ||
| if tag == "script" and self.script is not None: | ||
| self.scripts.append(self.script) | ||
| self.script = None | ||
|
|
||
| def handle_data(self, data): | ||
| if self.script is not None: | ||
| self.script[1].append(data) | ||
| if self.in_title: | ||
| self.title += data | ||
|
|
||
|
|
||
| def local_reference(root, source, reference): | ||
| url = urlsplit(reference) | ||
| if url.scheme or url.netloc or not url.path: | ||
| return | ||
| decoded = unquote(url.path) | ||
| destination = (root / decoded.lstrip("/") if decoded.startswith("/") else source.parent / decoded).resolve() | ||
| if not destination.is_relative_to(root): | ||
| raise ValueError(f"{source}: asset escapes site root: {reference}") | ||
| if not destination.exists(): | ||
| raise ValueError(f"{source}: missing local asset: {reference}") | ||
|
|
||
|
|
||
| def check_javascript(code, module=False): | ||
| command = ["node", "--check"] | ||
| if module: | ||
| command.append("--input-type=module") | ||
| subprocess.run(command, input=code, text=True, check=True) | ||
|
|
||
|
|
||
| def validate(root): | ||
| root = root.resolve() | ||
| pages = sorted(root.rglob("*.html")) | ||
| if not pages: | ||
| raise ValueError(f"No HTML pages in {root}") | ||
| for path in pages: | ||
| page = Page() | ||
| page.feed(path.read_text()) | ||
| if not page.doctype or not {"html", "head", "body", "title"} <= page.tags or not page.title.strip(): | ||
| raise ValueError(f"{path}: expected HTML document with doctype, head, body and title") | ||
| for reference in page.references: | ||
| local_reference(root, path, reference) | ||
| for module, script in page.scripts: | ||
| check_javascript("".join(script), module) | ||
| for path in root.rglob("*.js"): | ||
| subprocess.run(["node", "--check", str(path)], check=True) | ||
| for path in root.rglob("*.css"): | ||
| css = re.sub(r"/\*.*?\*/", "", path.read_text(), flags=re.S) | ||
| for reference in re.findall(r"url\(\s*['\"]?([^)'\"]+)['\"]?\s*\)", css): | ||
| local_reference(root, path, reference.strip()) | ||
| for path in root.rglob("*.webmanifest"): | ||
| manifest = json.loads(path.read_text()) | ||
| for icon in manifest.get("icons", []): | ||
| local_reference(root, path, icon["src"]) | ||
| project = root / "assets/project.json" | ||
| if project.exists(): | ||
| data = json.loads(project.read_text()) | ||
| if not data.get("targets"): | ||
| raise ValueError("Game project contains no targets") | ||
| for target in data["targets"]: | ||
| for asset in target.get("costumes", []) + target.get("sounds", []): | ||
| name = asset.get("md5ext") or f'{asset["assetId"]}.{asset["dataFormat"]}' | ||
| local_reference(root, project, name) | ||
| print(f"Validated {len(pages)} HTML page(s), local assets and JavaScript in {root}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("root", type=Path) | ||
| validate(parser.parse_args().root) | ||
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,29 @@ | ||
| name: PR validation | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [master] | ||
|
astrovm marked this conversation as resolved.
Resolved
|
||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| concurrency: | ||
| group: ci-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | ||
| cancel-in-progress: ${{ github.event_name == 'pull_request' }} | ||
|
|
||
| jobs: | ||
| validate: | ||
| name: Validate site | ||
| runs-on: ubuntu-26.04 | ||
| timeout-minutes: 10 | ||
| steps: | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| - uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6 | ||
| with: | ||
| python-version: "3.13" | ||
| - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6 | ||
| with: | ||
| node-version: "24" | ||
| - name: Validate HTML, JavaScript and local assets | ||
| run: python .github/scripts/validate-static.py . | ||
Oops, something went wrong.
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.