Skip to content
Merged
Changes from 18 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
58 changes: 58 additions & 0 deletions .github/workflows/json_schemas_pr_check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Check JSON schemas consistency

# The json_schemas package contains TypeScript code that generates JSON schema files.
# This workflow ensures that any changes to the TypeScript code are reflected in the generated JSON schema files.
# If there is a mismatch, the workflow fails, prompting the developer to regenerate the JSON schema files.

on:
pull_request:
# List all .ts - .json file pairs that need to be kept in sync
paths:
- 'packages/json_schemas/src/actor.schema.ts'
- 'packages/json_schemas/schemas/actor.schema.json'

jobs:
check_json_schemas:
name: 'Check JSON schemas consistency'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v5
with:
node-version: ${{ matrix.node-version }}

- name: Cache node_modules
uses: actions/cache@v4
with:
path: '**/node_modules'
key: ${{ runner.os }}-${{ matrix.node-version }}-modules-${{ hashFiles('**/package-lock.json') }}

- name: Install Dependencies
run: npm ci --no-audit
- run: npm run build

- name: Check JSON consistency
run: |
files_to_check=(
"packages/json_schemas/src/actor.schema.ts|packages/json_schemas/schemas/actor.schema.json"
# add more ts|json pairs here
)

failed=0

for pair in "${files_to_check[@]}"; do
ts_file="${pair%%|*}"
json_file="${pair##*|}"

if ! git diff --exit-code "$json_file" >/dev/null; then
echo "ERROR: $json_file is out of sync with $ts_file."
echo "Please put the schema changes to .ts file ($ts_file) and update the .json file ($json_file) by running the build script (npm run build)."
failed=1
fi
done

if [ $failed -eq 1 ]; then
exit 1
fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit overcomplicated, any reason you just don't diff the whole project? It will be fast enough, surely faster than running npm ci, building things or checking out the repo itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would literally just call git diff --exit-code, it will make the job fail if there are changes.

npm ci --no-audit
npm run build
git diff --exit-code

And this could be part of the regular test workflow, since there you need to build the project anyway, so why do it again separately. Project build should never produce any git changes, so it's a nice safeguard in general, not just for the JSON schema files.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this helps with DX. If someone who never saw this repository comes to update actor.json schema he would need to dig deeper to debug the failing workflow. With this he would just see exact instructions:

ERROR: packages/json_schemas/schemas/actor.schema.json is out of sync with packages/json_schemas/src/actor.schema.ts.
Please put the schema changes to .ts file (packages/json_schemas/src/actor.schema.ts) and update the .json file (packages/json_schemas/schemas/actor.schema.json) by running the build script (npm run build).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could do what @B4nan suggests, and just write out the message with exact instructions? It's pretty likely this is not going to change, and if it was supposed to change, we'd need to update the workflow anyway.