Skip to content

Commit b8d24ee

Browse files
Feat: Replace flake8, black and husky with ruff + pre-commit (#1846)
* feat: move from black and flake8 to ruff Not formatting yet, just configs * feat: replace husky with pre-commit Replaces husky as a hook manager with pre-commit manager as per new contributing guidelines. This also improves the readability of the hooks as everything is in a single file. * docs: update lint, format and hook docs Closes #1845 * feat: add ignore-revs file This file will ignore the refformating PR when using git-blame, so that we don't lose track of previous changes
1 parent 05e2a3a commit b8d24ee

15 files changed

Lines changed: 351 additions & 238 deletions

.git-blame-ignore-revs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
05e2a3ae5e878aa90e6d6e0ac20179a00f6652e4 # Ruff auto-formatting

.github/workflows/ci.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ jobs:
114114
- name: Setup Backend
115115
uses: ./.github/actions/setup-backend
116116

117-
- name: Lint
118-
run: poetry run flake8
117+
- name: Check Lint
118+
run: poetry run ruff check .
119119
working-directory: ./backend
120120

121121
- name: Check Format
122-
run: poetry run black --check .
122+
run: poetry run ruff format --check .
123123
working-directory: ./backend
124124

125125
- name: Run unit tests with coverage

.pre-commit-config.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: v0.15.9
4+
hooks:
5+
- id: ruff-format
6+
files: ^backend/
7+
args: [--check]
8+
- id: ruff-check
9+
files: ^backend/
10+
11+
- repo: local
12+
hooks:
13+
- id: frontend-lint-staged
14+
name: frontend lint-staged
15+
entry: bash -lc 'cd dashboard && pnpm lint-staged'
16+
language: system
17+
pass_filenames: false
18+
stages: [pre-commit]
19+
20+
- id: frontend-typecheck
21+
name: frontend typecheck
22+
entry: bash -lc 'cd dashboard && pnpm typecheck'
23+
language: system
24+
pass_filenames: false
25+
stages: [pre-commit]
26+
27+
- id: frontend-pre-push
28+
name: frontend pre-push
29+
entry: bash
30+
args:
31+
- -lc
32+
- |
33+
REMOTE="$1"
34+
if [ -z "$MASTER_REF" ]; then
35+
MASTER_REF="$REMOTE/main"
36+
fi
37+
38+
if ! read LOCAL_REF LOCAL_SHA REMOTE_REF REMOTE_SHA; then
39+
echo "NOTE: nothing to push, nothing to check"
40+
exit 0
41+
fi
42+
43+
if [ "$LOCAL_REF" = "(delete)" ]; then
44+
echo "NOTE: deleting branch, nothing to check"
45+
exit 0
46+
fi
47+
48+
exit 0
49+
language: system
50+
pass_filenames: false
51+
stages: [pre-push]

CONTRIBUTING.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ KernelCI Dashboard is an open-source project and contributions of all kinds are
88
- We recommend following the [Onboarding guide](./docs/Onboarding.md) to set up your environment and learn the project workflow. **Start here** if this is your first setup.
99
- New to the project? Pick an issue labeled ["good first issue"](https://github.com/kernelci/dashboard/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22).
1010
- There are a couple of extensions that may help you with linting and formatting your code. Consider installing [ESLint](https://eslint.org/) and [Prettier](https://prettier.io/) for your preferred code editor.
11-
- There are some of these to help with your Python development: [Black](https://black.readthedocs.io/en/stable/), [Flake8](https://flake8.pycqa.org/en/latest/), and [isort](https://pycqa.github.io/isort/).
11+
- For Python development, this repository uses [Ruff](https://docs.astral.sh/ruff/) for linting and formatting, and [pre-commit](https://pre-commit.com/) for Git hooks.
1212

1313
## Useful links
1414

@@ -97,10 +97,14 @@ You can find more details on the Conventional Commits specification site.
9797

9898
- Backend
9999
- See [backend/README.md](backend/README.md) for environment and commands
100-
- Run linting: `poetry run flake8`
100+
- Run linting: `poetry run ruff check .`
101+
- Run formatting check: `poetry run ruff format --check .`
102+
- Apply formatting: `poetry run ruff format .`
103+
- Auto-fix lint issues: `poetry run ruff check . --fix`
101104
- Run type checks (optional but recommended): `poetry run mypy`
102105
- Run tests: `poetry run pytest`
103-
- Apply formatting with `poetry run black .` before committing; the `backend/pre-commit` and `backend/pre-push` hooks can help automate this — see [backend/README.md](backend/README.md) for installation instructions
106+
- Install Git hooks once per clone: from the repository root run `poetry -C backend run pre-commit install --hook-type pre-commit --hook-type pre-push --install-hooks`
107+
- Run all hooks manually: `poetry -C backend run pre-commit run --all-files`
104108
- Frontend
105109
- See [dashboard/README.md](dashboard/README.md) for scripts and commands
106110
- Run linting: `pnpm lint`

backend/.flake8

Lines changed: 0 additions & 29 deletions
This file was deleted.

backend/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,27 @@ It is important to note that Django automatically creates migrations based on ch
100100
* [copy_db_data.sh](./scripts/copy_db_data.sh) will copy 7 days of data from you `DB_DEFAULT` to your `DASH_DB`. You can also modify the script for a custom interval, and you can check the [update_db](./kernelCI_app/management/commands/update_db.py) command for other arguments such as `--table` and `--origins`.
101101
* [generate-schema.sh](./generate-schema.sh) will automatically generate the OpenAPI schema for the endpoints. Please use it whenever the ins and outs of endpoints change.
102102
103+
## Linting, formatting, and hooks
104+
105+
The backend uses Ruff for linting and formatting.
106+
107+
- Check lint issues: `poetry run ruff check .`
108+
- Auto-fix lint issues: `poetry run ruff check . --fix`
109+
- Check formatting: `poetry run ruff format --check .`
110+
- Auto-fix formatting: `poetry run ruff format .`
111+
112+
This repository uses pre-commit for Git hooks (`pre-commit` and `pre-push`). Install hooks once per clone from the repository root:
113+
114+
```sh
115+
poetry -C backend run pre-commit install --hook-type pre-commit --hook-type pre-push --install-hooks
116+
```
117+
118+
To run all configured hooks manually:
119+
120+
```sh
121+
poetry -C backend run pre-commit run --all-files
122+
```
123+
103124
104125
## Running tests
105126

0 commit comments

Comments
 (0)