|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import json |
| 3 | +import re |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +REPO_BASE_URL = "https://raw.githubusercontent.com/Arkhe-Systems/senddock-templates/main" |
| 8 | +ALLOWED_CATEGORIES = {"welcome", "newsletter", "announcement", "digest", "transactional"} |
| 9 | +ID_PATTERN = re.compile(r"^[a-z0-9]+(-[a-z0-9]+)*$") |
| 10 | +MAX_THUMBNAIL_BYTES = 100 * 1024 |
| 11 | +MAX_NAME_CHARS = 40 |
| 12 | +MAX_DESCRIPTION_CHARS = 140 |
| 13 | +REQUIRED_FIELDS = {"id", "name", "category", "description", "thumbnail_url", "html_url", "variables"} |
| 14 | +BUILTIN_VARIABLES = {"name", "email", "subscriber_id", "unsubscribe_url"} |
| 15 | +SCRIPT_TAG = re.compile(r"<script[\s>]", re.IGNORECASE) |
| 16 | + |
| 17 | + |
| 18 | +def main() -> None: |
| 19 | + repo_root = Path(__file__).resolve().parent.parent |
| 20 | + manifest_path = repo_root / "index.json" |
| 21 | + errors: list[str] = [] |
| 22 | + |
| 23 | + try: |
| 24 | + manifest = json.loads(manifest_path.read_text()) |
| 25 | + except (json.JSONDecodeError, OSError) as e: |
| 26 | + print(f"ERROR: cannot parse index.json: {e}") |
| 27 | + sys.exit(1) |
| 28 | + |
| 29 | + if not isinstance(manifest, dict) or "version" not in manifest or "templates" not in manifest: |
| 30 | + errors.append("index.json: top-level must have `version` and `templates`") |
| 31 | + if not isinstance(manifest.get("templates"), list): |
| 32 | + errors.append("index.json: `templates` must be an array") |
| 33 | + |
| 34 | + seen_ids: set[str] = set() |
| 35 | + for i, entry in enumerate(manifest.get("templates", [])): |
| 36 | + prefix = f"templates[{i}]" |
| 37 | + if not isinstance(entry, dict): |
| 38 | + errors.append(f"{prefix}: must be an object") |
| 39 | + continue |
| 40 | + |
| 41 | + missing = REQUIRED_FIELDS - set(entry.keys()) |
| 42 | + if missing: |
| 43 | + errors.append(f"{prefix}: missing fields: {sorted(missing)}") |
| 44 | + continue |
| 45 | + |
| 46 | + eid = entry["id"] |
| 47 | + if not isinstance(eid, str) or not ID_PATTERN.match(eid): |
| 48 | + errors.append(f"{prefix}: id must be kebab-case (got {eid!r})") |
| 49 | + continue |
| 50 | + if eid in seen_ids: |
| 51 | + errors.append(f"{prefix}: duplicate id {eid!r}") |
| 52 | + seen_ids.add(eid) |
| 53 | + |
| 54 | + prefix = f"templates[{i}] ({eid})" |
| 55 | + |
| 56 | + name = entry["name"] |
| 57 | + if not isinstance(name, str) or not name or len(name) > MAX_NAME_CHARS: |
| 58 | + errors.append(f"{prefix}: name must be a non-empty string ≤ {MAX_NAME_CHARS} chars") |
| 59 | + |
| 60 | + if entry["category"] not in ALLOWED_CATEGORIES: |
| 61 | + errors.append(f"{prefix}: category must be one of {sorted(ALLOWED_CATEGORIES)}") |
| 62 | + |
| 63 | + description = entry["description"] |
| 64 | + if not isinstance(description, str) or not description or len(description) > MAX_DESCRIPTION_CHARS: |
| 65 | + errors.append(f"{prefix}: description must be a non-empty string ≤ {MAX_DESCRIPTION_CHARS} chars") |
| 66 | + |
| 67 | + expected_html = f"{REPO_BASE_URL}/templates/{eid}.html" |
| 68 | + expected_thumb = f"{REPO_BASE_URL}/templates/{eid}.png" |
| 69 | + if entry["html_url"] != expected_html: |
| 70 | + errors.append(f"{prefix}: html_url must be {expected_html}") |
| 71 | + if entry["thumbnail_url"] != expected_thumb: |
| 72 | + errors.append(f"{prefix}: thumbnail_url must be {expected_thumb}") |
| 73 | + |
| 74 | + variables = entry["variables"] |
| 75 | + if not isinstance(variables, list) or not all(isinstance(v, str) for v in variables): |
| 76 | + errors.append(f"{prefix}: variables must be an array of strings") |
| 77 | + else: |
| 78 | + for v in variables: |
| 79 | + if v in BUILTIN_VARIABLES: |
| 80 | + errors.append(f"{prefix}: variable {v!r} is built-in, don't declare it") |
| 81 | + |
| 82 | + html_path = repo_root / "templates" / f"{eid}.html" |
| 83 | + thumb_path = repo_root / "templates" / f"{eid}.png" |
| 84 | + |
| 85 | + if not html_path.is_file(): |
| 86 | + errors.append(f"{prefix}: missing file templates/{eid}.html") |
| 87 | + else: |
| 88 | + html = html_path.read_text(errors="replace") |
| 89 | + if SCRIPT_TAG.search(html): |
| 90 | + errors.append(f"{prefix}: html contains a <script> tag") |
| 91 | + |
| 92 | + if not thumb_path.is_file(): |
| 93 | + errors.append(f"{prefix}: missing file templates/{eid}.png") |
| 94 | + elif thumb_path.stat().st_size > MAX_THUMBNAIL_BYTES: |
| 95 | + kb = thumb_path.stat().st_size // 1024 |
| 96 | + errors.append(f"{prefix}: thumbnail is {kb}KB, max is {MAX_THUMBNAIL_BYTES // 1024}KB") |
| 97 | + |
| 98 | + templates_dir = repo_root / "templates" |
| 99 | + if templates_dir.is_dir(): |
| 100 | + for file in templates_dir.iterdir(): |
| 101 | + if file.suffix in {".html", ".png"} and file.stem not in seen_ids: |
| 102 | + errors.append(f"orphan file: templates/{file.name} has no entry in index.json") |
| 103 | + |
| 104 | + if errors: |
| 105 | + print("Validation failed:") |
| 106 | + for err in errors: |
| 107 | + print(f" - {err}") |
| 108 | + sys.exit(1) |
| 109 | + |
| 110 | + print(f"OK — {len(seen_ids)} template(s) valid") |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments