Skip to content

Commit d6410f0

Browse files
authored
Add pre-commit (#219)
* Upgrade dependencies Command: uv lock --upgrade * Add pre-commit as dev dependency Command: uv add --dev pre-commit * Fix: Add missing trailing newlines, remove trailing whitespace * Handle pytest deprecation warnings, treat all other warnings as errors * Add pre-commit configuration * Update documentation * Add check-toml pre-commit hook * Fix: Run pytest via uv * Update uv version in CI * Remove superfluous comment * Use pre-commit in CI * Rename CI steps * Add formatting issue * Restrict GitHub Actions repo permissions * Revert "Add formatting issue" This reverts commit 08c1577. * Remove uv-lock pre-commit hook Dependabot is responsible for this task
1 parent 5c118f0 commit d6410f0

File tree

9 files changed

+216
-86
lines changed

9 files changed

+216
-86
lines changed

.github/dependabot.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ updates:
77
- package-ecosystem: "github-actions"
88
directory: "/"
99
schedule:
10-
# Check for updates to GitHub Actions every week
1110
interval: "weekly"

.github/workflows/ci.yml

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
name: EuroPython Discord Bot CI
22
run-name: "🛠️ CI: ${{ github.event.head_commit.message }} (${{ github.sha }})"
3+
permissions:
4+
contents: read
35

46
on:
57
push:
@@ -9,7 +11,7 @@ on:
911

1012
jobs:
1113
lint:
12-
name: 🕵 Lint Python code
14+
name: Lint and Test
1315
runs-on: ubuntu-latest
1416

1517
steps:
@@ -20,39 +22,13 @@ jobs:
2022
- name: Install uv
2123
uses: astral-sh/setup-uv@v6
2224
with:
23-
version: "0.6.14"
25+
version: "0.7.3"
2426

25-
- name: Set up Python
27+
- name: Install Python
2628
run: uv python install
2729

28-
- name: Install dev dependencies
30+
- name: Install dependencies
2931
run: uv sync --dev
3032

31-
- name: Check code format
32-
run: uv run --dev ruff format --check
33-
34-
- name: Check code style
35-
run: uv run --dev ruff check
36-
37-
test:
38-
name: 🛠️ Test Python code
39-
runs-on: ubuntu-latest
40-
41-
steps:
42-
- uses: actions/checkout@v4
43-
with:
44-
fetch-depth: 0
45-
46-
- name: Install uv
47-
uses: astral-sh/setup-uv@v6
48-
with:
49-
version: "0.6.14"
50-
51-
- name: Set up Python
52-
run: uv python install
53-
54-
- name: Install dev dependencies
55-
run: uv sync --dev
56-
57-
- name: Run pytest
58-
run: uv run --dev pytest .
33+
- name: Run code checks
34+
run: uv run --dev pre-commit run --all-files

.github/workflows/deploy.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
name: Deploy to server
2+
permissions:
3+
contents: read
24

35
on:
46
workflow_dispatch:
@@ -20,7 +22,7 @@ jobs:
2022
run: |
2123
sudo apt update
2224
sudo apt install -y ansible
23-
25+
2426
- name: Set up SSH agent
2527
uses: webfactory/ssh-agent@v0.9.1
2628
with:

.pre-commit-config.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v5.0.0
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer
7+
- id: check-yaml
8+
- id: check-toml
9+
- id: check-added-large-files
10+
11+
- repo: https://github.com/astral-sh/ruff-pre-commit
12+
rev: v0.11.9
13+
hooks:
14+
- id: ruff
15+
# fix imports:
16+
# - I001: Bad import order or format
17+
# - F401: Unused import
18+
args: [ '--fix', '--fixable', 'I001,F401' ]
19+
- id: ruff-format
20+
21+
- repo: local
22+
hooks:
23+
- id: pytest
24+
# run pytest with low verbosity (no header, no tracebacks)
25+
name: pytest
26+
language: system
27+
entry: uv run --dev pytest --no-header --tb=no
28+
pass_filenames: false
29+
always_run: true

README.md

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ Used cache and log files (will be created if necessary):
4949
## Setup
5050
### Quickstart using `pip`
5151

52-
This project uses [uv](https://github.com/astral-sh/uv) for managing dependencies.
53-
If you just want to try the bot and skip the development setup,
54-
you can use `pip` instead of `uv` (requires Python >= 3.11):
52+
If you just want to try the bot and skip the development setup, you can use `pip` (requires Python >= 3.11):
5553

5654
```shell
5755
# create and activate virtual environment (optional, but recommended)
@@ -69,13 +67,13 @@ export PRETIX_TOKEN=... # Windows: $env:PRETIX_TOKEN = '...'
6967
run-bot --config your-config-file.toml
7068
```
7169

72-
### Development setup using `uv`
70+
### Full Development Setup
7371

74-
Install `uv` as documented [here](https://docs.astral.sh/uv/getting-started/installation/), then run `uv sync --dev` to create/update a
75-
virtual environment with all dependencies according to [`uv.lock`](./uv.lock).
76-
77-
If required, `uv` will download the required Python version, as specified in
78-
[`.python-version`](./.python-version).
72+
* Install `uv` as documented [here](https://docs.astral.sh/uv/getting-started/installation/).
73+
* Run `uv sync --dev` to create/update a virtual environment with all dependencies according to [`uv.lock`](./uv.lock).
74+
* Run `. .venv/bin/activate` (Windows: `.venv/Scripts/activate`) to activate the virtual environment
75+
* Run `pre-commit install` to install the [pre-commit](https://pre-commit.com/) hooks.
76+
* Run `pre-commit run --all-files` to verify your setup. All checks should pass.
7977

8078
To run the bot, use the following:
8179

@@ -85,31 +83,28 @@ export DISCORD_BOT_TOKEN=... # Windows: $env:DISCORD_BOT_TOKEN = '...'
8583
export PRETIX_TOKEN=... # Windows: $env:PRETIX_TOKEN = '...'
8684

8785
# run the bot with a given config file
88-
uv run run-bot --config your-config-file.toml
86+
run-bot --config your-config-file.toml
8987
```
9088

91-
#### Useful `uv` commands
89+
#### Working with `uv`
9290

91+
This is a list of useful commands when working with `uv`.
9392
Please refer to the [uv documentation](https://docs.astral.sh/uv) or `uv help` for details.
9493

9594
```shell
96-
# generate .venv/ from uv.lock
97-
uv sync
98-
uv sync --dev # include dev dependencies
99-
100-
# activate uv-generated venv
95+
# activate uv-generated virtual environment ("venv")
10196
. .venv/bin/activate # Windows: '.venv/Scripts/activate'
10297

103-
# execute command inside uv-generated venv
104-
uv run [command]
105-
106-
# reset all packages to versions pinned in uv.lock
98+
# create/synchronize venv based on uv.lock file
10799
uv sync
108100
uv sync --dev # include dev dependencies
109101

102+
# execute command inside uv-generated venv (can be skipped if venv is activated)
103+
uv run [command]
104+
110105
# add package
111106
uv add [package]
112-
uv add --dev [package] # install as dev dependency
107+
uv add --dev [package] # as dev dependency
113108

114109
# upgrade packages
115110
uv lock --upgrade
@@ -120,11 +115,12 @@ uv remove [package]
120115

121116
### Development tools
122117

123-
* Format code: `uv run --dev ruff format`
124-
* Check code format: `uv run --dev ruff format --check`
125-
* Sort imports: `uv run --dev ruff check --select I001 --fix`
126-
* Check code style: `uv run --dev ruff check .`
127-
* Run tests: `uv run --dev pytest .`
118+
* Run everything: `pre-commit run --all-files`
119+
* Format code: `ruff format`
120+
* Check code format: `ruff format --check`
121+
* Fix imports: `ruff check --select I001,F401 --fix`
122+
* Check code style: `ruff check .`
123+
* Run tests: `pytest`
128124

129125
### Deployment
130126

@@ -137,3 +133,4 @@ Related files:
137133
* [ansible/deploy-playbook.yml](./ansible/deploy-playbook.yml): The Ansible Playbook
138134
* [Dockerfile](./Dockerfile): The Docker container recipe
139135
* [compose.yaml](./compose.yaml): The Docker Compose recipe
136+
* [prod-config.toml](./prod-config.toml): The Prod bot configuration

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies = [
2020

2121
[dependency-groups]
2222
dev = [
23+
"pre-commit>=4.2.0",
2324
"pytest>=8.3.5",
2425
"pytest-aiohttp>=1.1.0",
2526
"pytest-asyncio>=0.26.0",
@@ -31,6 +32,11 @@ run-bot = "europython_discord.bot:main"
3132

3233
[tool.pytest.ini_options]
3334
asyncio_mode = "auto"
35+
asyncio_default_fixture_loop_scope = "function"
36+
filterwarnings = [
37+
"error",
38+
"ignore:'audioop' is deprecated:DeprecationWarning",
39+
]
3440

3541
[tool.ruff]
3642
line-length = 100

tests/registration/mock_pretix_items.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
]
1818
}
1919
]
20-
}
20+
}

tests/registration/mock_pretix_orders.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
]
3232
}
3333
]
34-
}
34+
}

0 commit comments

Comments
 (0)