|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Pre-commit hook for TelemetryFlow Python SDK |
| 4 | +# Runs: black, flake8/ruff, mypy, and security scan (bandit) |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +echo "Running pre-commit checks..." |
| 9 | + |
| 10 | +# Colors for output |
| 11 | +RED='\033[0;31m' |
| 12 | +GREEN='\033[0;32m' |
| 13 | +YELLOW='\033[1;33m' |
| 14 | +NC='\033[0m' # No Color |
| 15 | + |
| 16 | +# Get staged Python files |
| 17 | +STAGED_PY_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.py$' || true) |
| 18 | + |
| 19 | +if [ -z "$STAGED_PY_FILES" ]; then |
| 20 | + echo -e "${YELLOW}No Python files staged for commit. Skipping Python checks.${NC}" |
| 21 | + exit 0 |
| 22 | +fi |
| 23 | + |
| 24 | +# Function to check if a command exists |
| 25 | +command_exists() { |
| 26 | + command -v "$1" >/dev/null 2>&1 |
| 27 | +} |
| 28 | + |
| 29 | +FAILED=0 |
| 30 | + |
| 31 | +# 1. Check black formatting |
| 32 | +echo -e "\n${YELLOW}[1/4] Running black (formatter)...${NC}" |
| 33 | +if command_exists black; then |
| 34 | + if ! black --check --quiet $STAGED_PY_FILES 2>&1; then |
| 35 | + echo -e "${RED}black found formatting issues:${NC}" |
| 36 | + black --diff $STAGED_PY_FILES 2>&1 || true |
| 37 | + echo -e "${YELLOW}Run 'black <file>' to fix${NC}" |
| 38 | + FAILED=1 |
| 39 | + else |
| 40 | + echo -e "${GREEN}black passed${NC}" |
| 41 | + fi |
| 42 | +else |
| 43 | + echo -e "${YELLOW}black not installed. Skipping format check.${NC}" |
| 44 | + echo -e "${YELLOW}Install with: pip install black${NC}" |
| 45 | +fi |
| 46 | + |
| 47 | +# 2. Run linter (ruff or flake8) |
| 48 | +echo -e "\n${YELLOW}[2/4] Running linter...${NC}" |
| 49 | +if command_exists ruff; then |
| 50 | + if ! ruff check $STAGED_PY_FILES 2>&1; then |
| 51 | + echo -e "${RED}ruff found issues${NC}" |
| 52 | + FAILED=1 |
| 53 | + else |
| 54 | + echo -e "${GREEN}ruff passed${NC}" |
| 55 | + fi |
| 56 | +elif command_exists flake8; then |
| 57 | + if ! flake8 $STAGED_PY_FILES 2>&1; then |
| 58 | + echo -e "${RED}flake8 found issues${NC}" |
| 59 | + FAILED=1 |
| 60 | + else |
| 61 | + echo -e "${GREEN}flake8 passed${NC}" |
| 62 | + fi |
| 63 | +else |
| 64 | + echo -e "${YELLOW}ruff/flake8 not installed. Skipping lint check.${NC}" |
| 65 | + echo -e "${YELLOW}Install with: pip install ruff${NC}" |
| 66 | + echo -e "${YELLOW}Or: pip install flake8${NC}" |
| 67 | +fi |
| 68 | + |
| 69 | +# 3. Run type checker (mypy) |
| 70 | +echo -e "\n${YELLOW}[3/4] Running mypy (type checker)...${NC}" |
| 71 | +if command_exists mypy; then |
| 72 | + if ! mypy $STAGED_PY_FILES --ignore-missing-imports 2>&1; then |
| 73 | + echo -e "${RED}mypy found type issues${NC}" |
| 74 | + # Type issues are warnings, not failures by default |
| 75 | + echo -e "${YELLOW}(mypy issues are warnings only)${NC}" |
| 76 | + else |
| 77 | + echo -e "${GREEN}mypy passed${NC}" |
| 78 | + fi |
| 79 | +else |
| 80 | + echo -e "${YELLOW}mypy not installed. Skipping type check.${NC}" |
| 81 | + echo -e "${YELLOW}Install with: pip install mypy${NC}" |
| 82 | +fi |
| 83 | + |
| 84 | +# 4. Run security scan (bandit) |
| 85 | +echo -e "\n${YELLOW}[4/4] Running security scan...${NC}" |
| 86 | +if command_exists bandit; then |
| 87 | + if ! bandit -r $STAGED_PY_FILES -ll -q 2>&1; then |
| 88 | + echo -e "${RED}bandit found security issues${NC}" |
| 89 | + FAILED=1 |
| 90 | + else |
| 91 | + echo -e "${GREEN}bandit passed${NC}" |
| 92 | + fi |
| 93 | +elif command_exists safety; then |
| 94 | + echo -e "${YELLOW}Running safety check on dependencies...${NC}" |
| 95 | + if ! safety check --short-report 2>&1; then |
| 96 | + echo -e "${RED}safety found vulnerable dependencies${NC}" |
| 97 | + FAILED=1 |
| 98 | + else |
| 99 | + echo -e "${GREEN}safety passed${NC}" |
| 100 | + fi |
| 101 | +else |
| 102 | + echo -e "${YELLOW}bandit/safety not installed. Skipping security scan.${NC}" |
| 103 | + echo -e "${YELLOW}Install with: pip install bandit${NC}" |
| 104 | + echo -e "${YELLOW}Or: pip install safety${NC}" |
| 105 | +fi |
| 106 | + |
| 107 | +if [ $FAILED -ne 0 ]; then |
| 108 | + echo -e "\n${RED}Pre-commit checks failed. Please fix the issues above.${NC}" |
| 109 | + exit 1 |
| 110 | +fi |
| 111 | + |
| 112 | +echo -e "\n${GREEN}All pre-commit checks passed!${NC}" |
| 113 | +exit 0 |
0 commit comments