Skip to content

Commit

Permalink
Run pre-commit in CI (#29)
Browse files Browse the repository at this point in the history
* Add script to generate replay files and generated replay files

* Add CI to run cookiecutter tests

* Run pre-commit only for the generated project files

* Fix issues with running pre-commit in CI

* Add --all-files flag

* Add script to create cookiecutter project and to run pre-commit on the generated project

* Fix typo

* Remove commented out lines

* Change echo to print in green colour
  • Loading branch information
kumaranvpl authored Nov 8, 2024
1 parent 4718d26 commit f1fb07b
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 0 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Test

on:
push:
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
pre-commit-check:
strategy:
matrix:
python-version: ["3.12", "3.11", "3.10"]
app-type: ["fastapi+mesop", "mesop", "nats+fastapi+mesop"]
fail-fast: false
runs-on: ubuntu-latest
timeout-minutes: 15

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"

- name: Install Cookiecutter
run: pip install cookiecutter

- name: Test Cookiecutter
run: ./scripts/test_cookiecutter.sh ${{ matrix.app-type }} ${{ matrix.python-version }}

# https://github.com/marketplace/actions/alls-green#why
check: # This job does nothing and is only used for the branch protection
# from: https://github.com/re-actors/alls-green
# Important: For this to work properly, it is a must to have the job always
# run, otherwise GitHub will make it skipped when any of the dependencies
# fail. In some contexts, skipped is interpreted as success which may lead
# to undersired, unobvious and even dangerous (as in security breach
# "dangerous") side-effects.
if: always()

needs:
- pre-commit-check

runs-on: ubuntu-latest

steps:
- name: Decide whether the needed jobs succeeded or failed
uses: re-actors/alls-green@release/v1 # nosemgrep
with:
# allowed-failures: docs, linters
# allowed-skips: non-voting-flaky-job
jobs: ${{ toJSON(needs) }}
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
__pycache__
dist
.idea
venv*
.venv*
.env
.env*
*.lock
.vscode
.pypirc
.pytest_cache
.ruff_cache
.mypy_cache
.coverage*
.cache
htmlcov
token
.DS_Store

generated/
8 changes: 8 additions & 0 deletions cookiecutter_replay/fastapi+mesop_3.10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cookiecutter": {
"project_name": "My FastAgency App",
"project_slug": "my_fastagency_app",
"app_type": "fastapi+mesop",
"python_version": "3.10"
}
}
8 changes: 8 additions & 0 deletions cookiecutter_replay/fastapi+mesop_3.11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cookiecutter": {
"project_name": "My FastAgency App",
"project_slug": "my_fastagency_app",
"app_type": "fastapi+mesop",
"python_version": "3.11"
}
}
8 changes: 8 additions & 0 deletions cookiecutter_replay/fastapi+mesop_3.12.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cookiecutter": {
"project_name": "My FastAgency App",
"project_slug": "my_fastagency_app",
"app_type": "fastapi+mesop",
"python_version": "3.12"
}
}
8 changes: 8 additions & 0 deletions cookiecutter_replay/mesop_3.10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cookiecutter": {
"project_name": "My FastAgency App",
"project_slug": "my_fastagency_app",
"app_type": "mesop",
"python_version": "3.10"
}
}
8 changes: 8 additions & 0 deletions cookiecutter_replay/mesop_3.11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cookiecutter": {
"project_name": "My FastAgency App",
"project_slug": "my_fastagency_app",
"app_type": "mesop",
"python_version": "3.11"
}
}
8 changes: 8 additions & 0 deletions cookiecutter_replay/mesop_3.12.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cookiecutter": {
"project_name": "My FastAgency App",
"project_slug": "my_fastagency_app",
"app_type": "mesop",
"python_version": "3.12"
}
}
8 changes: 8 additions & 0 deletions cookiecutter_replay/nats+fastapi+mesop_3.10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cookiecutter": {
"project_name": "My FastAgency App",
"project_slug": "my_fastagency_app",
"app_type": "nats+fastapi+mesop",
"python_version": "3.10"
}
}
8 changes: 8 additions & 0 deletions cookiecutter_replay/nats+fastapi+mesop_3.11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cookiecutter": {
"project_name": "My FastAgency App",
"project_slug": "my_fastagency_app",
"app_type": "nats+fastapi+mesop",
"python_version": "3.11"
}
}
8 changes: 8 additions & 0 deletions cookiecutter_replay/nats+fastapi+mesop_3.12.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cookiecutter": {
"project_name": "My FastAgency App",
"project_slug": "my_fastagency_app",
"app_type": "nats+fastapi+mesop",
"python_version": "3.12"
}
}
33 changes: 33 additions & 0 deletions scripts/generate_cookiecutter_replay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json
from pathlib import Path

PROJECT_NAME = "My FastAgency App"
PROJECT_SLUG = "my_fastagency_app"

APP_TYPES = ["fastapi+mesop", "mesop", "nats+fastapi+mesop"]

PYTHON_VERSIONS = ["3.12", "3.11", "3.10"]


def generate_cookiecutter_replay():
for app_type in APP_TYPES:
for python_version in PYTHON_VERSIONS:
print(f"Generating cookiecutter replay for {app_type} with Python {python_version}")
cookiecutter_replay = {
"cookiecutter": {
"project_name": PROJECT_NAME,
"project_slug": PROJECT_SLUG,
"app_type": app_type,
"python_version": python_version,
}
}

# Write to cookiecutter replay file
replay_file = Path(__file__).parent.parent.resolve() / "cookiecutter_replay" / f"{app_type}_{python_version}.json"

with open(replay_file, "w") as f:
json.dump(cookiecutter_replay, f, indent=4)


if __name__ == "__main__":
generate_cookiecutter_replay()
3 changes: 3 additions & 0 deletions scripts/generate_cookiecutter_replay.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

python3 scripts/generate_cookiecutter_replay.py
22 changes: 22 additions & 0 deletions scripts/test_cookiecutter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

# This script is used to test the cookiecutter template
# It will create a new project using the cookiecutter template under the generated/ directory
# The script accepts two positional arguments: app-type and python-version
# The cookiecutter replay file is used to provide the default values for the cookiecutter template

# Accept two arguments: app-type and python-version
echo -e "\033[32mGenerating project using cookiecutter template with app-type: $1 and python-version: $2\033[0m"
cookiecutter -f --replay-file ./cookiecutter_replay/$1_$2.json --output-dir generated/ ./

# Install generated project's dependencies
echo -e "\033[32mInstalling dependencies for the generated project\033[0m"
cd generated/my_fastagency_app && pip install -e .[dev] && cd ../../

# Initialize git in the generated project(needed for pre-commit)
echo -e "\033[32mInitializing git in the generated project\033[0m"
cd generated/my_fastagency_app && git init && git add . && cd ../../

# Run pre-commit
echo -e "\033[32mRunning pre-commit\033[0m"
cd generated/my_fastagency_app && pre-commit run --show-diff-on-failure --color=always --all-files && cd ../../

0 comments on commit f1fb07b

Please sign in to comment.