Skip to content

fix(ci): resolve mypy errors and increase test coverage #15

fix(ci): resolve mypy errors and increase test coverage

fix(ci): resolve mypy errors and increase test coverage #15

Workflow file for this run

name: Quality Assurance
on:
pull_request:
branches: [main, develop]
push:
branches: [main, develop]
jobs:
# Stage 1: Linting - If this fails, tests won't run
lint:
name: Code Quality (Linting & Formatting)
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.7.1
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
run: poetry install --no-interaction --no-root
- name: Run Black (formatting check)
run: |
poetry run black --check app tests
if [ $? -ne 0 ]; then
echo "❌ FAILED: Code is not formatted with Black"
echo "Run: poetry run black app tests"
exit 1
fi
- name: Run Ruff (linting)
run: |
poetry run ruff check app tests
if [ $? -ne 0 ]; then
echo "❌ FAILED: Ruff found linting errors"
echo "Run: poetry run ruff check app tests --fix"
exit 1
fi
- name: Run MyPy (type checking)
run: |
poetry run mypy app
if [ $? -ne 0 ]; then
echo "❌ FAILED: MyPy found type errors"
exit 1
fi
- name: ✅ Linting Passed
run: echo "All linting checks passed!"
# Stage 2: Tests - Only runs if linting passes
test:
name: Unit & Integration Tests
runs-on: ubuntu-latest
needs: lint # This ensures tests only run after linting passes
services:
postgres:
image: pgvector/pgvector:pg15
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: app_test
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.7.1
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
run: poetry install --no-interaction
- name: Run tests with coverage
env:
POSTGRES_SERVER: localhost
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: app_test
ENVIRONMENT: testing
JWT_SECRET_KEY: testing_secret_key_change_me_in_prod_12345
API_KEY_SECRET: testing_api_key_secret_change_me_12345
run: |
poetry run pytest -v --cov=app --cov-report=xml --cov-report=term-missing
if [ $? -ne 0 ]; then
echo "❌ FAILED: Tests did not pass"
exit 1
fi
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
fail_ci_if_error: false
- name: Check coverage threshold
run: |
COVERAGE=$(poetry run coverage report | grep TOTAL | awk '{print $4}' | sed 's/%//')
echo "Coverage: $COVERAGE%"
if (( $(echo "$COVERAGE < 80" | bc -l) )); then
echo "❌ FAILED: Coverage is below 80% ($COVERAGE%)"
exit 1
fi
echo "✅ Coverage meets threshold: $COVERAGE%"
- name: ✅ Tests Passed
run: echo "All tests passed with adequate coverage!"
# Optional: Security scanning
security:
name: Security Scan
runs-on: ubuntu-latest
needs: [lint, test]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Bandit (security linter)
uses: tj-actions/bandit@v5.1
with:
targets: |
app/
options: "-r -ll -s B104"