File size: 2,869 Bytes
440cddf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
# PRIX Pre-commit Hook Installer
# Usage: ./install-hooks.sh

set -e

HOOK_DIR="$(git rev-parse --show-toplevel)/.git/hooks"
PRECOMMIT_HOOK="$HOOK_DIR/pre-commit"

echo "🔧 Installing PRIX pre-commit hook..."

if [ ! -d "$HOOK_DIR" ]; then
    echo "❌ Error: .git/hooks directory not found. Are you in a git repository?"
    exit 1
fi

if [ -f "$PRECOMMIT_HOOK" ]; then
    echo "⚠️  Existing pre-commit hook found. Backing up to pre-commit.bak"
    cp "$PRECOMMIT_HOOK" "$HOOK_DIR/pre-commit.bak"
fi

cat > "$PRECOMMIT_HOOK" << 'EOF'
#!/bin/bash
# PRIX AI Code Review - Pre-commit Hook
# Generated by PRIX

# Configuration
PRIX_MIN_SEVERITY="${PRIX_MIN_SEVERITY:-minor}"
PRIX_MIN_CONFIDENCE="${PRIX_MIN_CONFIDENCE:-60}"
PRIX_DISABLE="${PRIX_DISABLE:-false}"

# Colors
RED='\033[0;31m'
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
NC='\033[0m'

if [ "$PRIX_DISABLE" = "true" ]; then
    echo "ℹ️  PRIX pre-commit disabled (PRIX_DISABLE=true)"
    exit 0
fi

echo "🔍 PRIX: Running AI code review on staged files..."

# Check for required environment variables
if [ -z "$GROQ_API_KEY" ] && [ -z "$AI_API_KEY" ]; then
    echo -e "${YELLOW}⚠️  Warning: GROQ_API_KEY or AI_API_KEY not set. Skipping PRIX review.${NC}"
    echo "   Set these environment variables to enable pre-commit review."
    exit 0
fi

# Get staged files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(ts|tsx|js|jsx|go|py)$' || true)

if [ -z "$STAGED_FILES" ]; then
    echo "ℹ️  No supported files staged for review"
    exit 0
fi

FILE_COUNT=$(echo "$STAGED_FILES" | wc -l)
echo "📋 Found $FILE_COUNT file(s) to review"

# Run PRIX CLI
npx prix-cli \
    --staged \
    --min-severity "$PRIX_MIN_SEVERITY" \
    --min-confidence "$PRIX_MIN_CONFIDENCE" \
    --output console 2>&1

EXIT_CODE=$?

if [ $EXIT_CODE -eq 0 ]; then
    echo -e "${GREEN}✅ PRIX: No critical issues found${NC}"
elif [ $EXIT_CODE -eq 1 ]; then
    if [ -n "$CI" ]; then
        echo -e "${RED}❌ PRIX: Critical issues found. Commit blocked in CI.${NC}"
        echo "   To bypass this hook in local development, use: git commit --no-verify"
    else
        echo -e "${YELLOW}⚠️  PRIX: Issues found. Review recommended but not blocking.${NC}"
        EXIT_CODE=0
    fi
fi

exit $EXIT_CODE
EOF

chmod +x "$PRECOMMIT_HOOK"

echo -e "${GREEN}✅ PRIX pre-commit hook installed!${NC}"
echo ""
echo "Configuration (environment variables):"
echo "  PRIX_MIN_SEVERITY  - Minimum severity to report (critical|major|minor|info) [default: minor]"
echo "  PRIX_MIN_CONFIDENCE - Minimum confidence threshold 0-100 [default: 60]"
echo "  PRIX_DISABLE       - Set to 'true' to skip review [default: false]"
echo ""
echo "To bypass this hook locally: git commit --no-verify"