Skip to content

Commit 9449f8c

Browse files
authored
chore: add checks for schema changes (#1992)
* chore: add pre-commit check for API schema sync Signed-off-by: Luiz Georg <luiz.georg@profusion.mobi> * chore: add pre-commit checks to CI workflow This removes code duplication and makes the CI checks consistent with the development environment. Signed-off-by: Luiz Georg <luiz.georg@profusion.mobi> * fixup! chore: add pre-commit checks to CI workflow Signed-off-by: Luiz Georg <luiz.georg@profusion.mobi> * fixup! chore: add pre-commit check for API schema sync Signed-off-by: Luiz Georg <luiz.georg@profusion.mobi> --------- Signed-off-by: Luiz Georg <luiz.georg@profusion.mobi>
1 parent 9b41c9c commit 9449f8c

2 files changed

Lines changed: 41 additions & 19 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ jobs:
4444
run: pnpm install --frozen-lockfile
4545
working-directory: ./dashboard
4646

47-
lint-js:
48-
if: github.event.pull_request.draft != true
49-
needs: setup-node
47+
pre-commit-checks:
5048
runs-on: ubuntu-latest
51-
timeout-minutes: 5
49+
needs: setup-node
50+
timeout-minutes: 10
5251
steps:
53-
- uses: actions/checkout@v4
52+
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
53+
- uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0
54+
with:
55+
python-version: 3.12
5456

5557
- name: Fetch node_modules
5658
uses: actions/cache@v4
@@ -68,9 +70,15 @@ jobs:
6870
with:
6971
node-version-file: './dashboard/.nvmrc'
7072

71-
- name: Run eslint
72-
run: pnpm lint-staged
73-
working-directory: ./dashboard
73+
- name: Cache Pre-Commit Hooks
74+
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
75+
with:
76+
path: ~/.cache/pre-commit
77+
key: pre-commit-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('.pre-commit-config.yaml') }}
78+
- name: Setup pre-commit
79+
run: pip install pre-commit
80+
- name: Run pre-commit hooks
81+
run: pre-commit run --all-files --show-diff-on-failure
7482

7583
build-front:
7684
if: github.event.pull_request.draft != true
@@ -104,7 +112,7 @@ jobs:
104112
run: pnpm build
105113
working-directory: ./dashboard
106114

107-
lint-and-unit-test-django:
115+
unit-test-django:
108116
if: github.event.pull_request.draft != true
109117
runs-on: ubuntu-latest
110118
timeout-minutes: 3
@@ -114,14 +122,6 @@ jobs:
114122
- name: Setup Backend
115123
uses: ./.github/actions/setup-backend
116124

117-
- name: Check Lint
118-
run: poetry run ruff check .
119-
working-directory: ./backend
120-
121-
- name: Check Format
122-
run: poetry run ruff format --check .
123-
working-directory: ./backend
124-
125125
- name: Run unit tests with coverage
126126
run: |
127127
poetry run pytest -m unit --cov=kernelCI_app --cov=kernelCI_cache --cov-report=term-missing
@@ -183,7 +183,7 @@ jobs:
183183
if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
184184
runs-on: ubuntu-latest
185185
needs:
186-
- lint-and-unit-test-django
186+
- unit-test-django
187187
- integration-test-django
188188
permissions:
189189
contents: write
@@ -220,7 +220,7 @@ jobs:
220220

221221
# Call job in a a split file
222222
call-deploy-staging:
223-
needs: [lint-js, build-front, lint-and-unit-test-django, integration-test-django]
223+
needs: [pre-commit-checks, build-front, unit-test-django, integration-test-django]
224224
# This 'if' ensures it only runs on pushes to main (the production requirement)
225225
if: github.event_name == 'push' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
226226
uses: ./.github/workflows/deploy-staging.yaml

.pre-commit-config.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,25 @@ repos:
7474
- --
7575
language: system
7676
always_run: true
77+
78+
- id: api-schema
79+
name: API schema is up-to-date
80+
entry: |
81+
python -c '
82+
import os, subprocess, sys;
83+
os.chdir("backend")
84+
subprocess.run(["poetry", "install"], capture_output=True)
85+
spectacular = subprocess.run(["poetry", "run", "python", "manage.py", "spectacular"], capture_output=True, text=True)
86+
new_schema = spectacular.stdout
87+
with open("schema.yml") as f:
88+
old_schema = f.read()
89+
if new_schema != old_schema:
90+
print("Schema is not up-to-date. Run \"python manage.py spectacular --file schema.yml\" to regenerate it")
91+
sys.exit(1)
92+
'
93+
files: 'backend/(.*\.py|schema.yml)'
94+
pass_filenames: false
95+
language: python
96+
language_version: '3.12'
97+
additional_dependencies: [poetry]
98+
stages: [pre-commit]

0 commit comments

Comments
 (0)