#!/usr/bin/env bash # Git Hooks Setup for MEV Bot CI/CD Integration # Creates git hooks that integrate with the CI/CD pipeline set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" HOOKS_DIR="$PROJECT_ROOT/.git/hooks" cd "$PROJECT_ROOT" log() { echo "[HOOKS-SETUP] $*" } error() { echo "[ERROR] $*" >&2 } # Check if we're in a git repository if ! git rev-parse --git-dir >/dev/null 2>&1; then error "Not in a git repository" exit 1 fi log "Setting up Git hooks integration with CI/CD pipeline..." # Create hooks directory if it doesn't exist mkdir -p "$HOOKS_DIR" # Pre-commit hook cat > "$HOOKS_DIR/pre-commit" << 'EOF' #!/usr/bin/env bash # Pre-commit hook - Fast validation before commit set -e echo "🔍 Running pre-commit validation..." # Check if we have staged files if git diff --cached --quiet; then echo "No staged changes to validate" exit 0 fi # Run fast CI validation if command -v make >/dev/null 2>&1; then echo "Running pre-commit CI pipeline..." make ci-precommit else echo "Running basic checks..." # Basic Go checks if command -v go >/dev/null 2>&1; then echo "Building..." go build ./cmd/mev-bot echo "Running tests..." go test ./pkg/... -short echo "Checking format..." if ! gofmt -l . | grep -q .; then echo "✅ Code formatting is clean" else echo "❌ Code needs formatting:" gofmt -l . echo "Run: gofmt -w ." exit 1 fi echo "Running vet..." go vet ./... fi fi echo "✅ Pre-commit validation passed" EOF # Pre-push hook cat > "$HOOKS_DIR/pre-push" << 'EOF' #!/usr/bin/env bash # Pre-push hook - Comprehensive validation before push set -e echo "🚀 Running pre-push validation..." # Get the branch being pushed branch=$(git rev-parse --abbrev-ref HEAD) echo "Validating branch: $branch" # Run appropriate CI based on branch type if command -v make >/dev/null 2>&1; then if [[ "$branch" =~ ^(feature|fix)/ ]]; then echo "Running development CI for feature/fix branch..." make ci-dev elif [[ "$branch" =~ ^release/ ]] || [[ "$branch" == "master" ]] || [[ "$branch" == "main" ]]; then echo "Running full CI for release/main branch..." make ci-full else echo "Running quick CI for other branches..." make ci-quick fi else echo "Running basic validation..." if command -v go >/dev/null 2>&1; then echo "Building..." go build ./cmd/mev-bot echo "Running full test suite..." go test ./... echo "Running static analysis..." go vet ./... fi fi echo "✅ Pre-push validation passed" EOF # Post-commit hook cat > "$HOOKS_DIR/post-commit" << 'EOF' #!/usr/bin/env bash # Post-commit hook - Optional post-commit actions # Get commit info commit_hash=$(git rev-parse HEAD) commit_msg=$(git log -1 --pretty=%B) branch=$(git rev-parse --abbrev-ref HEAD) echo "📝 Post-commit: $commit_hash on $branch" # Optional: Run quick smoke test after commit if [[ "$commit_msg" =~ ^(feat|fix|perf): ]]; then echo "Running smoke test for significant changes..." if command -v make >/dev/null 2>&1; then timeout 30 make ci-precommit || echo "Smoke test completed" fi fi EOF # Prepare-commit-msg hook cat > "$HOOKS_DIR/prepare-commit-msg" << 'EOF' #!/usr/bin/env bash # Prepare commit message hook - Add conventional commit format help commit_file="$1" commit_source="$2" # Only add template for regular commits (not merges, amendments, etc.) if [[ "$commit_source" == "" ]] || [[ "$commit_source" == "template" ]]; then # Get the branch name branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "main") # Add conventional commit template if message is empty if [[ ! -s "$commit_file" ]]; then cat >> "$commit_file" << 'TEMPLATE' # Conventional Commits Format: # type(scope): description # # Types: feat, fix, docs, style, refactor, perf, test, chore # Example: feat(parser): add support for multicall transactions # # Body (optional): # - Explain what and why vs. how # - Include motivation for the change # - Contrast with previous behavior # # Footer (optional): # - Breaking changes: BREAKING CHANGE: # - Issues: Closes #123, Fixes #456 TEMPLATE fi fi EOF # Post-merge hook cat > "$HOOKS_DIR/post-merge" << 'EOF' #!/usr/bin/env bash # Post-merge hook - Actions after merge echo "🔀 Post-merge validation..." # Run CI after merge to ensure integration is clean if command -v make >/dev/null 2>&1; then echo "Running post-merge CI validation..." make ci-dev else echo "Running basic post-merge checks..." if command -v go >/dev/null 2>&1; then go build ./cmd/mev-bot go test ./pkg/... -short fi fi # Check if dependencies changed if git diff HEAD@{1} --name-only | grep -q "go.mod\|go.sum"; then echo "📦 Dependencies changed, updating..." go mod tidy go mod verify fi echo "✅ Post-merge validation completed" EOF # Pre-rebase hook cat > "$HOOKS_DIR/pre-rebase" << 'EOF' #!/usr/bin/env bash # Pre-rebase hook - Validation before rebase upstream="$1" branch="$2" echo "🔄 Pre-rebase validation..." echo "Rebasing: ${branch:-$(git rev-parse --abbrev-ref HEAD)} onto $upstream" # Warn about rebasing public branches current_branch=${branch:-$(git rev-parse --abbrev-ref HEAD)} if [[ "$current_branch" == "master" ]] || [[ "$current_branch" == "main" ]] || [[ "$current_branch" == "develop" ]]; then echo "⚠️ WARNING: Rebasing public branch '$current_branch'" echo "This may rewrite history. Continue? (y/N)" read -r response if [[ ! "$response" =~ ^[Yy]$ ]]; then echo "Rebase cancelled" exit 1 fi fi # Run quick validation if command -v make >/dev/null 2>&1; then make ci-precommit fi echo "✅ Pre-rebase validation passed" EOF # Make all hooks executable chmod +x "$HOOKS_DIR"/* log "✅ Git hooks installed:" log " - pre-commit: Fast validation (build, test, format)" log " - pre-push: Comprehensive CI validation" log " - post-commit: Optional smoke tests" log " - prepare-commit-msg: Conventional commit template" log " - post-merge: Integration validation" log " - pre-rebase: Safety checks for public branches" echo "" log "🎯 Hook Integration Features:" log " - Automatic CI pipeline integration" log " - Branch-specific validation levels" log " - Conventional commit message formatting" log " - Dependency change detection" log " - Safety checks for public branch operations" echo "" log "💡 To disable hooks temporarily:" log " git commit --no-verify" log " git push --no-verify" echo "" log "🔧 To customize hooks, edit files in: .git/hooks/" # Test the hooks echo "" log "Testing hook installation..." if [[ -x "$HOOKS_DIR/pre-commit" ]]; then log "✅ Pre-commit hook installed and executable" else error "❌ Pre-commit hook installation failed" fi if [[ -x "$HOOKS_DIR/pre-push" ]]; then log "✅ Pre-push hook installed and executable" else error "❌ Pre-push hook installation failed" fi log "🎉 Git hooks setup completed successfully!" log "Next commit will use the new validation pipeline."