|
| 1 | +"""AST guard: raw ``.get('issue_url')`` / ``.get('issue_urls')`` calls banned in ``fleet/``. |
| 2 | +
|
| 3 | +Issue #4112 defense-in-depth: the canonical extraction function lives in |
| 4 | +``_issue_url_helpers.py``, and any raw ``.get()`` call elsewhere in ``fleet/`` |
| 5 | +(except ``_issue_url_helpers.py`` itself and ``state_types.py``, which |
| 6 | +deserializes ``DispatchRecord`` from its own JSON dict) would re-introduce the |
| 7 | +singular/plural key mismatch that orphaned labels for the 7th time. |
| 8 | +
|
| 9 | +Also enforces that ``fleet_claim_guard.py`` retains BOTH key variants in its |
| 10 | +inline dual-key lookup — the guard is a stdlib-only hook script and cannot |
| 11 | +import the canonical helper, so it keeps its inline pattern and this test |
| 12 | +guards that both ``issue_urls`` and ``issue_url`` keys remain present. |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import ast |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +pytestmark = [pytest.mark.layer("arch"), pytest.mark.small] |
| 23 | + |
| 24 | + |
| 25 | +SRC_ROOT = Path(__file__).resolve().parent.parent.parent / "src" / "autoskillit" / "fleet" |
| 26 | +HOOK_GUARD_PATH = ( |
| 27 | + Path(__file__).resolve().parent.parent.parent |
| 28 | + / "src" |
| 29 | + / "autoskillit" |
| 30 | + / "hooks" |
| 31 | + / "guards" |
| 32 | + / "fleet_claim_guard.py" |
| 33 | +) |
| 34 | + |
| 35 | +# Files exempt from the raw-``get`` ban: |
| 36 | +# - ``_issue_url_helpers.py``: defines the canonical accessor. |
| 37 | +# - ``state_types.py``: deserializes ``DispatchRecord`` from its own JSON dict. |
| 38 | +EXEMPT_FILES: frozenset[str] = frozenset({"_issue_url_helpers.py", "state_types.py"}) |
| 39 | + |
| 40 | +BANNED_KEYS: frozenset[str] = frozenset({"issue_url", "issue_urls"}) |
| 41 | + |
| 42 | + |
| 43 | +def _find_issue_url_get_calls(path: Path) -> list[tuple[int, str]]: |
| 44 | + """Return ``(line, key)`` tuples for raw ``.get`` calls on issue-URL keys.""" |
| 45 | + try: |
| 46 | + tree = ast.parse(path.read_text()) |
| 47 | + except SyntaxError: |
| 48 | + return [] |
| 49 | + results: list[tuple[int, str]] = [] |
| 50 | + for node in ast.walk(tree): |
| 51 | + if not isinstance(node, ast.Call): |
| 52 | + continue |
| 53 | + func = node.func |
| 54 | + if not (isinstance(func, ast.Attribute) and func.attr == "get"): |
| 55 | + continue |
| 56 | + if not node.args: |
| 57 | + continue |
| 58 | + first_arg = node.args[0] |
| 59 | + if not isinstance(first_arg, ast.Constant): |
| 60 | + continue |
| 61 | + if first_arg.value in BANNED_KEYS: |
| 62 | + results.append((node.lineno, first_arg.value)) |
| 63 | + return results |
| 64 | + |
| 65 | + |
| 66 | +def test_no_raw_issue_url_get_in_fleet() -> None: |
| 67 | + """Raw ``.get('issue_url')`` / ``.get('issue_urls')`` are banned in fleet/. |
| 68 | +
|
| 69 | + All fleet code must route through ``extract_issue_urls()`` to guarantee the |
| 70 | + dual-key lookup is applied uniformly. Any direct ``.get()`` call would |
| 71 | + re-introduce the singular/plural mismatch. |
| 72 | + """ |
| 73 | + violations: list[str] = [] |
| 74 | + for py_file in sorted(SRC_ROOT.glob("*.py")): |
| 75 | + if py_file.name in EXEMPT_FILES: |
| 76 | + continue |
| 77 | + for lineno, key in _find_issue_url_get_calls(py_file): |
| 78 | + violations.append(f"{py_file.name}:{lineno} (.get({key!r}))") |
| 79 | + |
| 80 | + assert not violations, ( |
| 81 | + "Raw .get('issue_url') / .get('issue_urls') calls in fleet/ must go " |
| 82 | + f"through extract_issue_urls() in _issue_url_helpers.py. Found in: {violations}" |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +def test_fleet_claim_guard_has_dual_key_lookup() -> None: |
| 87 | + """``fleet_claim_guard.py`` must look up BOTH ``issue_urls`` AND ``issue_url``. |
| 88 | +
|
| 89 | + The hook is stdlib-only and cannot import ``extract_issue_urls()``, so it |
| 90 | + keeps its inline dual-key pattern. This test enforces that both key |
| 91 | + variants remain present — if a contributor removes either the plural or |
| 92 | + singular lookup, this test fails. |
| 93 | + """ |
| 94 | + assert HOOK_GUARD_PATH.exists(), f"Hook guard not found at {HOOK_GUARD_PATH}" |
| 95 | + found_keys = {key for _, key in _find_issue_url_get_calls(HOOK_GUARD_PATH)} |
| 96 | + |
| 97 | + assert "issue_urls" in found_keys, ( |
| 98 | + "fleet_claim_guard.py must look up the plural 'issue_urls' key." |
| 99 | + ) |
| 100 | + assert "issue_url" in found_keys, ( |
| 101 | + "fleet_claim_guard.py must look up the singular 'issue_url' key " |
| 102 | + "(dual-key ingredient access is required)." |
| 103 | + ) |
0 commit comments