#!/usr/bin/env python3 """Check that every label the triage workflow applies is declared in labels.yml. ``.github/labels.yml`` is the declarative source of truth for repository labels, and ``.github/workflows/triage.yml`` applies labels by name at runtime. GitHub rejects an ``addLabels`` call with **422** when *any* requested label does not exist, and drops the whole batch -- so a label added to the workflow but forgotten here makes labelling stop working silently instead of failing loudly. This is the static guard for that pair: it extracts the label literals the triage workflow can apply and asserts each one is declared in labels.yml. The reader follows quoted literals through arrays, conditional values, variable aliases, Sets and add/push mutations in each workflow script. Filter predicates, log messages, YAML comments and JavaScript comments are not label sources. The current AREA_RULES first-element map is supported; other map transformations, computed calls/strings and unresolved values fail closed. This is not a general JavaScript evaluator. The read-only ``skip-validation`` control is not applied. Standard library only -- the check has to stay cheap enough to run on every PR. Regression coverage: backend/tests/test_label_declarations.py. Usage: python scripts/check_label_declarations.py """ from __future__ import annotations import ast import re import sys from pathlib import Path from typing import NamedTuple REPO_ROOT = Path(__file__).resolve().parent.parent LABELS_FILE = REPO_ROOT / ".github" / "labels.yml" TRIAGE_WORKFLOW = REPO_ROOT / ".github" / "workflows" / "triage.yml" # Only workflow JavaScript is inspected; YAML and JavaScript comments are not # label sources. Regex literals must be tokens too, so their escaped slashes # cannot be mistaken for the start of a comment. SCRIPT = re.compile(r"^(?P *)script:\s*[|>][-+]?\s*(?:#.*)?$") JS_TOKEN = re.compile( r"(?P//[^\n]*|/\*.*?\*/)" r"|(?P'(?:\\.|[^'\\])*'|\"(?:\\.|[^\"\\])*\")" r"|(?P