|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Validate module-metadata.yml files against docs/REVIEW-PROCESS.md. |
| 3 | +
|
| 4 | +Errors fail the check. Review expiry only warns, so one stale module does not |
| 5 | +block unrelated pull requests. |
| 6 | +""" |
| 7 | + |
| 8 | +import datetime |
| 9 | +import pathlib |
| 10 | +import re |
| 11 | +import sys |
| 12 | + |
| 13 | +import yaml |
| 14 | + |
| 15 | +STATUSES = {"draft", "in-review", "reviewed", "stable", "needs-update", "deprecated"} |
| 16 | +REVIEWED_STATUSES = {"reviewed", "stable"} |
| 17 | +STRING_FIELDS = ["title", "slug", "summary"] |
| 18 | +LIST_FIELDS = [ |
| 19 | + "learning_objectives", |
| 20 | + "prerequisites", |
| 21 | + "authors", |
| 22 | + "reviewers", |
| 23 | + "maintainers", |
| 24 | + "related_tracks", |
| 25 | + "related_labs", |
| 26 | + "related_datasets", |
| 27 | +] |
| 28 | +REQUIRED = STRING_FIELDS + LIST_FIELDS + ["status", "version", "last_reviewed"] |
| 29 | +SLUG_RE = re.compile(r"^[a-z0-9]+(-[a-z0-9]+)*$") |
| 30 | +DATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$") |
| 31 | +EXPIRY_DAYS = 365 |
| 32 | + |
| 33 | +errors = 0 |
| 34 | + |
| 35 | + |
| 36 | +def error(path, message): |
| 37 | + global errors |
| 38 | + errors += 1 |
| 39 | + print(f"::error file={path}::{message}") |
| 40 | + |
| 41 | + |
| 42 | +def warning(path, message): |
| 43 | + print(f"::warning file={path}::{message}") |
| 44 | + |
| 45 | + |
| 46 | +def load(path): |
| 47 | + try: |
| 48 | + data = yaml.safe_load(path.read_text(encoding="utf-8")) |
| 49 | + except yaml.YAMLError as exc: |
| 50 | + error(path, f"invalid YAML: {exc}") |
| 51 | + return None |
| 52 | + if not isinstance(data, dict): |
| 53 | + error(path, "top level must be a mapping") |
| 54 | + return None |
| 55 | + return data |
| 56 | + |
| 57 | + |
| 58 | +def handle(value): |
| 59 | + return str(value).strip().lstrip("@").lower() |
| 60 | + |
| 61 | + |
| 62 | +def parse_date(value): |
| 63 | + if isinstance(value, datetime.date): |
| 64 | + return value |
| 65 | + if isinstance(value, str) and DATE_RE.match(value): |
| 66 | + try: |
| 67 | + return datetime.date.fromisoformat(value) |
| 68 | + except ValueError: |
| 69 | + return None |
| 70 | + return None |
| 71 | + |
| 72 | + |
| 73 | +def check_template(path): |
| 74 | + data = load(path) |
| 75 | + if data is None: |
| 76 | + return |
| 77 | + for key in REQUIRED: |
| 78 | + if key not in data: |
| 79 | + error(path, f"template is missing field '{key}'") |
| 80 | + |
| 81 | + |
| 82 | +def check_module(path): |
| 83 | + data = load(path) |
| 84 | + if data is None: |
| 85 | + return |
| 86 | + |
| 87 | + for key in REQUIRED: |
| 88 | + if key not in data: |
| 89 | + error(path, f"missing required field '{key}'") |
| 90 | + for key in data: |
| 91 | + if key not in REQUIRED: |
| 92 | + warning(path, f"unknown field '{key}'") |
| 93 | + |
| 94 | + for key in STRING_FIELDS: |
| 95 | + value = data.get(key) |
| 96 | + if key in data and not (isinstance(value, str) and value.strip()): |
| 97 | + error(path, f"'{key}' must be a non-empty string") |
| 98 | + |
| 99 | + for key in LIST_FIELDS: |
| 100 | + value = data.get(key) |
| 101 | + if key not in data: |
| 102 | + continue |
| 103 | + if not isinstance(value, list): |
| 104 | + error(path, f"'{key}' must be a list") |
| 105 | + continue |
| 106 | + for item in value: |
| 107 | + if not (isinstance(item, str) and item.strip()): |
| 108 | + error(path, f"'{key}' contains an empty entry (unfilled template?)") |
| 109 | + break |
| 110 | + |
| 111 | + if isinstance(data.get("learning_objectives"), list) and not data["learning_objectives"]: |
| 112 | + error(path, "'learning_objectives' must have at least one entry") |
| 113 | + |
| 114 | + slug = data.get("slug") |
| 115 | + if isinstance(slug, str) and slug.strip(): |
| 116 | + if not SLUG_RE.match(slug): |
| 117 | + error(path, f"slug '{slug}' must be lowercase kebab-case") |
| 118 | + directory = re.sub(r"^\d+-", "", path.parent.name) |
| 119 | + if directory != slug: |
| 120 | + error(path, f"slug '{slug}' must match the directory name '{path.parent.name}' without its number prefix") |
| 121 | + |
| 122 | + version = data.get("version") |
| 123 | + if "version" in data and (version is None or not str(version).strip()): |
| 124 | + error(path, "'version' must not be empty") |
| 125 | + |
| 126 | + status = data.get("status") |
| 127 | + if "status" in data and status not in STATUSES: |
| 128 | + error(path, f"status '{status}' must be one of: {', '.join(sorted(STATUSES))}") |
| 129 | + |
| 130 | + last_reviewed = data.get("last_reviewed") |
| 131 | + reviewed_on = None |
| 132 | + if last_reviewed is not None: |
| 133 | + reviewed_on = parse_date(last_reviewed) |
| 134 | + if reviewed_on is None: |
| 135 | + error(path, "'last_reviewed' must be null or a YYYY-MM-DD date") |
| 136 | + elif reviewed_on > datetime.date.today(): |
| 137 | + error(path, "'last_reviewed' is in the future") |
| 138 | + |
| 139 | + reviewers = data.get("reviewers") if isinstance(data.get("reviewers"), list) else [] |
| 140 | + authors = data.get("authors") if isinstance(data.get("authors"), list) else [] |
| 141 | + |
| 142 | + overlap = {handle(r) for r in reviewers} & {handle(a) for a in authors} |
| 143 | + if overlap: |
| 144 | + error(path, f"reviewers must be independent of authors (both: {', '.join(sorted(overlap))})") |
| 145 | + |
| 146 | + if status in REVIEWED_STATUSES: |
| 147 | + if not reviewers: |
| 148 | + error(path, f"status '{status}' requires at least one reviewer") |
| 149 | + if last_reviewed is None: |
| 150 | + error(path, f"status '{status}' requires 'last_reviewed'") |
| 151 | + elif reviewed_on is not None: |
| 152 | + age = (datetime.date.today() - reviewed_on).days |
| 153 | + if age > EXPIRY_DAYS: |
| 154 | + warning(path, f"last reviewed {age} days ago; past the {EXPIRY_DAYS}-day expiry, so the status should be 'needs-update'") |
| 155 | + |
| 156 | + |
| 157 | +def main(): |
| 158 | + root = pathlib.Path(".") |
| 159 | + template = root / "templates" / "module-metadata.yml" |
| 160 | + if template.exists(): |
| 161 | + check_template(template) |
| 162 | + |
| 163 | + modules = sorted( |
| 164 | + p |
| 165 | + for p in root.rglob("module-metadata.yml") |
| 166 | + if ".git" not in p.parts and "templates" not in p.parts |
| 167 | + ) |
| 168 | + for path in modules: |
| 169 | + check_module(path) |
| 170 | + |
| 171 | + print(f"Checked {len(modules)} module(s); {errors} error(s).") |
| 172 | + return 1 if errors else 0 |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + sys.exit(main()) |
0 commit comments