hooks.sh 2.0 KB

12345678910111213141516171819202122232425262728293031
  1. #!/bin/sh
  2. # Usage: hooks.sh banner | metrics | unanswered | tip push|pr. Runs hooks.py when python3 is 3.9 or newer; metrics also runs when python or py is.
  3. if [ "$1" = tip ]; then
  4. if [ "$GITHUB_ACTIONS" = true ] || [ "$CI" = true ]; then exit 0; fi
  5. case "$CLAUDE_SECURITY_SCAN_TIP" in [Oo][Ff][Ff]) exit 0 ;; esac
  6. unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_COMMON_DIR
  7. fi
  8. floor='sys.exit(3 * (sys.version_info < (3, 9)))'
  9. probe='import sys; sys.stdout.write("%d.%d.%d" % sys.version_info[:3]); '"$floor"
  10. python=python3
  11. failed=
  12. have=$(python3 -c "$probe" 2>/dev/null </dev/null); status=$?
  13. if [ "$1" = metrics ]; then
  14. [ "$status" -eq 0 ] || { failed=python3; python=; }
  15. # The fallback runs by the path its probe printed, not by name: py is a launcher and "py hooks.py" may choose a different Python than "py -c" did.
  16. exe_probe='import sys; sys.stdout.write(sys.executable.replace("\\", "/")); '"$floor"
  17. for candidate in python py; do
  18. if found=$("$candidate" -c "$exe_probe" 2>/dev/null </dev/null); then python=${python:-$found}; status=0; else failed="$failed $candidate"; fi
  19. done
  20. fi
  21. case "$status:$1" in
  22. 0:unanswered) python3 "$(dirname -- "$0")/hooks.py" unanswered 2>/dev/null ;;
  23. # $failed is unquoted so each failed name is its own argument; each name probed must be in hooks.py's INTERPRETERS.
  24. 0:metrics) "$python" "$(dirname -- "$0")/hooks.py" metrics $failed 2>/dev/null ;;
  25. 0:tip) GIT_TERMINAL_PROMPT=0 GIT_NO_LAZY_FETCH=1 \
  26. python3 "$(dirname -- "$0")/hooks.py" tip "$2" "$CLAUDE_PLUGIN_DATA" "$CLAUDE_SECURITY_SCAN_TIP" 2>/dev/null ;;
  27. 0:banner) python3 "$(dirname -- "$0")/hooks.py" banner "$CLAUDE_PLUGIN_DATA" ;;
  28. 3:banner) printf '{"systemMessage": "\\n\\u26a0\\ufe0f Claude Security needs python3 3.9 or newer, but this python3 is %s. Scanning and fixing will fail until a newer python3 is first on PATH.\\n"}' "$have" ;;
  29. *:banner) printf '%s\n' '{"systemMessage":"\n⚠️ Claude Security needs a working python3 (3.9 or newer) on PATH and could not run one. Install Python 3, then start a new session.\n"}' ;;
  30. esac
  31. exit 0