Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions .github/workflows/apply-test-ids.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
name: apply-test-ids

on:
pull_request_target:
types: [opened, synchronize]
paths:
- 'tests/**/*.json'

permissions:
contents: write
pull-requests: write

jobs:
apply:
runs-on: ubuntu-latest

steps:
# 1. Detect what changed in this PR
- name: Check changed paths
uses: dorny/paths-filter@v3
id: filter
with:
filters: |
tests:
- 'tests/**/*.json'
scripts:
- 'scripts/**'
- 'package.json'
- 'package-lock.json'

# 2. Fail if both tests AND scripts changed together
- name: Block mixed changes
if: steps.filter.outputs.tests == 'true' && steps.filter.outputs.scripts == 'true'
run: |
echo "This PR changes both test files and scripts at the same time."
echo "Please split them into separate PRs."
exit 1

# 3. Checkout repo
- name: Checkout
if: steps.filter.outputs.tests == 'true'
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0

# 4. Setup Node.js
- name: Setup Node.js
if: steps.filter.outputs.tests == 'true'
uses: actions/setup-node@v4
with:
node-version: 18

# 5. Install dependencies
- name: Install dependencies
if: steps.filter.outputs.tests == 'true'
run: npm ci

# 6. Fetch list of files changed in this PR
- name: Get changed test files
if: steps.filter.outputs.tests == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
curl -s \
-H "Authorization: Bearer $GITHUB_TOKEN" \
"${{ github.event.pull_request.url }}/files?per_page=100" \
| jq -r ".[].filename" \
> changed-files.txt

grep -E "^tests/(draft[^/]+)/.*\.json$" changed-files.txt \
| sed -E "s|^tests/([^/]+)/.*|\1|" \
| sort -u \
> affected-drafts.txt

# 7. Run add-test-ids.js for each changed file
- name: Apply missing test IDs
if: steps.filter.outputs.tests == 'true'
env:
CI: "false"
run: |
if [ ! -s affected-drafts.txt ]; then
echo "No test JSON files changed - nothing to do."
exit 0
fi

while IFS= read -r draft; do
echo "Processing dialect: $draft"
grep -E "^tests/${draft}/.*\.json$" changed-files.txt | while IFS= read -r file; do
if [ -f "$file" ]; then
echo " -> $file"
node scripts/add-test-ids.js "$draft" "$file"
fi
done
done < affected-drafts.txt

# 8. Commit and push back to the PR branch
- name: Commit and push changes
if: steps.filter.outputs.tests == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.name "test-id-bot"
git config user.email "[email protected]"

git add "tests/**/*.json"

if git diff --cached --quiet; then
echo "No test ID changes needed - nothing to commit."
echo "CHANGES_MADE=false" >> $GITHUB_ENV
else
SUMMARY=""
TOTAL=0
while IFS= read -r file; do
COUNT=$(git diff --cached -- "$file" | grep "^+" | grep '"id":' | wc -l | tr -d " ")
if [ "$COUNT" -gt 0 ]; then
SUMMARY="${SUMMARY}\n- \`${file}\` - **${COUNT}** ID(s) added"
TOTAL=$((TOTAL + COUNT))
fi
done < <(git diff --cached --name-only)

printf "%b" "$SUMMARY" > /tmp/summary.txt
echo "TOTAL_IDS=${TOTAL}" >> $GITHUB_ENV

git commit -m "chore: auto-add missing test IDs"

COMMIT_SHA=$(git rev-parse HEAD)
echo "COMMIT_SHA=${COMMIT_SHA}" >> $GITHUB_ENV

git push \
https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.event.pull_request.head.repo.full_name }}.git \
HEAD:refs/heads/${{ github.event.pull_request.head.ref }}

echo "CHANGES_MADE=true" >> $GITHUB_ENV
fi

# 9. Post a summary comment on the PR
- name: Post PR comment
if: env.CHANGES_MADE == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
SUMMARY=$(cat /tmp/summary.txt)
COMMIT_URL="https://github.com/${{ github.event.pull_request.head.repo.full_name }}/commit/${COMMIT_SHA}"

BODY="### test-id-bot added ${TOTAL_IDS} missing test ID(s)\n\n${SUMMARY}\n\nView the full diff of added IDs: ${COMMIT_URL}\n\n> IDs are deterministic hashes based on the schema, test data, and expected result."

curl -s -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
"${{ github.event.pull_request.comments_url }}" \
--data "$(jq -n --arg body "$(printf '%b' "$BODY")" '{body: $body}')"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
node_modules/
172 changes: 172 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
{
"name": "json-schema-test-suite",
"version": "0.1.0",
"type": "module",
"description": "A language agnostic test suite for the JSON Schema specifications",
"repository": "github:json-schema-org/JSON-Schema-Test-Suite",
"keywords": [
"json-schema",
"tests"
],
"author": "http://json-schema.org",
"license": "MIT"
"license": "MIT",
"dependencies": {
"@hyperjump/browser": "^1.3.1",
"@hyperjump/json-pointer": "^1.1.1",
"@hyperjump/json-schema": "^1.17.2",
"@hyperjump/pact": "^1.4.0",
"@hyperjump/uri": "^1.3.2",
"json-stringify-deterministic": "^1.0.12"
},
"devDependencies": {
"jsonc-parser": "^3.3.1"
}
}
Loading
Loading