Skip to content

Commit fb13318

Browse files
committed
Add schema validation workflow and update detect-file-changes github action to output the detected files
1 parent a7805bd commit fb13318

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
lines changed

.github/actions/detect-file-changes/action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ runs:
1515
outputs:
1616
changed:
1717
description: "Returns `true` if any changed file matches the specified patterns, `false` otherwise."
18+
changed_files:
19+
description: "Returns a space-separated list of all changed files that match the specified patterns, or an empty list if none match."

.github/actions/detect-file-changes/detect-file-changes.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,23 @@ function fileMatches(file, compiled) {
232232
changedFiles.forEach(f => console.log(`- ${f}`));
233233

234234
// Check if any file matches
235-
const matched = changedFiles.some(f => fileMatches(f, compiled));
235+
const matchedFiles = changedFiles.filter(f => fileMatches(f, compiled));
236+
const matched = matchedFiles.length > 0;
236237

237-
// Write GitHub Action output
238+
// Write GitHub Action outputs
238239
fs.appendFileSync(
239240
process.env.GITHUB_OUTPUT,
240241
`changed=${matched ? 'true' : 'false'}\n`
241242
);
242243

243-
console.log(`Files match filter → ${matched}`);
244+
// Write the list of matched files as a multiline output
245+
fs.appendFileSync(
246+
process.env.GITHUB_OUTPUT,
247+
`changed_files=${matchedFiles.join(' ')}\n`
248+
);
249+
250+
console.log(`Files matching filter (${matchedFiles.length}):`);
251+
matchedFiles.forEach(f => console.log(`- ${f}`));
244252
} catch (err) {
245253
console.log(`file-filter encountered an error: ${err?.message || err}`);
246254
process.exit(0); // Non-fatal for CI
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: "Validate index.json files"
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
validate-json:
8+
name: "📋 Validate changed JSON files"
9+
runs-on: ubuntu-22.04
10+
timeout-minutes: 10
11+
12+
steps:
13+
- name: "☁️ Checkout repository"
14+
uses: actions/checkout@v4
15+
16+
- name: "🔎 Detect relevant file changes"
17+
id: filter
18+
uses: ./.github/actions/detect-file-changes
19+
with:
20+
file-patterns: |
21+
- 'metadata/index.json'
22+
- 'metadata/*/*/index.json'
23+
- 'tests/src/index.json'
24+
- 'tests/src/*/*/*/index.json'
25+
github_token: ${{ secrets.GITHUB_TOKEN }}
26+
27+
- name: "Setup Python"
28+
if: steps.filter.outputs.changed == 'true'
29+
uses: actions/setup-python@v4
30+
with:
31+
python-version: '3.10'
32+
33+
- name: "Install validation tools"
34+
if: steps.filter.outputs.changed == 'true'
35+
run: |
36+
pip install jq check-jsonschema
37+
38+
- name: "Check that the changed index.json files conform to their schemas"
39+
if: steps.filter.outputs.changed == 'true'
40+
run: |
41+
set -e
42+
43+
# Treat changed_files as a proper array
44+
FILES=(${{ steps.filter.outputs.changed_files }})
45+
46+
# Track failures
47+
FAILURES=()
48+
49+
for FILE in "${FILES[@]}"; do
50+
51+
# Determine schema for each file
52+
if [[ "$FILE" == "metadata/index.json" ]]; then
53+
SCHEMA="schemas/metadata-root-index-schema-v1.0.0.json"
54+
elif [[ "$FILE" == metadata/*/*/index.json ]]; then
55+
SCHEMA="schemas/metadata-library-index-schema-v1.0.0.json"
56+
elif [[ "$FILE" == "tests/src/index.json" ]]; then
57+
SCHEMA="schemas/tests-root-index-schema-v1.0.0.json"
58+
elif [[ "$FILE" == tests/src/*/*/*/index.json ]]; then
59+
SCHEMA="schemas/tests-project-index-schema-v1.0.0.json"
60+
else
61+
echo "ℹ️ Skipping: $FILE (no schema mapping)"
62+
continue
63+
fi
64+
65+
echo "🔍 Validating $FILE using $SCHEMA"
66+
67+
# Step 1: Check JSON well-formedness
68+
if ! jq -e . "$FILE" >/dev/null; then
69+
echo "❌ $FILE is not valid JSON"
70+
FAILURES+=("$FILE (invalid JSON)")
71+
continue
72+
fi
73+
74+
# Step 2: Validate against schema
75+
if ! check-jsonschema --schemafile "$SCHEMA" "$FILE"; then
76+
FAILURES+=("$FILE (schema validation failed)")
77+
continue
78+
fi
79+
80+
echo "✅ $FILE validated successfully"
81+
done
82+
83+
# Fail at the end if any errors occurred
84+
if [ ${#FAILURES[@]} -ne 0 ]; then
85+
echo "::error ::Some files failed validation:"
86+
for f in "${FAILURES[@]}"; do
87+
echo "::error :: $f"
88+
done
89+
exit 1
90+
fi

0 commit comments

Comments
 (0)