-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroken_refs.py
More file actions
134 lines (113 loc) · 4.99 KB
/
broken_refs.py
File metadata and controls
134 lines (113 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""Broken standards/*.md references check.
Scans every markdown file in the snapshot for links whose targets live
under ``standards/``. Resolves each target against the meta-repo's
``standards/`` directory at snapshot time. Missing target -> ``error``.
Zero surface today — the design-doc preflight found 0 ``standards/*.md``
links across 13 agent-context files in 9 repos. The check is implemented
anyway so the plumbing is in place the moment tool repos start linking
to standards.
Link shapes we care about:
* ``[text](standards/foo.md)``
* ``[text](standards/foo.md#anchor)`` — fragment checking deferred; we
only verify the file exists for now
* ``[text](../Developer-Tools-Directory/standards/foo.md)`` — tolerated
by stripping to the trailing ``standards/foo.md``
* reference-style ``[text][label]`` + ``[label]: standards/foo.md`` —
the label-definition regex below catches these
* bare URLs to GitHub-hosted standards are NOT checked; contribute
a separate check for those when/if they appear
We do NOT follow non-``standards/`` links. That is out of scope for this
check; it would duplicate a general markdown link checker.
"""
from __future__ import annotations
import re
from pathlib import Path
from typing import Iterable, List, Tuple
from ..types import Finding, RepoSnapshot
NAME = "broken-refs"
# Inline link: ``[text](target)`` where target starts with or contains
# ``standards/`` and ends with ``.md`` (with optional ``#fragment``).
_INLINE_LINK_RE = re.compile(
rb"\[[^\]]*\]\(\s*([^)\s]*standards/[^)\s#]+\.md(?:#[^)\s]*)?)\s*\)"
)
# Reference-style definition: ``[label]: standards/foo.md``
_REF_DEF_RE = re.compile(
rb"^\s*\[[^\]]+\]:\s*([^\s]*standards/[^\s#]+\.md(?:#[^\s]*)?)\s*$",
re.MULTILINE,
)
def _iter_standards_links(content: bytes) -> Iterable[Tuple[str, int]]:
"""Yield ``(target, line_number)`` for every standards link in the
file. ``target`` is a decoded string; ``line_number`` is 1-indexed."""
for m in _INLINE_LINK_RE.finditer(content):
yield m.group(1).decode("utf-8", errors="replace"), content.count(b"\n", 0, m.start()) + 1
for m in _REF_DEF_RE.finditer(content):
yield m.group(1).decode("utf-8", errors="replace"), content.count(b"\n", 0, m.start()) + 1
def _extract_standard_filename(target: str) -> str | None:
"""Given a link target, return the standards file basename or None.
``standards/foo.md`` -> ``foo.md``
``standards/foo.md#anchor`` -> ``foo.md``
``../standards/foo.md`` -> ``foo.md``
``https://github.com/.../standards/foo.md`` -> ``foo.md``
"""
target = target.split("#", 1)[0]
marker = "standards/"
idx = target.rfind(marker)
if idx == -1:
return None
basename = target[idx + len(marker):].strip("/")
if not basename or not basename.endswith(".md"):
return None
if "/" in basename:
# Nested path under standards/. We only ship flat files in
# ``standards/``, so a nested path is a broken reference by
# construction.
return basename
return basename
class BrokenRefsCheck:
name: str = NAME
def run(self, snapshot: RepoSnapshot) -> Iterable[Finding]:
if NAME in snapshot.config.skip_checks:
return ()
out: List[Finding] = []
meta_files = snapshot.meta_standards
for rel_path, file in snapshot.files.items():
pragma = next(
(p for p in file.pragmas if p.check_name == NAME), None
)
if pragma is not None:
out.append(
Finding(
repo=snapshot.slug,
file=rel_path,
check=NAME,
severity="info",
message=(
"skipped by drift-ignore pragma"
+ (f" (reason: {pragma.reason})" if pragma.reason else "")
),
)
)
continue
for target, line in _iter_standards_links(file.content):
basename = _extract_standard_filename(target)
if basename is None:
continue
if basename not in meta_files:
out.append(
Finding(
repo=snapshot.slug,
file=rel_path,
check=NAME,
severity="error",
message=(
f"broken standards reference at line {line}: "
f"{target!r} (standards/{basename} does not "
f"exist in meta-repo)"
),
suggested_fix=(
f"check the filename; available standards: "
f"{', '.join(sorted(meta_files)) or '(none found)'}"
),
)
)
return out