-
Notifications
You must be signed in to change notification settings - Fork 5
502 lines (427 loc) · 21.9 KB
/
Copy pathexcel_inbox.yaml
File metadata and controls
502 lines (427 loc) · 21.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# .github/workflows/excel_inbox.yaml
#
# Excel Inbox — apply vocabulary changes and validate
# ====================================================
# Triggered when a PR to main touches the inbox/ folder.
#
# Contributor flow:
# 1. Download docs/assets/coremeta4cat_vocabulary.xlsx.
# 2. Edit it (add/modify/delete rows), re-save as coremeta4cat_vocabulary.xlsx.
# 3. Open a PR that places the file at inbox/coremeta4cat_vocabulary.xlsx.
# 4. This workflow runs three dependent jobs:
# a. Job 1 (apply-inbox): Applies the changes to the schema YAML files.
# Saves the modified schema and apply report as CI artifacts.
# b. Job 2 (validate-schema, matrix 3.9–3.13): Validates the modified
# schema on each supported Python version. Appears as five individual
# check runs in the PR — same visibility as main.yaml tests.
# c. Job 3 (finish): Runs a round-trip check (inbox Excel vs the updated
# schema YAMLs), posts a detailed PR comment, then on success commits
# the schema changes back to the branch and removes the inbox file.
# 5. The bot-commit in Job 3 triggers main.yaml to run its full test matrix on
# the updated PR branch (branch-protection required checks are satisfied).
# Because the inbox file is gone, main.yaml's paths-ignore suppresses any
# re-run of excel_inbox.yaml — no infinite loop.
# 6. A maintainer reviews the diff, verifies the bot commit, and merges.
# 7. After merge, update_excel.yaml regenerates docs/assets/coremeta4cat_vocabulary.xlsx.
#
# Security note: uses pull_request_target with two-checkout pattern.
# Contributor-controlled files (the xlsx) are only opened as data, never
# executed. All scripts run from main (checked out in _main_branch/).
# See: https://securitylab.github.com/resources/github-actions-preventing-pwn-requests/
---
name: Excel inbox — apply and validate
on: # yamllint disable-line rule:truthy
pull_request_target:
branches:
- main
types: [opened, reopened, synchronize]
paths:
- "inbox/**"
env:
FORCE_COLOR: "1"
INBOX_FILE: "inbox/coremeta4cat_vocabulary.xlsx"
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
permissions: {}
jobs:
# ══════════════════════════════════════════════════════════════════════════════
# Job 1 — Apply inbox Excel changes to the schema YAMLs
# ══════════════════════════════════════════════════════════════════════════════
apply-inbox:
name: Apply inbox Excel to schema
if: ${{ !github.event.pull_request.merged }}
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read # reads fork PR branch; write is granted only in the finish job
outputs:
inbox_present: ${{ steps.inbox_check.outputs.present }}
apply_status: ${{ steps.apply.outputs.status }}
steps:
# ── Checkout ─────────────────────────────────────────────────────────────
- name: Check out PR branch (fork-safe)
uses: actions/checkout@v6.0.3
with:
# Contributor's branch — only read here; write-back is in the finish job.
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 1
persist-credentials: false
- name: Check out main branch into _main_branch/
# Always run OUR scripts from main — never from the PR.
# This is the key security boundary for pull_request_target.
uses: actions/checkout@v6.0.3
with:
ref: main
path: _main_branch
fetch-depth: 1
persist-credentials: false
# ── Tool setup ───────────────────────────────────────────────────────────
- name: Install uv
uses: astral-sh/setup-uv@v8.2.0
with:
python-version: "3.12"
enable-cache: true
cache-dependency-glob: "uv.lock"
- name: Install project from main branch
run: uv sync --dev
working-directory: _main_branch
# ── Pre-flight ───────────────────────────────────────────────────────────
- name: Check inbox file exists
id: inbox_check
run: |
if [ -f "${{ env.INBOX_FILE }}" ]; then
echo "present=true" >> "$GITHUB_OUTPUT"
echo "Inbox file found: ${{ env.INBOX_FILE }}"
else
echo "present=false" >> "$GITHUB_OUTPUT"
echo "No inbox file at ${{ env.INBOX_FILE }} — nothing to process."
fi
# ── Apply ────────────────────────────────────────────────────────────────
- name: Apply inbox to schema (inbox_to_schema.py)
if: steps.inbox_check.outputs.present == 'true'
id: apply
run: |
# set +e: prevents shell abort on non-zero exit before we save output.
set +e
OUTPUT=$(uv run python scripts/inbox_to_schema.py \
"../${{ env.INBOX_FILE }}" 2>&1)
EXIT_CODE=$?
set -e
echo "$OUTPUT"
# Save to file for the finish job's PR comment.
# printf '%s\n' ensures a trailing newline so GITHUB_ENV delimiter
# parsing in the finish job always finds its closing marker on its own line.
printf '%s\n' "$OUTPUT" > /tmp/apply_output.md
case "$EXIT_CODE" in
0) echo "status=ok" >> "$GITHUB_OUTPUT" ;;
2) echo "status=warnings" >> "$GITHUB_OUTPUT" ;;
*) echo "status=errors" >> "$GITHUB_OUTPUT" ;;
esac
# Fail this step (continue-on-error keeps the job alive for artifact upload).
if [ "$EXIT_CODE" -eq 3 ] || [ "$EXIT_CODE" -eq 1 ]; then
exit 1
fi
working-directory: _main_branch
continue-on-error: true
# ── Artifact upload ──────────────────────────────────────────────────────
- name: Upload apply output (for PR comment in finish job)
if: steps.inbox_check.outputs.present == 'true'
uses: actions/upload-artifact@v4
with:
name: apply-output
path: /tmp/apply_output.md
retention-days: 1
- name: Upload modified schema (for validate-schema and finish jobs)
if: >
steps.inbox_check.outputs.present == 'true' &&
steps.apply.outcome != 'failure'
uses: actions/upload-artifact@v4
with:
name: modified-schema
path: _main_branch/src/coremeta4cat/schema/
retention-days: 1
# ── Gate ────────────────────────────────────────────────────────────────
- name: Fail job if apply errored (after artifacts are saved)
if: steps.apply.outcome == 'failure'
run: |
echo "::error::inbox_to_schema.py reported errors — schema was not modified."
exit 1
# ══════════════════════════════════════════════════════════════════════════════
# Job 2 — Validate the modified schema on every supported Python version
# ══════════════════════════════════════════════════════════════════════════════
validate-schema:
name: Validate schema (Python ${{ matrix.python-version }})
needs: apply-inbox
if: >
!github.event.pull_request.merged &&
needs.apply-inbox.outputs.inbox_present == 'true' &&
needs.apply-inbox.outputs.apply_status != 'errors'
runs-on: ubuntu-latest
timeout-minutes: 20
permissions: {}
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
fail-fast: false
steps:
- name: Check out main branch
# Run the test suite against the main-branch project with the modified
# schema injected from the artifact.
uses: actions/checkout@v6.0.3
with:
ref: main
fetch-depth: 1
persist-credentials: false
- name: Download modified schema
uses: actions/download-artifact@v4
with:
name: modified-schema
path: src/coremeta4cat/schema/
- name: Install uv
uses: astral-sh/setup-uv@v8.2.0
with:
python-version: ${{ matrix.python-version }}
enable-cache: true
cache-dependency-glob: "uv.lock"
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v6.2.0
with:
python-version: ${{ matrix.python-version }}
- name: Install just
run: uv tool install rust-just
- name: Install project
run: uv sync --dev
- name: Regenerate Excel vocabulary from schema
# Mirrors main.yaml: ensures docs/assets/coremeta4cat_vocabulary.xlsx
# reflects the modified schema before any test that reads it runs.
run: just schema-to-excel
- name: Run test suite
run: just test
# ══════════════════════════════════════════════════════════════════════════════
# Job 3 — Round-trip check, post PR comment, commit schema to PR branch
# ══════════════════════════════════════════════════════════════════════════════
finish:
name: Round-trip, comment, and commit
needs: [apply-inbox, validate-schema]
# always() so we post the PR comment even when upstream jobs failed/were skipped.
if: >
always() &&
!github.event.pull_request.merged &&
needs.apply-inbox.outputs.inbox_present == 'true'
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: write # to push the bot-commit back to the PR branch
pull-requests: write # to post/update the PR comment
steps:
# ── Checkout ─────────────────────────────────────────────────────────────
- name: Check out PR branch (fork-safe)
uses: actions/checkout@v6.0.3
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 0
persist-credentials: true
- name: Check out main branch into _main_branch/
uses: actions/checkout@v6.0.3
with:
ref: main
path: _main_branch
fetch-depth: 1
persist-credentials: false
# ── Tool setup ───────────────────────────────────────────────────────────
- name: Install uv
uses: astral-sh/setup-uv@v8.2.0
with:
python-version: "3.12"
enable-cache: true
cache-dependency-glob: "_main_branch/uv.lock"
- name: Install project from main branch
run: uv sync --dev
working-directory: _main_branch
# ── Artifact download ─────────────────────────────────────────────────────
- name: Download apply output
uses: actions/download-artifact@v4
with:
name: apply-output
path: /tmp/apply-output/
- name: Download modified schema (for round-trip check and commit)
if: needs.apply-inbox.outputs.apply_status != 'errors'
uses: actions/download-artifact@v4
with:
name: modified-schema
path: _main_branch/src/coremeta4cat/schema/
continue-on-error: true
- name: Load apply output into environment
run: |
if [ -f /tmp/apply-output/apply_output.md ]; then
# Use a random delimiter so the content can never accidentally contain it.
# The explicit leading \n before the delimiter ensures it lands on its own
# line even when the file was written without a trailing newline.
DELIM="$(openssl rand -hex 16)"
{
printf 'apply_output<<%s\n' "$DELIM"
cat /tmp/apply-output/apply_output.md
printf '\n%s\n' "$DELIM"
} >> "$GITHUB_ENV"
fi
# ── Round-trip check ──────────────────────────────────────────────────────
- name: Round-trip check (inbox Excel vs updated schema)
# excel_to_schema.py resolves SCHEMA_DIR from its own location on disk, so
# it reads the modified YAMLs we just restored from the artifact in
# _main_branch/src/coremeta4cat/schema/ — no separate regen step needed.
if: needs.apply-inbox.outputs.apply_status != 'errors'
id: roundtrip
run: |
set +e
RT_OUTPUT=$(uv run python scripts/excel_to_schema.py \
"../${{ env.INBOX_FILE }}" 2>&1)
RT_EXIT=$?
set -e
echo "$RT_OUTPUT"
{
echo "roundtrip_output<<RT_EOF"
echo "$RT_OUTPUT"
echo "RT_EOF"
} >> "$GITHUB_ENV"
if [ "$RT_EXIT" -eq 0 ] \
&& echo "$RT_OUTPUT" | grep -q "Schema and workbook are fully aligned"; then
echo "status=ok" >> "$GITHUB_OUTPUT"
else
echo "status=diff" >> "$GITHUB_OUTPUT"
fi
working-directory: _main_branch
continue-on-error: true
# ── PR comment ────────────────────────────────────────────────────────────
- name: Post PR comment
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const applyStatus = "${{ needs.apply-inbox.outputs.apply_status }}";
// "success" | "failure" | "skipped" | "cancelled"
const testResult = "${{ needs.validate-schema.result }}";
const rtStatus = "${{ steps.roundtrip.outputs.status }}";
const applyOutput = (process.env.apply_output || "").trim();
const rtOutput = (process.env.roundtrip_output || "").trim();
const applyFailed = applyStatus === "errors";
const testFailed = testResult === "failure";
const testSkipped = testResult === "skipped" || testResult === "cancelled";
const rtDiff = rtStatus === "diff";
const allOk = !applyFailed && !testFailed && !testSkipped && !rtDiff
&& testResult === "success";
const sections = [];
// ── inbox_to_schema.py report ──────────────────────────────────
if (applyOutput) {
sections.push(applyOutput, "");
} else {
sections.push(
"## 📋 Inbox vocabulary — processing report", "",
"*(No output from inbox_to_schema.py)*", ""
);
}
// ── schema validation ──────────────────────────────────────────
if (applyFailed) {
sections.push(
"### ⏭️ Schema validation — skipped", "",
"Tests were not run because the inbox processing step failed.", ""
);
} else {
const icon = testResult === "success" ? "✅"
: testResult === "failure" ? "❌" : "⏭️";
const msg = testResult === "success"
? "All LinkML validation checks passed on Python 3.9 – 3.13."
: testResult === "failure"
? "**LinkML validation failed on one or more Python versions.** "
+ "See the individual *Validate schema (Python …)* check runs for details."
: "Schema validation was skipped or cancelled.";
sections.push(
`### ${icon} Schema validation (\`just test\`, Python 3.9 – 3.13)`, "",
msg, ""
);
}
// ── round-trip check ───────────────────────────────────────────
if (rtStatus) {
const icon = rtStatus === "ok" ? "✅" : "⚠️";
const msg = rtStatus === "ok"
? "The inbox workbook and the updated schema are fully aligned."
: "Some fields in the inbox workbook differ from the updated schema. "
+ "This may indicate fields that could not be automatically converted.";
sections.push(
`### ${icon} Round-trip check`, "",
msg, "",
"<details><summary>Diff output</summary>", "",
"```", rtOutput.slice(0, 4000), "```",
"</details>", ""
);
}
// ── summary ────────────────────────────────────────────────────
sections.push("---", "");
if (allOk) {
sections.push(
"✅ **All checks passed.** Schema changes have been applied to this "
+ "branch. A maintainer will review the diff and merge. "
+ "The vocabulary workbook (`docs/assets/`) will be regenerated "
+ "automatically by `update_excel.yaml` after merge.", ""
);
} else {
sections.push(
"❌ **Some checks failed.** Fix the issues listed above, "
+ "update the workbook, and push again.", ""
);
}
const body = sections.join("\n");
// Update existing bot comment or create a 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("Inbox vocabulary") || c.body.includes("Excel inbox"))
);
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,
});
}
# ── Gate / commit ─────────────────────────────────────────────────────────
- name: Fail on errors (after comment posted)
if: >
needs.apply-inbox.outputs.apply_status == 'errors' ||
needs.validate-schema.result == 'failure'
run: |
echo "::error::Inbox processing failed. See the PR comment for details."
exit 1
- name: Commit schema changes and remove inbox file
if: >
needs.apply-inbox.outputs.apply_status != 'errors' &&
needs.validate-schema.result == 'success'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Copy modified schema YAMLs from _main_branch/ back to PR branch.
mkdir -p src/coremeta4cat/schema/
cp -f _main_branch/src/coremeta4cat/schema/*.yaml \
src/coremeta4cat/schema/
# Stage schema YAMLs and remove the inbox file.
# docs/assets/coremeta4cat_vocabulary.xlsx is NOT committed here —
# update_excel.yaml is the designated owner of that file and will
# regenerate it canonically after this PR is merged to main.
git add src/coremeta4cat/schema/*.yaml
git rm --force "${{ env.INBOX_FILE }}"
# No [skip ci]: this commit triggers main.yaml to run its full test
# matrix on the updated PR branch, satisfying branch-protection checks.
# main.yaml's paths-ignore suppresses re-running excel_inbox.yaml
# because the inbox file is now absent from the PR diff.
git commit -m "ci: apply inbox vocabulary changes"
git push