From 55afefa813b05d0e31f37d2c182d4ff01c24f037 Mon Sep 17 00:00:00 2001 From: Oleksandr Bulanov Date: Fri, 11 Oct 2024 13:38:55 +0300 Subject: [PATCH 1/4] [DI-2709] Adds CI worflow for the generated project - Adds `.guthub/workflows/ci.yml` with couple jobs to: -- run pre-commit hooks on CI, inc. ruff linting & formatting; -- run `api` tests; - Update couple versions for pre-commit hooks --- README.md | 6 +++ .../.github/workflows/ci.yml | 53 +++++++++++++++++++ .../.pre-commit-config.yaml | 29 ++++------ {{ cookiecutter.project_slug }}/Makefile | 3 ++ .../api/pyproject.toml | 3 ++ 5 files changed, 76 insertions(+), 18 deletions(-) create mode 100644 {{ cookiecutter.project_slug }}/.github/workflows/ci.yml diff --git a/README.md b/README.md index 865436b..4e1f148 100644 --- a/README.md +++ b/README.md @@ -48,3 +48,9 @@ Run application: ```bash make compose-up ``` + +We are using docker for running the app, so you don't have to keep your python env locally. +But we also use pre-commit hooks, and the `safety` hook requires `poetry` to be in your `PATH`. + +> [!TIP] +> Ensure the poetry installed with the same python version as in `pyproject.toml` diff --git a/{{ cookiecutter.project_slug }}/.github/workflows/ci.yml b/{{ cookiecutter.project_slug }}/.github/workflows/ci.yml new file mode 100644 index 0000000..66064aa --- /dev/null +++ b/{{ cookiecutter.project_slug }}/.github/workflows/ci.yml @@ -0,0 +1,53 @@ +name: CI +on: [push] + +concurrency: + group: ${{ '{{' }} github.workflow {{ '}}' }}-${{ '{{' }} github.ref {{ '}}' }} + cancel-in-progress: true + + +jobs: + linter: + runs-on: ubuntu-latest + steps: + - name: Checkout Code Repository + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "{{ cookiecutter.python_version.lower() }}" + + # We need `poetry` to run safety pre-commit hook + - name: Install Poetry + uses: snok/install-poetry@v1 + + - name: Run pre-commit + uses: pre-commit/action@v3.0.1 + + # Tests & migrations checks + checks: + runs-on: ubuntu-latest + steps: + + - name: Checkout Code Repository + uses: actions/checkout@v4 + + - name: Create .env file + run: | + cp ./api/.env.example ./api/.env + + - name: Build the Stack + run: make compose-build + + - name: Check missing DB migrations + run: make api-check-migrations + + - name: Run DB Migrations + run: make api-migrate + + - name: Run `api` Tests + run: make api-test + + - name: Tear down the Stack + run: make compose-down diff --git a/{{ cookiecutter.project_slug }}/.pre-commit-config.yaml b/{{ cookiecutter.project_slug }}/.pre-commit-config.yaml index 79e7ea1..8726480 100644 --- a/{{ cookiecutter.project_slug }}/.pre-commit-config.yaml +++ b/{{ cookiecutter.project_slug }}/.pre-commit-config.yaml @@ -3,7 +3,7 @@ default_language_version: repos: # General - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v5.0.0 hooks: - id: check-added-large-files args: ['--maxkb=10000'] @@ -19,29 +19,22 @@ repos: - id: pretty-format-json args: ['--autofix', '--indent 2', ] - repo: https://github.com/codespell-project/codespell - rev: v2.2.4 + rev: v2.3.0 hooks: - id: codespell + args: [ "--toml=api/pyproject.toml" ] + additional_dependencies: + - tomli # API - - repo: https://github.com/charliermarsh/ruff-pre-commit - rev: v0.0.265 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.6.9 hooks: - id: ruff - args: ['--fix', '--exit-non-zero-on-fix'] + args: [ '--fix', '--exit-non-zero-on-fix' ] + - id: ruff-format - repo: https://github.com/Lucas-C/pre-commit-hooks-safety - rev: v1.3.1 + rev: v1.3.3 hooks: - id: python-safety-dependencies-check - entry: safety - args: [check] - files: ./api/requirements-build.txt -# TODO: Run this hook from docker container. -# - repo: local -# hooks: -# - id: migrations-check -# language: system -# name: Check for uncreated migrations. -# entry: sh -c "make api-check-migrations" -# files: models/*.*\.py$ -# stages: [commit] + files: pyproject.toml diff --git a/{{ cookiecutter.project_slug }}/Makefile b/{{ cookiecutter.project_slug }}/Makefile index 0ce29b4..95b1d49 100644 --- a/{{ cookiecutter.project_slug }}/Makefile +++ b/{{ cookiecutter.project_slug }}/Makefile @@ -26,3 +26,6 @@ compose-down: compose-build: cd docker/ && docker compose build + +api-migrate: + cd docker/ && docker compose run --rm api make migrate diff --git a/{{ cookiecutter.project_slug }}/api/pyproject.toml b/{{ cookiecutter.project_slug }}/api/pyproject.toml index 682e527..27417a7 100644 --- a/{{ cookiecutter.project_slug }}/api/pyproject.toml +++ b/{{ cookiecutter.project_slug }}/api/pyproject.toml @@ -125,3 +125,6 @@ exclude_lines =[ ] show_missing = true +[tool.codespell] +skip = "*.lock" +ignore-words = "codespell-ignore-words.txt" From ecf72d8e150288a1866228b75ce90fc7d39a41fc Mon Sep 17 00:00:00 2001 From: Oleksandr Bulanov Date: Mon, 14 Oct 2024 12:35:42 +0300 Subject: [PATCH 2/4] [DI-2709] Update docs --- {{ cookiecutter.project_slug }}/Makefile | 6 +++--- {{ cookiecutter.project_slug }}/README.md | 4 ++++ .../docs/pre_commit_hook.md | 13 ++++++++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/{{ cookiecutter.project_slug }}/Makefile b/{{ cookiecutter.project_slug }}/Makefile index 95b1d49..14706b8 100644 --- a/{{ cookiecutter.project_slug }}/Makefile +++ b/{{ cookiecutter.project_slug }}/Makefile @@ -9,6 +9,9 @@ api-test: api-check-migrations: cd docker/ && docker compose run --rm api make check-migrations +api-migrate: + cd docker/ && docker compose run --rm api make migrate + api-check: cd docker/ && docker compose run --rm api make check @@ -26,6 +29,3 @@ compose-down: compose-build: cd docker/ && docker compose build - -api-migrate: - cd docker/ && docker compose run --rm api make migrate diff --git a/{{ cookiecutter.project_slug }}/README.md b/{{ cookiecutter.project_slug }}/README.md index 735c42d..1a5495d 100644 --- a/{{ cookiecutter.project_slug }}/README.md +++ b/{{ cookiecutter.project_slug }}/README.md @@ -56,3 +56,7 @@ minimal setup. If you need python environment, to run some commands, just use `make api-bash`, and you will be in the container with all dependencies installed. +> [!NOTE] +> To ensure your code follow the standards etc. we strongly recommend to use `pre-commit` locally, that means you need +> to have it installed manually, see [pre-commit hook docs](docs/pre_commit_hook.md) for more details. + diff --git a/{{ cookiecutter.project_slug }}/docs/pre_commit_hook.md b/{{ cookiecutter.project_slug }}/docs/pre_commit_hook.md index d831c04..4ddac6f 100644 --- a/{{ cookiecutter.project_slug }}/docs/pre_commit_hook.md +++ b/{{ cookiecutter.project_slug }}/docs/pre_commit_hook.md @@ -2,11 +2,18 @@ To run additional checks before making commit we use [Pre-commit](https://pre-commit.com/) hooks. -`pre-commit` package is installed with backend requirements, so there is no need of [manual installation](https://pre-commit.com/#install). -But you need to generate the actual git pre-commit hook. It should be done only once: + +Since the `api` is running inside docker container we don't have a pre-commit as a dependency there, so you need to +install `pre-commit` package [manually](https://pre-commit.com/#install). + +> [!NOTE] +> The `safety` hook also requires `poetry` to be in your `PATH`. +> Ensure the poetry installed with the same python version as in `pyproject.toml` + +You need to generate the actual git pre-commit hook. It should be done only once: ```bash -pre-commit install +pre-commit install --install-hooks ``` To run pre-commit hooks manually: From 4a1c293606349de48ab849ac45e285863bb0bd5f Mon Sep 17 00:00:00 2001 From: Alexandr Bulanov Date: Tue, 15 Oct 2024 11:49:06 +0300 Subject: [PATCH 3/4] [DI-2709] Update docs Co-authored-by: Denys Chura <38692483+denys-chura@users.noreply.github.com> --- {{ cookiecutter.project_slug }}/docs/pre_commit_hook.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/{{ cookiecutter.project_slug }}/docs/pre_commit_hook.md b/{{ cookiecutter.project_slug }}/docs/pre_commit_hook.md index 4ddac6f..734bc5f 100644 --- a/{{ cookiecutter.project_slug }}/docs/pre_commit_hook.md +++ b/{{ cookiecutter.project_slug }}/docs/pre_commit_hook.md @@ -7,8 +7,8 @@ Since the `api` is running inside docker container we don't have a pre-commit as install `pre-commit` package [manually](https://pre-commit.com/#install). > [!NOTE] -> The `safety` hook also requires `poetry` to be in your `PATH`. -> Ensure the poetry installed with the same python version as in `pyproject.toml` +> The `safety` hook also [requires](https://github.com/Lucas-C/pre-commit-hooks-safety?tab=readme-ov-file#supported-files) `poetry` to be in your `PATH`. +> Ensure the poetry [installed](https://python-poetry.org/docs/#installation) with the same python version as in `pyproject.toml` You need to generate the actual git pre-commit hook. It should be done only once: From e8764ddf1cd2dda1cada6dbe4217fe8340d96119 Mon Sep 17 00:00:00 2001 From: Alexandr Bulanov Date: Tue, 15 Oct 2024 11:49:36 +0300 Subject: [PATCH 4/4] [DI-2709] Update docs Co-authored-by: Denys Chura <38692483+denys-chura@users.noreply.github.com> --- {{ cookiecutter.project_slug }}/docs/pre_commit_hook.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/{{ cookiecutter.project_slug }}/docs/pre_commit_hook.md b/{{ cookiecutter.project_slug }}/docs/pre_commit_hook.md index 734bc5f..aa4d760 100644 --- a/{{ cookiecutter.project_slug }}/docs/pre_commit_hook.md +++ b/{{ cookiecutter.project_slug }}/docs/pre_commit_hook.md @@ -3,8 +3,8 @@ To run additional checks before making commit we use [Pre-commit](https://pre-commit.com/) hooks. -Since the `api` is running inside docker container we don't have a pre-commit as a dependency there, so you need to -install `pre-commit` package [manually](https://pre-commit.com/#install). +Since the `api` is running inside docker container and we don't have a pre-commit as a dependency there, you need to +install `pre-commit` package [locally](https://pre-commit.com/#install). > [!NOTE] > The `safety` hook also [requires](https://github.com/Lucas-C/pre-commit-hooks-safety?tab=readme-ov-file#supported-files) `poetry` to be in your `PATH`.