#!/bin/sh # Pre-commit hook to prevent manual editing of autogenerated validation rules # This script checks if any rules files have been manually modified set -eu SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" # Color codes for output RED='\033[0;31m' YELLOW='\033[1;33m' GREEN='\033[0;32m' NC='\033[0m' # No Color # Note: RULES_DIR check removed - not used in this version # Function to check if a file looks manually edited check_file_manually_edited() { file="$1" # Check if file has the autogenerated header if ! head -n 5 "$file" | grep -q "DO NOT EDIT MANUALLY"; then printf '%b⚠️ SUSPICIOUS: %s missing '\''DO NOT EDIT MANUALLY'\'' header%b\n' "$RED" "$file" "$NC" return 1 fi # Check if file has generator version if ! grep -q "Generated by update-validators.py" "$file"; then printf '%b⚠️ SUSPICIOUS: %s missing generator attribution%b\n' "$RED" "$file" "$NC" return 1 fi return 0 } # Function to check if rules are up-to-date check_rules_up_to_date() { printf '%b🔍 Checking if validation rules are up-to-date...%b\n' "$YELLOW" "$NC" # Run the update script in dry-run mode if cd "$PROJECT_ROOT" && python3 validate-inputs/scripts/update-validators.py --dry-run >/dev/null 2>&1; then printf '%b✅ Validation rules are up-to-date%b\n' "$GREEN" "$NC" return 0 else printf '%b❌ Validation rules are out-of-date%b\n' "$RED" "$NC" printf '%b💡 Run '\''make update-validators'\'' to regenerate rules%b\n' "$YELLOW" "$NC" return 1 fi } # Main check function main() { exit_code=0 files_checked=0 printf '%b🛡️ Checking autogenerated validation rules...%b\n' "$YELLOW" "$NC" # Check all rules.yml files in action directories # Store find results in a temp file to avoid subshell tmpfile=$(mktemp) find "$PROJECT_ROOT" -path "*/rules.yml" -type f 2>/dev/null > "$tmpfile" while IFS= read -r file; do if [ -f "$file" ]; then files_checked=$((files_checked + 1)) if ! check_file_manually_edited "$file"; then exit_code=1 fi fi done < "$tmpfile" rm -f "$tmpfile" if [ "$files_checked" -eq 0 ]; then printf '%b⚠️ No validation rule files found%b\n' "$YELLOW" "$NC" return 0 fi # Check if rules are up-to-date if ! check_rules_up_to_date; then exit_code=1 fi if [ "$exit_code" -eq 0 ]; then printf '%b✅ All %d validation rules look properly autogenerated%b\n' "$GREEN" "$files_checked" "$NC" else printf "\n" printf '%b❌ VALIDATION RULES CHECK FAILED%b\n' "$RED" "$NC" printf '%b📋 To fix these issues:%b\n' "$YELLOW" "$NC" printf " 1. Revert any manual changes to rules files\n" printf " 2. Run 'make update-validators' to regenerate rules\n" printf " 3. Modify generator logic in update-validators.py if needed\n" printf "\n" printf '%b📖 Rules are now stored as rules.yml in each action folder%b\n' "$YELLOW" "$NC" fi return $exit_code } # Run the check main "$@"