Skip to content

Commit a964645

Browse files
committed
Closes #16248: Replace git commit hook script with pre-commit
1 parent 7ac6dff commit a964645

File tree

3 files changed

+71
-17
lines changed

3 files changed

+71
-17
lines changed

.pre-commit-config.yaml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: v0.6.9
4+
hooks:
5+
- id: ruff
6+
name: "Ruff linter"
7+
args: [ netbox/ ]
8+
- repo: local
9+
hooks:
10+
- id: django-check
11+
name: "Django system check"
12+
description: "Run Django's internal check for common problems"
13+
entry: python netbox/manage.py check
14+
language: system
15+
pass_filenames: false
16+
types: [python]
17+
- id: django-makemigrations
18+
name: "Django migrations check"
19+
description: "Check for any missing Django migrations"
20+
entry: python netbox/manage.py makemigrations --check
21+
language: system
22+
pass_filenames: false
23+
types: [python]
24+
- id: mkdocs-build
25+
name: "Build documentation"
26+
description: "Build the documentation with mkdocs"
27+
files: 'docs/'
28+
entry: mkdocs build
29+
language: system
30+
pass_filenames: false
31+
- id: yarn-validate
32+
name: "Yarn validate"
33+
description: "Check UI ESLint, TypeScript, and Prettier compliance"
34+
files: 'netbox/project-static/'
35+
entry: yarn --cwd netbox/project-static validate
36+
language: system
37+
pass_filenames: false
38+
- id: verify-bundles
39+
name: "Verify static asset bundles"
40+
description: "Ensure that any modified static assets have been compiled"
41+
files: 'netbox/project-static/'
42+
entry: scripts/verify-bundles.sh
43+
language: system
44+
pass_filenames: false

docs/development/getting-started.md

+22-17
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,7 @@ $issue-$description
6262

6363
The description should be just two or three words to imply the focus of the work being performed. For example, bug #1234 to fix a TypeError exception when creating a device might be named `1234-device-typerror`. This ensures that branches are always follow some logical ordering (e.g. when running `git branch -a`) and helps other developers quickly identify the purpose of each.
6464

65-
### 3. Enable Pre-Commit Hooks
66-
67-
NetBox ships with a [git pre-commit hook](https://githooks.com/) script that automatically checks for style compliance and missing database migrations prior to committing changes. This helps avoid erroneous commits that result in CI test failures. You are encouraged to enable it by creating a link to `scripts/git-hooks/pre-commit`:
68-
69-
```no-highlight
70-
cd .git/hooks/
71-
ln -s ../../scripts/git-hooks/pre-commit
72-
```
73-
For the pre-commit hooks to work, you will also need to install the [ruff](https://docs.astral.sh/ruff/) linter:
74-
75-
```no-highlight
76-
python -m pip install ruff
77-
```
78-
...and set up the yarn packages as shown in the [Web UI Development Guide](web-ui.md)
79-
80-
### 4. Create a Python Virtual Environment
65+
### 3. Create a Python Virtual Environment
8166

8267
A [virtual environment](https://docs.python.org/3/tutorial/venv.html) (or "venv" for short) is like a container for a set of Python packages. These allow you to build environments suited to specific projects without interfering with system packages or other projects. When installed per the documentation, NetBox uses a virtual environment in production.
8368

@@ -101,14 +86,34 @@ source ~/.venv/netbox/bin/activate
10186

10287
Notice that the console prompt changes to indicate the active environment. This updates the necessary system environment variables to ensure that any Python scripts are run within the virtual environment.
10388

104-
### 5. Install Required Packages
89+
### 4. Install Required Packages
10590

10691
With the virtual environment activated, install the project's required Python packages using the `pip` module. Required packages are defined in `requirements.txt`. Each line in this file specifies the name and specific version of a required package.
10792

10893
```no-highlight
10994
python -m pip install -r requirements.txt
11095
```
11196

97+
### 5. Install Pre-Commit
98+
99+
NetBox uses [`pre-commit`](https://pre-commit.com/) to automatically validate code when commiting new changes. This includes the following operations:
100+
101+
* Run the `ruff` Python linter
102+
* Run Django's internal system check
103+
* Check for missing database migrations
104+
* Validate any changes to the documentation with `mkdocs`
105+
* Validate Typescript & Sass styling with `yarn`
106+
* Ensure that any modified static front end assets have been recompiled
107+
108+
Enable `pre-commit` with the following commands _prior_ to commiting any changes:
109+
110+
```no-highlight
111+
python -m pip install ruff pre-commit
112+
pre-commit install
113+
```
114+
115+
You may also need to set up the yarn packages as shown in the [Web UI Development Guide](web-ui.md).
116+
112117
### 6. Configure NetBox
113118

114119
Within the `netbox/netbox/` directory, copy `configuration_example.py` to `configuration.py` and update the following parameters:

scripts/git-hooks/pre-commit

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ RED='\033[0;31m'
1414
YELLOW='\033[0;33m'
1515
NOCOLOR='\033[0m'
1616

17+
printf "${YELLOW}This script is obsolete and will be removed in a future release.\n"
18+
printf "Please use pre-commit instead:\n"
19+
printf " pip install pre-commit\n"
20+
printf " pre-commit install${NOCOLOR}\n"
21+
1722
if [ -d ./venv/ ]; then
1823
VENV="$PWD/venv"
1924
if [ -e $VENV/bin/python ]; then

0 commit comments

Comments
 (0)