|
| 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