-
Notifications
You must be signed in to change notification settings - Fork 2
296 lines (256 loc) · 11.9 KB
/
Copy pathvalidate-content.yaml
File metadata and controls
296 lines (256 loc) · 11.9 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
name: Validate Content
on:
pull_request:
branches: [master]
paths:
- "**.md"
# push:
# branches: [master]
# paths:
# - "**.md"
# Allow the action to post PR comments
permissions:
pull-requests: write
contents: read
jobs:
validate:
name: Content Validation
runs-on: ubuntu-latest
steps:
- name: Checkout content repo
uses: actions/checkout@v4
with:
fetch-depth: 0
# ─────────────────────────────────────────────
# 1. Collect changed markdown files
# ─────────────────────────────────────────────
- name: Get changed markdown files
id: changed
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD -- '*.md' | tr '\n' ' ')
else
FILES=$(git diff --name-only HEAD~1 HEAD -- '*.md' | tr '\n' ' ')
fi
echo "Changed files: $FILES"
echo "files=$FILES" >> $GITHUB_OUTPUT
# ─────────────────────────────────────────────
# 2. Set up tools
# ─────────────────────────────────────────────
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install dependencies
run: |
pip install pyyaml
npm install -g markdownlint-cli
# ─────────────────────────────────────────────
# 3. Run all checks, write errors to report file
# ─────────────────────────────────────────────
- name: Run all content checks
id: checks
if: always()
run: |
python3 << 'PYEOF'
import os, sys, yaml, glob, re, json
CHANGED = os.environ.get("CHANGED_FILES", "").split()
all_md = glob.glob("**/*.md", recursive=True)
files = [f for f in CHANGED if f] if CHANGED else all_md
report = []
def add(check, filepath, line, msg):
report.append({"check": check, "file": filepath, "line": line, "msg": msg})
# ── Frontmatter ─────────────────────────
REQUIRED = ["title", "author"]
for filepath in files:
if not os.path.isfile(filepath):
continue
content = open(filepath, encoding="utf-8").read()
if not content.startswith("---"):
add("Frontmatter", filepath, 1, "Missing frontmatter block (must start with `---`)")
continue
parts = content.split("---", 2)
if len(parts) < 3:
add("Frontmatter", filepath, 1, "Malformed frontmatter — no closing `---`")
continue
try:
fm = yaml.safe_load(parts[1]) or {}
except yaml.YAMLError as e:
add("Frontmatter", filepath, 1, f"Invalid YAML in frontmatter: {e}")
continue
for field in REQUIRED:
if field not in fm or not str(fm[field]).strip():
add("Frontmatter", filepath, 1, f"Missing required field `{field}`")
# ── Emoji / keycap sequences ─────────────
KEYCAP = re.compile(r'[0-9#*]\uFE0F\u20E3')
for filepath in files:
if not os.path.isfile(filepath):
continue
for lineno, line in enumerate(open(filepath, encoding="utf-8"), 1):
if KEYCAP.search(line):
add("Emoji", filepath, lineno,
"Unsupported keycap emoji (e.g. 1️⃣) — breaks Quartz OG image generation. Replace with plain text or a supported emoji.")
# ── Broken internal links ────────────────
known = set()
for f in all_md:
slug = os.path.splitext(f)[0].lstrip("./")
known.add(slug.lower())
known.add(os.path.basename(slug).lower())
WIKILINK = re.compile(r'\[\[([^\]|#]+)(?:[|#][^\]]*)?\]\]')
MD_LINK = re.compile(r'\[[^\]]*\]\((?!https?://)([^)#]+?)(?:#[^)]*)?\)')
for filepath in files:
if not os.path.isfile(filepath):
continue
for lineno, line in enumerate(open(filepath, encoding="utf-8"), 1):
if line.strip().startswith("```") or line.strip().startswith("`"):
continue
for m in WIKILINK.finditer(line):
target = os.path.splitext(m.group(1).strip())[0].lower()
if target not in known:
add("Links", filepath, lineno, f"Broken wikilink: `[[{m.group(1)}]]`")
for m in MD_LINK.finditer(line):
target = m.group(1).strip()
if target.startswith("mailto:") or target.startswith("/"):
continue
slug = os.path.splitext(target.lstrip("./"))[0].lower()
if slug not in known:
add("Links", filepath, lineno, f"Broken markdown link: `({m.group(1)})`")
# ── Write report ─────────────────────────
with open("/tmp/check_report.json", "w") as f:
json.dump(report, f)
if report:
sys.exit(1)
PYEOF
env:
CHANGED_FILES: ${{ steps.changed.outputs.files }}
# ─────────────────────────────────────────────
# 4. Markdown lint — append to report
# ─────────────────────────────────────────────
- name: Markdown lint
if: always()
run: |
FILES="${{ steps.changed.outputs.files }}"
if [ -z "$FILES" ]; then
FILES=$(find . -name "*.md" | tr '\n' ' ')
fi
# Write output to file — avoids fragile inline variable interpolation into Python
markdownlint $FILES --disable MD013 MD033 MD041 > /tmp/lint_out.txt 2>&1 || true
python3 << 'PYEOF'
import json, re
lint_raw = open("/tmp/lint_out.txt").read()
report_path = "/tmp/check_report.json"
try:
report = json.load(open(report_path))
except Exception:
report = []
pattern = re.compile(r'^(.+?):(\d+)(?::\d+)?\s+(MD\d+\S+.*)$')
added = 0
for line in lint_raw.splitlines():
m = pattern.match(line.strip())
if m:
report.append({
"check": "Markdown Lint",
"file": m.group(1),
"line": int(m.group(2)),
"msg": m.group(3)
})
added += 1
with open(report_path, "w") as f:
json.dump(report, f)
if added:
exit(1)
PYEOF
# ─────────────────────────────────────────────
# 5. Post PR comment with all errors
# ─────────────────────────────────────────────
- name: Post failure comment on PR
if: failure() && github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
let report = [];
try {
report = JSON.parse(fs.readFileSync('/tmp/check_report.json', 'utf8'));
} catch (e) {
core.warning('Could not read report file: ' + e);
}
if (report.length === 0) return;
const groups = {};
for (const item of report) {
if (!groups[item.check]) groups[item.check] = [];
groups[item.check].push(item);
}
const icons = {
"Frontmatter": "📋",
"Emoji": "🔣",
"Links": "🔗",
"Markdown Lint": "📝",
};
let body = `## ❌ Content Validation Failed\n\n`;
body += `Found **${report.length} issue(s)** in the changed files. Please fix them before merging.\n\n---\n\n`;
for (const [check, items] of Object.entries(groups)) {
const icon = icons[check] || "⚠️";
body += `### ${icon} ${check} — ${items.length} issue${items.length > 1 ? 's' : ''}\n\n`;
body += `| File | Line | Issue |\n|------|------|-------|\n`;
for (const item of items) {
body += `| \`${item.file}\` | ${item.line} | ${item.msg} |\n`;
}
body += `\n`;
}
body += `---\n> 💡 Fix the issues above, push again, and this check will re-run automatically.`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existing = comments.find(c =>
c.user.type === 'Bot' && c.body.includes('Content Validation')
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}
# ─────────────────────────────────────────────
# 6. Update comment to green when all fixed
# ─────────────────────────────────────────────
- name: Post success comment on PR
if: success() && github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existing = comments.find(c =>
c.user.type === 'Bot' && c.body.includes('Content Validation')
);
const body = `## ✅ Content Validation Passed\n\nAll checks passed — frontmatter, links, emoji, and markdown syntax look good! Ready to merge. 🎉`;
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}