Repository navigation
322 lines (278 loc) · 14 KB
/
Copy pathvalidate-order.yaml
File metadata and controls
322 lines (278 loc) · 14 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
name: Validate order.yaml
on:
pull_request:
branches: [master]
paths:
- "**/_ordering.yaml"
- "**/order.yaml"
- "**/ordering.yaml"
pull_request_target:
branches: [master]
paths:
- "**/_ordering.yaml"
- "**/order.yaml"
- "**/ordering.yaml"
# push:
# branches: [master]
# paths:
# - "**/_ordering.yaml"
permissions:
issues: write
pull-requests: write
contents: read
jobs:
validate-order:
name: order.yaml Validation
runs-on: ubuntu-latest
steps:
- name: Checkout base
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Checkout PR head (for pull_request_target)
if: github.event_name == 'pull_request_target'
run: |
git fetch origin pull/${{ github.event.pull_request.number }}/head:pr-head
git checkout pr-head
- name: Fetch base branch for diff
if: github.event_name == 'pull_request' || github.event_name == 'pull_request_target'
run: git fetch origin ${{ github.base_ref }} 2>/dev/null || git fetch origin master || true
# ─────────────────────────────────────────────
# 1. Collect changed order files (PR context)
# ─────────────────────────────────────────────
- name: Get changed order files
id: changed
run: |
if [ "${{ github.event_name }}" = "pull_request" ] || [ "${{ github.event_name }}" = "pull_request_target" ]; then
FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD -- '*order*.yaml' '*_ordering.yaml' | tr '\n' ' ')
else
FILES=$(git diff --name-only HEAD~1 HEAD -- '*order*.yaml' '*_ordering.yaml' | tr '\n' ' ')
fi
echo "Changed order files: $FILES"
echo "files=$FILES" >> $GITHUB_OUTPUT
# ─────────────────────────────────────────────
# 2. Set up Python
# ─────────────────────────────────────────────
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install dependencies
run: pip install pyyaml
# ─────────────────────────────────────────────
# 3. Run order.yaml checks
# ─────────────────────────────────────────────
- name: Validate order.yaml
id: check
env:
CHANGED_ORDER_FILES: ${{ steps.changed.outputs.files }}
GITHUB_EVENT_NAME: ${{ github.event_name }}
run: |
python3 << 'PYEOF'
import os, sys, yaml, glob, json
report = []
# In PR mode, only validate changed ordering files to avoid legacy debt blocking PRs
# This follows quartz render: ordering file is optional, missing entries just mean alphabetical fallback
CHANGED_ORDER = os.environ.get("CHANGED_ORDER_FILES", "").split()
def add(yaml_path, msg):
report.append({"file": yaml_path, "msg": msg})
def check_order_yaml(yaml_path):
base_dir = os.path.dirname(yaml_path) or "."
# ── Parse YAML ───────────────────────
try:
data = yaml.safe_load(open(yaml_path, encoding="utf-8"))
except yaml.YAMLError as e:
add(yaml_path, f"Invalid YAML syntax: {e}")
return
if not isinstance(data, dict) or "order" not in data:
add(yaml_path, "Missing top-level `order:` key")
return
seen = set()
def walk(entries, context_dir):
if not isinstance(entries, list):
return
for entry in entries:
if not isinstance(entry, dict):
continue
# ── file entry ──────────────
if "file" in entry:
name = str(entry["file"])
# No .md extension allowed
if name.endswith(".md"):
add(yaml_path,
f"`file: {name}` — remove the `.md` extension, bare filename only")
# Duplicate check
dup_key = f"file:{context_dir}/{name}"
if dup_key in seen:
add(yaml_path,
f"Duplicate entry: `file: {name}` listed more than once under `{context_dir}/`")
else:
seen.add(dup_key)
# File existence check
bare = name.removesuffix(".md")
exists = any(os.path.isfile(p) for p in [
os.path.join(context_dir, bare + ".md"),
os.path.join(context_dir, bare),
])
if not exists:
add(yaml_path,
f"`file: {name}` — not found in `{context_dir}/`")
# Recurse into nested order
if "order" in entry:
walk(entry["order"], os.path.join(context_dir, bare))
# ── folder entry ────────────
elif "folder" in entry:
name = str(entry["folder"])
# Duplicate check
dup_key = f"folder:{context_dir}/{name}"
if dup_key in seen:
add(yaml_path,
f"Duplicate entry: `folder: {name}` listed more than once under `{context_dir}/`")
else:
seen.add(dup_key)
# Folder existence check
folder_path = os.path.join(context_dir, name)
if not os.path.isdir(folder_path):
add(yaml_path,
f"`folder: {name}` — directory not found at `{folder_path}/`")
# Recurse into nested order
if "order" in entry:
walk(entry["order"], folder_path)
walk(data["order"], base_dir)
# Determine which ordering files to validate
# Quartz engine uses _ordering.yaml at repo root (orderFromYAML.ts), but CONTRIBUTING mentions order.yaml per folder
# Support both to avoid silent skip (previous bug: only **/order.yaml missed _ordering.yaml)
is_pr = os.environ.get("GITHUB_EVENT_NAME", "") in ("pull_request", "pull_request_target")
if CHANGED_ORDER:
# PR mode: only check files touched in this PR (avoids legacy _ordering.yaml debt)
ordering_files = [f for f in CHANGED_ORDER if f and os.path.isfile(f)]
if not ordering_files:
ordering_files = [f for f in CHANGED_ORDER if f]
ordering_files = [f for f in ordering_files if "order" in f]
print(f"Validating changed ordering files: {ordering_files}")
elif is_pr:
# PR without order changes: skip full scan to avoid legacy debt blocking unrelated PRs
print("No changed ordering files in this PR — skipping order validation (legacy _ordering.yaml not checked)")
ordering_files = []
else:
# Full scan (push or workflow_dispatch)
ordering_files = set(glob.glob("**/_ordering.yaml", recursive=True))
ordering_files.update(glob.glob("**/order.yaml", recursive=True))
ordering_files.update(glob.glob("**/ordering.yaml", recursive=True))
if os.path.isfile("_ordering.yaml"):
ordering_files.add("_ordering.yaml")
ordering_files = sorted(ordering_files)
print(f"Validating all ordering files: {ordering_files}")
if not ordering_files:
print("⚠ No ordering file to validate — passing")
for oy in ordering_files:
if not os.path.isfile(oy):
add(oy, "File not found (deleted in this PR?)")
continue
check_order_yaml(oy)
# Write report
with open("/tmp/order_report.json", "w") as f:
json.dump(report, f)
if report:
print(f"\n❌ Found {len(report)} issue(s) in order.yaml files:")
for item in report:
print(f" {item['file']}: {item['msg']}")
sys.exit(1)
else:
print("✅ All order.yaml files are valid.")
PYEOF
# ─────────────────────────────────────────────
# 3. Post failure comment on PR
# ─────────────────────────────────────────────
- name: Post failure comment on PR
if: ${{ failure() && (github.event_name == 'pull_request_target' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository)) }}
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
let report = [];
try {
report = JSON.parse(fs.readFileSync('/tmp/order_report.json', 'utf8'));
} catch (e) {
core.warning('Could not read order report: ' + e);
}
if (report.length === 0) return;
// Group by file
const groups = {};
for (const item of report) {
if (!groups[item.file]) groups[item.file] = [];
groups[item.file].push(item.msg);
}
let body = `## 🗂️ order.yaml Validation Failed\n\n`;
body += `Found **${report.length} issue(s)**. Please fix them before merging.\n\n---\n\n`;
for (const [file, msgs] of Object.entries(groups)) {
body += `### \`${file}\`\n\n`;
body += `| # | Issue |\n|---|-------|\n`;
msgs.forEach((msg, i) => {
body += `| ${i + 1} | ${msg} |\n`;
});
body += `\n`;
}
body += `---\n`;
body += `> 💡 Fix the issues above, push again, and this check will re-run automatically.\n\n`;
body += `**Quick reference:**\n`;
body += `- Use bare filenames: \`file: my-note\` not \`file: my-note.md\`\n`;
body += `- Every \`file:\` must have a matching \`.md\` file on disk\n`;
body += `- Every \`folder:\` must be a real directory\n`;
body += `- No duplicate entries under the same parent`;
// Update existing bot comment or create new one
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('order.yaml 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,
});
}
# ─────────────────────────────────────────────
# 4. Post success comment on PR
# ─────────────────────────────────────────────
- name: Post success comment on PR
if: ${{ success() && (github.event_name == 'pull_request_target' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository)) }}
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('order.yaml Validation')
);
const body = `## 🗂️ ✅ order.yaml Validation Passed\n\nAll \`order.yaml\` files are valid — no missing files, folders, duplicates, or \`.md\` extensions found. 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,
});
}