#!/bin/bash # Validate changed files before PR creation # Runs: shellcheck, JSON validation, Python syntax check set -euo pipefail ERRORS=0 # Get changed files compared to main CHANGED_FILES=$(git diff --name-only origin/main 2>/dev/null || git diff --name-only HEAD~1) echo "Validating changed files..." for file in $CHANGED_FILES; do [[ -f "$file" ]] || continue case "$file" in *.sh) if command -v shellcheck &>/dev/null; then if ! shellcheck -S warning "$file" 2>/dev/null; then echo "FAIL: shellcheck $file" ((ERRORS++)) else echo "OK: $file" fi else echo "SKIP: shellcheck not installed" fi ;; *.json) if ! python3 -m json.tool "$file" >/dev/null 2>&1; then echo "FAIL: invalid JSON $file" ((ERRORS++)) else echo "OK: $file" fi ;; *.yaml|*.yml) if command -v yamllint &>/dev/null; then if ! yamllint -d relaxed "$file" 2>/dev/null; then echo "FAIL: yamllint $file" ((ERRORS++)) else echo "OK: $file" fi elif python3 -c "import yaml" 2>/dev/null; then if ! python3 -c "import yaml; yaml.safe_load(open('$file'))" 2>/dev/null; then echo "FAIL: invalid YAML $file" ((ERRORS++)) else echo "OK: $file" fi else echo "SKIP: no YAML validator" fi ;; *.py) if ! python3 -m py_compile "$file" 2>/dev/null; then echo "FAIL: Python syntax $file" ((ERRORS++)) else echo "OK: $file" fi ;; *.md) echo "OK: $file (markdown, no validation)" ;; *) echo "SKIP: $file (no validator)" ;; esac done if [[ $ERRORS -gt 0 ]]; then echo "" echo "Validation failed with $ERRORS error(s)" exit 1 fi echo "" echo "All validations passed" exit 0