Skip to content

Commit

Permalink
Migrate pre-commit hooks to lefthook (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
disrupted authored Jan 15, 2025
1 parent 0e9aa8f commit 59765dc
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 185 deletions.
15 changes: 10 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,24 @@ jobs:
version: ${{ env.UV_VERSION }}

- name: Generate schema (kpops schema)
run: uv run --frozen pre-commit run gen-schema --all-files --show-diff-on-failure
run: uv run --frozen
lefthook run pre-commit --commands kpops-schema --force

- name: Generate CLI Usage docs (typer-cli)
run: uv run --frozen pre-commit run gen-docs-cli --all-files --show-diff-on-failure
run: uv run --frozen
lefthook run pre-commit --commands kpops-docs-cli --force

- name: Generate Environment variable docs
run: uv run --frozen pre-commit run gen-docs-env-vars --all-files --show-diff-on-failure
run: uv run --frozen
lefthook run pre-commit --commands kpops-docs-env-vars --force

- name: Generate pipeline definitions
run: uv run --frozen pre-commit run gen-docs-components --all-files --show-diff-on-failure
run: uv run --frozen
lefthook run pre-commit --commands kpops-docs-components --force

- name: Test docs build (mkdocs)
run: uv run --frozen --group=docs mkdocs build -f docs/mkdocs.yml
run: uv run --frozen --group=docs
mkdocs build -f docs/mkdocs.yml

# TODO: extract into ci-templates
publish-snapshot-version:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
.vscode
site/
scratch*
lefthook-local.yaml
83 changes: 0 additions & 83 deletions .pre-commit-config.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion docs/docs/developer/auto-generation.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Auto generation

Auto generation happens mostly with [`pre-commit`](https://pre-commit.com/) hooks. You can find the pre-commit configuration [here](https://github.com/bakdata/kpops/blob/main/.pre-commit-config.yaml). These pre-commit hooks call different [Python scripts](https://github.com/bakdata/kpops/tree/main/hooks) to auto generate code for the documentation.
Auto generation happens mostly with Git hooks. You can find the [`lefthook`](https://evilmartians.github.io/lefthook/) configuration [here](https://github.com/bakdata/kpops/blob/main/lefthook.yaml). These pre-commit hooks call different [Python scripts](https://github.com/bakdata/kpops/tree/main/hooks) to auto generate code for the documentation.

## Generation scripts and their respective files

Expand Down
20 changes: 1 addition & 19 deletions docs/docs/developer/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,4 @@ This will fetch the resources under the `examples` folder.

## Style

We advise that you stick to our `pre-commit` hooks for code linting, formatting, and auto-generation of documentation. After you install them using `pre-commit install` they're triggered automatically during `git commit`. Additionally, you can manually invoke them with `pre-commit run -a`. In order for `dprint` to work, you have to manually [install](#markdown) it locally. It will work in the CI, so it is also possible to manually carry out formatting changes flagged by `dprint` in the CI and skip installing it locally.

### Python

To ensure a consistent Python code style, we use [Ruff](https://docs.astral.sh/ruff/) for both linting and formatting. The official docs contain a guide on [editor integration](https://docs.astral.sh/ruff/integrations/).

Our configuration can be found in [KPOps](https://github.com/bakdata/kpops)' top-level `pyproject.toml`.

### Markdown

To ensure a consistent markdown style, we use [dprint](https://dprint.dev)'s [Markdown code formatter](https://dprint.dev/plugins/markdown/). Our configuration can be found [here](https://github.com/bakdata/kpops/blob/main/dprint.json).

### CSS

To ensure a consistent CSS style, we use the [malva](https://github.com/g-plane/malva) [dprint](https://dprint.dev)'s plugin. Our configuration can be found [here](https://github.com/bakdata/kpops/blob/main/dprint.json).

### TOML

To ensure a consistent TOML style, we use [dprint](https://dprint.dev)'s [TOML code formatter](https://dprint.dev/plugins/toml/). Our configuration can be found [here](https://github.com/bakdata/kpops/blob/main/dprint.json).
We advise that you stick to our Git hooks for code linting, formatting, and auto-generation of documentation. After you install them using `lefthook install` they're triggered automatically during `git` operations, such as commit or checkout. Additionally, you can manually invoke them with `lefthook run pre-commit --all-files`. Please also install the [`dprint`](https://dprint.dev/) formatter.
6 changes: 1 addition & 5 deletions dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
"markdown": {},
"toml": {},
"malva": {},
"includes": [
"**/*.{md}",
"**/*.{toml}",
"**/*.{css}"
],
"includes": ["**/*.{md,toml,css}"],
"excludes": [
".pytest_cache/**",
".mypy_cache/**",
Expand Down
2 changes: 1 addition & 1 deletion hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""KPOps pre-commit hooks."""
"""KPOps Git hooks."""

from pathlib import Path

Expand Down
4 changes: 2 additions & 2 deletions hooks/gen_docs/gen_docs_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
PATH_DOCS_KPOPS_STRUCTURE,
}
# All args provided to the script
# Pre-commit passes changed files as args
# pre-commit/lefthook pass changed files as args
SCRIPT_ARGUMENTS = set(sys.argv)

log = logging.getLogger("DocumentationGenerator")
Expand Down Expand Up @@ -247,7 +247,7 @@ def get_sections(component_name: str, *, exist_changes: bool) -> KpopsComponent:
# Delete the old dependency files
for dangerous_file in DANGEROUS_FILES_TO_CHANGE:
dangerous_file.unlink(missing_ok=True)
# Don't display warning if `-a` flag suspected in `pre-commit run`
# Don't display warning if `--all-files` flag suspected in `pre-commit run` or `lefthook run`
if ".gitignore" not in SCRIPT_ARGUMENTS:
log.warning(
redify(
Expand Down
53 changes: 53 additions & 0 deletions lefthook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/evilmartians/lefthook/master/schema.json

pre-commit:
parallel: true
commands:
uv-lock:
glob: "{pyproject.toml,uv.lock}"
run: uv lock --check
ruff-check:
glob: "*.py"
run: uv run ruff check --force-exclude --config pyproject.toml --fix --show-fixes {staged_files}
stage_fixed: true
ruff-format:
glob: "*.py"
run: uv run ruff format --force-exclude --config pyproject.toml {staged_files}
stage_fixed: true
pyright:
glob: "*.py"
run: uv run pyright
dprint:
glob: "*.{md,toml,css}"
run: dprint fmt
kpops-schema:
glob: "{uv.lock,kpops/**/*.py,hooks/gen_schema.py}"
run: python -m hooks.gen_schema && git add -u docs/docs/schema
kpops-docs-cli:
glob: "{uv.lock,kpops/cli/main.py,hooks/gen_docs/gen_docs_cli_usage.py}"
run: python -m hooks.gen_docs.gen_docs_cli_usage
kpops-docs-env-vars:
glob: "{uv.lock,kpops/**/*.py,hooks/gen_docs/gen_docs_env_vars.py}"
run: python -m hooks.gen_docs.gen_docs_env_vars
kpops-docs-components:
glob: "{\
.gitignore,\
uv.lock,\
hooks/gen_docs/gen_docs_components.py,\
docs/docs/resources/pipeline-defaults/headers/*.yaml,\
docs/docs/resources/pipeline-components/headers/*.yaml,\
docs/docs/resources/pipeline-components/sections/*.yaml,\
docs/docs/resources/pipeline-components/dependencies/*.yaml,\
kpops/components/*.py\
}"
# .gitignore -- indicates whether the script is ran with `--all-files`
# hooks/gen_docs/gen_docs_components.py -- hook's behavior is possibly changed
# docs/.../.yaml -- examples changed or dependencies edited
# kpops/components/... -- KPOps components possibly changed
run: python -m hooks.gen_docs.gen_docs_components {staged_files}

post-checkout:
commands:
dependencies:
glob: "uv.lock"
run: uv sync
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ kpops = "kpops.cli.main:app"

[dependency-groups]
dev = [
"lefthook>=1.10.4",
"polyfactory>=2.13.0",
"pre-commit>=2.19.0",
"pyright>=1.1.352",
"pytablewriter[from]>=1.0.0",
"pytest>=8.3.2",
Expand Down Expand Up @@ -79,6 +79,8 @@ asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"

[tool.pyright]
include = ["kpops", "tests", "hooks"]

reportImportCycles = true

reportUnknownParameterType = "warning"
Expand Down
Loading

0 comments on commit 59765dc

Please sign in to comment.