|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Pre-push hook to run Python linting |
| 4 | +# This script will block the push if linting fails |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +echo "🔍 Running pre-push checks..." |
| 9 | + |
| 10 | +# Colors for output |
| 11 | +RED='\033[0;31m' |
| 12 | +GREEN='\033[0;32m' |
| 13 | +YELLOW='\033[1;33m' |
| 14 | +BLUE='\033[0;34m' |
| 15 | +NC='\033[0m' # No Color |
| 16 | + |
| 17 | +# Function to print colored output |
| 18 | +print_error() { |
| 19 | + echo -e "${RED}❌ $1${NC}" |
| 20 | +} |
| 21 | + |
| 22 | +print_success() { |
| 23 | + echo -e "${GREEN}✅ $1${NC}" |
| 24 | +} |
| 25 | + |
| 26 | +print_warning() { |
| 27 | + echo -e "${YELLOW}⚠️ $1${NC}" |
| 28 | +} |
| 29 | + |
| 30 | +print_info() { |
| 31 | + echo -e "${BLUE}ℹ️ $1${NC}" |
| 32 | +} |
| 33 | + |
| 34 | +# Check if we're in the right directory |
| 35 | +if [ ! -f "action.yml" ] || [ ! -d "src" ]; then |
| 36 | + print_error "This doesn't appear to be the contrast-ai-smartfix-action repository root" |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +# Run Python linting |
| 41 | +echo "" |
| 42 | +print_info "Running Python linter..." |
| 43 | + |
| 44 | +# Check if flake8 is installed, if not try to install it |
| 45 | +if ! command -v flake8 &> /dev/null; then |
| 46 | + print_warning "flake8 not found. Attempting to install..." |
| 47 | + if command -v pip &> /dev/null; then |
| 48 | + pip install flake8 |
| 49 | + elif command -v pip3 &> /dev/null; then |
| 50 | + pip3 install flake8 |
| 51 | + else |
| 52 | + print_error "Neither pip nor pip3 found. Please install flake8 manually:" |
| 53 | + print_error "pip install flake8" |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | +fi |
| 57 | + |
| 58 | +# Find all Python files in src/ and test/ directories |
| 59 | +PYTHON_FILES=$(find src/ test/ -name "*.py" 2>/dev/null || true) |
| 60 | + |
| 61 | +if [ -z "$PYTHON_FILES" ]; then |
| 62 | + print_warning "No Python files found in src/ or test/ directories" |
| 63 | + exit 0 |
| 64 | +fi |
| 65 | + |
| 66 | +echo "📁 Found Python files:" |
| 67 | +echo "$PYTHON_FILES" | sed 's/^/ /' |
| 68 | + |
| 69 | +# Run flake8 on the Python files |
| 70 | +echo "" |
| 71 | +echo "🧹 Running flake8 linter..." |
| 72 | + |
| 73 | +if flake8 $PYTHON_FILES; then |
| 74 | + print_success "All Python files passed linting!" |
| 75 | + echo "" |
| 76 | + print_success "Pre-push linting check completed successfully!" |
| 77 | + echo "🚀 Push proceeding..." |
| 78 | + exit 0 |
| 79 | +else |
| 80 | + echo "" |
| 81 | + print_error "Linting failed! Push blocked." |
| 82 | + echo "" |
| 83 | + echo "💡 To fix linting issues:" |
| 84 | + echo " 1. Review the errors above" |
| 85 | + echo " 2. Fix the issues in your code" |
| 86 | + echo " 3. Commit your fixes" |
| 87 | + echo " 4. Try pushing again" |
| 88 | + echo "" |
| 89 | + echo "🔧 To skip all pre-push checks (not recommended):" |
| 90 | + echo " git push --no-verify" |
| 91 | + echo "" |
| 92 | + exit 1 |
| 93 | +fi |
0 commit comments