Skip to content

Commit 4373a68

Browse files
authored
chore: integrate pre-commit hooks (baserow#5392)
* chore: integrate pre-commit hooks for backend and frontend code quality enforcement * fix: uv.lock * refactor: consolidate pre-commit hooks to use just recipes and add a pre-commit-uninstall command * docs: document pre-commit branch linting and update pre-commit configuration to increase file size limits and remove serial execution requirements.
1 parent 5974708 commit 4373a68

8 files changed

Lines changed: 240 additions & 22 deletions

File tree

.pre-commit-config.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Pre-commit hooks for Baserow.
2+
#
3+
# The lint/format hooks intentionally delegate to the `just` recipes
4+
# (`just b fix` and `just f fix`) so that pre-commit, CI and manual runs all
5+
# use the exact same commands and configuration. This avoids drift between
6+
# the tools and re-running `just fix` after pre-commit will not produce
7+
# additional changes.
8+
repos:
9+
- repo: https://github.com/pre-commit/pre-commit-hooks
10+
rev: v5.0.0
11+
hooks:
12+
- id: check-yaml
13+
# Helm templates and multi-document GitLab CI files are not valid
14+
# standalone YAML and are validated by their own tooling.
15+
args: [--allow-multiple-documents]
16+
exclude: ^(web-frontend/\.nuxt/|web-frontend/dist/|web-frontend/\.output/|deploy/helm/|\.gitlab/)
17+
- id: check-added-large-files
18+
# Built-in templates ship as multi-MB .zip fixtures; bump the limit so
19+
# those are allowed while still flagging unexpectedly large additions.
20+
args: [--maxkb=5000]
21+
- id: check-merge-conflict
22+
23+
- repo: local
24+
hooks:
25+
- id: backend-fix
26+
name: backend lint & format (just b fix)
27+
entry: just b fix
28+
language: system
29+
pass_filenames: false
30+
files: \.py$
31+
32+
- id: frontend-fix
33+
name: frontend lint & format (just f fix)
34+
entry: just f fix
35+
language: system
36+
pass_filenames: false
37+
files: \.(js|jsx|ts|tsx|vue|mjs|css|scss|json|md|html)$
38+
exclude: ^(web-frontend/\.nuxt/|web-frontend/dist/|web-frontend/\.output/)

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Use `just` from the repo root; it wraps the backend and frontend workflows consi
1313
- `just b test -n=auto` runs backend pytest suites in parallel.
1414
- `just f test` runs frontend Vitest suites.
1515
- `just lint` runs both backend and frontend linters; `just fix` applies auto-fixes.
16+
- `just b run pre-commit run --files $(git diff --name-only HEAD)` lints only the files you've touched on your branch (staged + unstaged); use `origin/develop...HEAD` instead of `HEAD` to scope to the whole branch. See `docs/development/code-quality.md` for details.
1617
- `just b migrate` runs Django migrations.
1718

1819
For direct package-manager use, backend commands run through `uv` and frontend commands through `yarn`.

backend/justfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ check: _check-dev
207207
#!/usr/bin/env bash
208208
set -euo pipefail
209209
{{ _load_env }}
210-
210+
211211
{{ uv_run }} ruff check --config pyproject.toml {{ backend_source_dirs }} {{ backend_tests_dirs }}
212212
{{ uv_run }} ruff format --config pyproject.toml --check {{ backend_source_dirs }} {{ backend_tests_dirs }}
213213
214-
alias lint := check
214+
alias lint := check
215215
alias l := check
216216

217217
# Fix code style (sort imports + format)
@@ -340,7 +340,7 @@ check-migrations: _check-dev
340340
#!/usr/bin/env bash
341341
set -euo pipefail
342342
{{ _load_env }}
343-
343+
344344
DJANGO_SETTINGS_MODULE={{ django_settings }} {{ uv_run }} baserow makemigrations --dry-run --check
345345
346346
# =============================================================================

backend/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ baserow = "baserow.manage:main"
114114

115115
[dependency-groups]
116116
dev = [
117+
"pre-commit>=4.0.0",
117118
"ruff>=0.8.0",
118119
"pytest==9.0.3",
119120
"pytest-django==4.12.0",

backend/uv.lock

Lines changed: 102 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/development/code-quality.md

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,87 @@ If you have the [development environment](./running-the-dev-env-locally.md) up a
1010
you can easily run the linters using [just](./justfile.md) commands.
1111

1212
**Backend (from project root or `backend/` directory):**
13-
* `just b format`: auto format all Python code using black.
14-
* `just b sort`: sort imports using isort.
15-
* `just b fix`: run both format and sort.
16-
* `just b lint`: check Python code with flake8, black, isort, and bandit.
13+
14+
- `just b format`: auto-format all Python code using Ruff formatter.
15+
- `just b fix`: run Ruff checks with automatic fixes and format Python code.
16+
- `just b lint`: check Python code with Ruff.
1717

1818
**Frontend (from project root or `web-frontend/` directory):**
19-
* `just f lint`: check JavaScript with eslint and SCSS with stylelint.
20-
* `just f fix`: auto-fix code style issues.
19+
20+
- `just f lint`: check JavaScript and SCSS files with ESLint and Stylelint.
21+
- `just f fix`: auto-fix code style issues.
2122

2223
## Running tests
2324

2425
There are also commands to easily run the tests.
2526

26-
* `just b test` (backend): run all backend Python tests with pytest.
27-
* `just b test -n=auto` (backend): run tests in parallel for faster execution.
28-
* `just f test` (frontend): run all frontend tests with Jest.
27+
- `just b test` (backend): run all backend Python tests with pytest.
28+
- `just b test -n=auto` (backend): run tests in parallel for faster execution.
29+
- `just f test` (frontend): run all frontend tests with Jest.
30+
31+
## Git pre-commit hooks
32+
33+
Baserow uses [`pre-commit`](https://pre-commit.com/) to automatically run linters and
34+
formatters before commits are created. This ensures your changes comply with repo-wide
35+
code quality rules without waiting for CI feedback.
36+
37+
The lint/format hooks delegate to the same `just b fix` and `just f fix` recipes used
38+
by CI and manual runs, so pre-commit will never produce changes that differ from
39+
running `just fix` yourself.
40+
41+
### Installation
42+
43+
To set up the pre-commit hooks locally in your `.git/` folder, run the following command from the repository root:
44+
45+
```bash
46+
just pre-commit-install
47+
```
48+
49+
This registers a few general hygiene hooks (YAML syntax checks, merge-conflict
50+
markers, large files) plus the backend and frontend lint/format hooks. Trailing
51+
whitespace and end-of-file fixes are intentionally left to `ruff`/`prettier` to
52+
avoid touching legacy files.
53+
54+
To remove the hooks again:
55+
56+
```bash
57+
just pre-commit-uninstall
58+
```
59+
60+
### Running manually
61+
62+
You can also run pre-commit manually at any time against all files or staged changes:
63+
64+
```bash
65+
# Run against all files
66+
just b run pre-commit run --all-files
67+
68+
# Run against specific files
69+
just b run pre-commit run --files path/to/file1.py path/to/file2.js
70+
```
71+
72+
#### Tip: lint everything you've touched on this branch
73+
74+
A normal `git commit` runs the hooks on **staged files only**, and `pre-commit run`
75+
with no arguments does the same. To lint every file you have changed relative to
76+
`HEAD` (staged *and* unstaged), pass the diff explicitly:
77+
78+
```bash
79+
just b run pre-commit run --files $(git diff --name-only HEAD)
80+
```
81+
82+
This is handy before opening a pull request: it scopes the run to your
83+
work-in-progress without re-linting the entire monorepo the way
84+
`--all-files` does. Swap `HEAD` for a base ref (for example `origin/develop`) to
85+
lint everything your branch changes:
86+
87+
```bash
88+
just b run pre-commit run --files $(git diff --name-only origin/develop...HEAD)
89+
```
90+
91+
See the [pre-commit documentation](https://pre-commit.com/#usage) for more advanced
92+
usage (skipping hooks for a commit, running a single hook, updating hook versions,
93+
etc.).
2994

3095
## Continuous integration
3196

0 commit comments

Comments
 (0)