# 🔍 Quality Gates — Klaus-proxy
Guía de herramientas de calidad de código, checks automáticos y configuración local.
## 🤔 ¿Qué hago? ¿Cómo lo hago? ¿Y para qué lo hago?
**¿Qué hago?** Garantizo que todo código es correcto, seguro y de alta calidad antes de llegar a producción.
**¿Cómo lo hago?** Con 5 herramientas automáticas:
- **Ruff** — linting (busca errores, anti-patterns)
- **Black** — formatting (código consistente)
- **Mypy** — type checking (validar tipos)
- **Pytest-cov** — test coverage (asegurar que todo se prueba)
- **Bandit + Safety** — security scanning (detectar vulnerabilidades)
**¿Para qué?** Para evitar bugs, mantener consistencia, detectar problemas de seguridad antes de prod.
---
## 🚀 Setup local
### 1. Instalar pre-commit
```bash
pip install pre-commit
pre-commit install
```
### 2. Correr checks manuales
```bash
# Linting
ruff check proxy/ tests/
# Formatting
black proxy/ tests/
# Type checking
mypy proxy/
# Tests + coverage
pytest tests/ --cov=proxy --cov-fail-under=80
# Security
bandit -r proxy/ -ll
safety check
```
### 3. Auto-fix lo que se pueda
```bash
# Ruff auto-fix
ruff check proxy/ --fix
# Black auto-format
black proxy/ tests/
```
## 🔄 CI/CD Pipeline
Cada PR ejecuta automáticamente:
```mermaid
graph LR
A["👀 PR"] → B["🎨 Ruff"]
B → C["🖌️ Black"]
C → D["📝 Mypy"]
D → E["🔐 Bandit"]
E → F["📦 Safety"]
F → G["✅ Tests"]
G → H{Coverage ≥ 80%?}
H → |No| I["❌ Fail"]
H → |Yes| J["✅ Pass"]
```
**Requisitos para merge:**
- ✅ Ruff sin errores
- ✅ Black formato OK
- ✅ Mypy type check OK
- ✅ Bandit security OK
- ✅ Safety dependencies OK
- ✅ Tests pasen + coverage ≥ 80%
## 📝 Configuración
### pyproject.toml
```toml
[tool.ruff]
line-length = 100
target-version = "py312"
[tool.black]
line-length = 100
[tool.coverage.report]
fail_under = 80 # Falla si cobertura < 80%
[tool.pytest.ini_options]
testpaths = ["tests"]
```
### .pre-commit-config.yaml
Hooks que corren antes de cada commit:
```yaml
- Ruff lint + format
- Black format
- Mypy type checking
- Bandit security
- Safety dependencies
```
## 🆘 Troubleshooting
| Problema | Solución |
| --- | --- |
| Ruff falla | Correr `ruff check --fix` |
| Black falla | Correr `black proxy/ tests/` |
| Mypy falla | Revisar tipos; usar `# type: ignore` si necesario |
| Coverage baja | Agregar tests; ejecutar `pytest --cov` |
| Bandit alerta | Revisar; marcar como safe si es falso positivo |
## 🔗 Referencias
- [Ruff docs](https://docs.astral.sh/ruff/)
- [Black docs](https://black.readthedocs.io/)
- [Mypy docs](https://mypy.readthedocs.io/)
- [Pytest-cov docs](https://pytest-cov.readthedocs.io/)
- [Bandit docs](https://bandit.readthedocs.io/)
- [pyproject.toml](../pyproject.toml)
- [.pre-commit-config.yaml](../.pre-commit-config.yaml)