⚑Pre-commit Setup

Based on Codacy analysis, this document outlines the comprehensive improvements made to our pre-commit hooks system.

🎯 Overview

We've enhanced your existing task-based workflow with proper pre-commit hooks that automatically catch and fix the issues identified by Codacy analysis.

What Was Added

βœ… Formal pre-commit hooks (.pre-commit-config.yaml) βœ… Security scanning (bandit, safety) βœ… Dependency vulnerability checks (trivy-style scanning) βœ… Dockerfile linting (hadolint) βœ… Shell script linting (shellcheck) βœ… Custom security checks (shell=True detection) βœ… Complexity analysis (radon) βœ… Enhanced task commands (security, complexity, hooks management)

πŸ› οΈ Quick Setup

# Run the setup script
./scripts/setup-precommit.sh

# Or manual setup:
uv sync --extra dev
task hooks:setup

πŸ“Š Coverage Matrix - Codacy Issues Addressed

Codacy Issue Category
Tool/Hook
Status
Priority

🚨 shell=True vulnerabilities

bandit, custom hook

βœ… Active

Critical

πŸ”’ Hardcoded credentials

bandit

βœ… Active

Medium

πŸ“ Code complexity

radon

βœ… Active

Medium

🎨 Trailing whitespace

trailing-whitespace

βœ… Active

Low

🎨 Dead code

ruff

βœ… Active

Low

πŸ“¦ Dependency CVEs

safety

βœ… Active

Critical

🐳 Dockerfile issues

hadolint

βœ… Active

Medium

πŸ“ YAML formatting

yamllint

βœ… Enhanced

Low

πŸ”§ Tools Configuration

Security Tools

Bandit (Python Security)

Addresses: Issues #61 (shell=True), #64 (hardcoded credentials)

Safety (Dependency Vulnerabilities)

Addresses: Issue #65 (dependency CVEs)

Custom Shell=True Check

Addresses: Issue #61 (prevents future shell=True usage)

Code Quality Tools

Radon (Complexity Analysis)

Addresses: Issue #62 (method complexity)

Hadolint (Dockerfile)

Addresses: Issue #66 (Dockerfile security)

πŸš€ Usage Guide

Daily Development Workflow

Commit Workflow

Security-Specific Commands

πŸ“‹ Hook Categories

πŸ”΄ Critical (Always Run)

  • black - Code formatting

  • ruff - Linting and import sorting

  • mypy - Type checking

  • bandit - Security scanning

  • check-subprocess-shell - Prevent shell=True

  • trailing-whitespace - Fix style issues

🟑 Manual (Run as Needed)

  • safety - Dependency vulnerabilities

  • complexity-check - Code complexity analysis

  • check-test-credentials - Test credential validation

🟒 Informational

  • hadolint - Dockerfile linting

  • shellcheck - Shell script linting

  • yamllint - YAML formatting

πŸŽ›οΈ Configuration Files

.pre-commit-config.yaml

Main configuration with all hooks and settings.

pyproject.toml (Enhanced)

Taskfile.yml (Enhanced)

Added security, complexity, and hooks management tasks.

πŸ”§ Customization

Adjusting Security Rules

Complexity Thresholds

Adding Custom Hooks

🎯 Next Steps

Immediate Actions

  1. Run setup: ./scripts/setup-precommit.sh

  2. Address critical issues: Focus on GitHub issues #61 and #65

  3. Test workflow: Make a test commit to verify hooks work

Ongoing Maintenance

  1. Weekly: task hooks:update to update hook versions

  2. Monthly: pre-commit run safety --hook-stage manual for dependency checks

  3. Before releases: task complexity to check code quality trends

Integration with CI/CD

Your existing autofix workflow will work seamlessly with these changes:

Consider adding:

πŸ€” Troubleshooting

Hook Failures

Performance Issues

πŸ“ˆ Benefits

Security

  • Automatic detection of shell=True vulnerabilities

  • Dependency scanning for known CVEs

  • Credential leak prevention in test files

Code Quality

  • Complexity monitoring to prevent technical debt

  • Consistent formatting across all files

  • Type safety enforcement

Developer Experience

  • Faster feedback - catch issues before CI

  • Automatic fixes for many issues

  • Clear error messages with fix suggestions

Team Consistency

  • Standardized workflow across all developers

  • Automated enforcement of coding standards

  • Reduced code review overhead


πŸŽ‰ Your pre-commit setup now addresses all Codacy issues while maintaining your existing workflow!

For questions or issues, refer to:

Last updated

Was this helpful?