-
Notifications
You must be signed in to change notification settings - Fork 2
255 lines (214 loc) · 9.95 KB
/
Copy pathvalidate-order.yaml
File metadata and controls
255 lines (214 loc) · 9.95 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
name: Validate order.yaml
on:
pull_request:
branches: [main]
paths:
- "**/order.yaml"
# push:
# branches: [main]
# paths:
# - "**/order.yaml"
permissions:
pull-requests: write
contents: read
jobs:
validate-order:
name: order.yaml Validation
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0
# ─────────────────────────────────────────────
# 1. Set up Python
# ─────────────────────────────────────────────
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install dependencies
run: pip install pyyaml
# ─────────────────────────────────────────────
# 2. Run order.yaml checks
# ─────────────────────────────────────────────
- name: Validate order.yaml
id: check
run: |
python3 << 'PYEOF'
import os, sys, yaml, glob, json
report = []
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)
# Run on every order.yaml in the repo
for oy in glob.glob("**/order.yaml", recursive=True):
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'
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'
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,
});
}