Skip to content

Add pre-commit #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
# Check for updates to GitHub Actions every week
interval: "weekly"
40 changes: 8 additions & 32 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: EuroPython Discord Bot CI
run-name: "🛠️ CI: ${{ github.event.head_commit.message }} (${{ github.sha }})"
permissions:
contents: read

on:
push:
Expand All @@ -9,7 +11,7 @@ on:

jobs:
lint:
name: 🕵 Lint Python code
name: Lint and Test
runs-on: ubuntu-latest

steps:
Expand All @@ -20,39 +22,13 @@ jobs:
- name: Install uv
uses: astral-sh/setup-uv@v6
with:
version: "0.6.14"
version: "0.7.3"

- name: Set up Python
- name: Install Python
run: uv python install

- name: Install dev dependencies
- name: Install dependencies
run: uv sync --dev

- name: Check code format
run: uv run --dev ruff format --check

- name: Check code style
run: uv run --dev ruff check

test:
name: 🛠️ Test Python code
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install uv
uses: astral-sh/setup-uv@v6
with:
version: "0.6.14"

- name: Set up Python
run: uv python install

- name: Install dev dependencies
run: uv sync --dev

- name: Run pytest
run: uv run --dev pytest .
- name: Run code checks
run: uv run --dev pre-commit run --all-files
4 changes: 3 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
name: Deploy to server
permissions:
contents: read

on:
workflow_dispatch:
Expand All @@ -20,7 +22,7 @@ jobs:
run: |
sudo apt update
sudo apt install -y ansible

- name: Set up SSH agent
uses: webfactory/ssh-agent@v0.9.1
with:
Expand Down
29 changes: 29 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
- id: check-added-large-files

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.11.9
hooks:
- id: ruff
# fix imports:
# - I001: Bad import order or format
# - F401: Unused import
args: [ '--fix', '--fixable', 'I001,F401' ]
- id: ruff-format

- repo: local
hooks:
- id: pytest
# run pytest with low verbosity (no header, no tracebacks)
name: pytest
language: system
entry: uv run --dev pytest --no-header --tb=no
pass_filenames: false
always_run: true
49 changes: 23 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ Used cache and log files (will be created if necessary):
## Setup
### Quickstart using `pip`

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

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

### Development setup using `uv`
### Full Development Setup

Install `uv` as documented [here](https://docs.astral.sh/uv/getting-started/installation/), then run `uv sync --dev` to create/update a
virtual environment with all dependencies according to [`uv.lock`](./uv.lock).

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

To run the bot, use the following:

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

# run the bot with a given config file
uv run run-bot --config your-config-file.toml
run-bot --config your-config-file.toml
```

#### Useful `uv` commands
#### Working with `uv`

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

```shell
# generate .venv/ from uv.lock
uv sync
uv sync --dev # include dev dependencies

# activate uv-generated venv
# activate uv-generated virtual environment ("venv")
. .venv/bin/activate # Windows: '.venv/Scripts/activate'

# execute command inside uv-generated venv
uv run [command]

# reset all packages to versions pinned in uv.lock
# create/synchronize venv based on uv.lock file
uv sync
uv sync --dev # include dev dependencies

# execute command inside uv-generated venv (can be skipped if venv is activated)
uv run [command]

# add package
uv add [package]
uv add --dev [package] # install as dev dependency
uv add --dev [package] # as dev dependency

# upgrade packages
uv lock --upgrade
Expand All @@ -120,11 +115,12 @@ uv remove [package]

### Development tools

* Format code: `uv run --dev ruff format`
* Check code format: `uv run --dev ruff format --check`
* Sort imports: `uv run --dev ruff check --select I001 --fix`
* Check code style: `uv run --dev ruff check .`
* Run tests: `uv run --dev pytest .`
* Run everything: `pre-commit run --all-files`
* Format code: `ruff format`
* Check code format: `ruff format --check`
* Fix imports: `ruff check --select I001,F401 --fix`
* Check code style: `ruff check .`
* Run tests: `pytest`

### Deployment

Expand All @@ -137,3 +133,4 @@ Related files:
* [ansible/deploy-playbook.yml](./ansible/deploy-playbook.yml): The Ansible Playbook
* [Dockerfile](./Dockerfile): The Docker container recipe
* [compose.yaml](./compose.yaml): The Docker Compose recipe
* [prod-config.toml](./prod-config.toml): The Prod bot configuration
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies = [

[dependency-groups]
dev = [
"pre-commit>=4.2.0",
"pytest>=8.3.5",
"pytest-aiohttp>=1.1.0",
"pytest-asyncio>=0.26.0",
Expand All @@ -31,6 +32,11 @@ run-bot = "europython_discord.bot:main"

[tool.pytest.ini_options]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"
filterwarnings = [
"error",
"ignore:'audioop' is deprecated:DeprecationWarning",
]

[tool.ruff]
line-length = 100
Expand Down
2 changes: 1 addition & 1 deletion tests/registration/mock_pretix_items.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
]
}
]
}
}
2 changes: 1 addition & 1 deletion tests/registration/mock_pretix_orders.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
]
}
]
}
}
Loading