Last active
October 31, 2026 20:16
-
-
Save quintonwall/36f2c1f3f833990add61ea86fab9d9d3 to your computer and use it in GitHub Desktop.
Github pre-commit hook using factory for code reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Report-only pre-commit review of the staged diff via Factory's Droid. | |
| # Blocks a commit (always exits 1) if verdict is FAIL | |
| set -uo pipefail | |
| command -v droid >/dev/null 2>&1 || { echo "[droid-review] droid not installed, skipping"; exit 0; } | |
| # Sensitive paths are excluded from the diff, always. Add to this list, never remove. | |
| EXCLUDES=( | |
| ':!.env' ':!.env.*' ':!**/.env' ':!**/.env.*' | |
| ':!*.pem' ':!*.key' ':!*.p12' ':!*.pfx' ':!*.crt' ':!*.jks' ':!*.keystore' | |
| ':!**/id_rsa*' ':!**/id_ed25519*' | |
| ':!**/credentials*' ':!**/secret*' ':!**/*token*' ':!**/*.secrets.*' | |
| ':!.npmrc' ':!**/.npmrc' ':!.netrc' ':!*.tfstate' ':!*.tfvars' | |
| ':!.learnmcp/**' ':!**/.learnmcp/**' ':!*.sqlite' ':!*.db' | |
| ':!.droid-review/**' ':!supabase/.temp/**' ':!*service-account*.json' ':!package-lock.json' | |
| ) | |
| DIFF=$(git diff --cached --diff-filter=ACM -- . "${EXCLUDES[@]}" | head -c 200000) | |
| [ -z "$DIFF" ] && exit 0 | |
| # If a secret scanner is available, never ship a diff that contains a secret. | |
| if command -v gitleaks >/dev/null 2>&1; then | |
| if ! gitleaks protect --staged --redact >/dev/null 2>&1; then | |
| echo "[droid-review] gitleaks flagged the staged changes, skipping AI review. Run: gitleaks protect --staged --redact" | |
| exit 0 | |
| fi | |
| fi | |
| # Run droid from an empty temp dir so its file tools can only see the filtered diff, | |
| # never the repo (and any .env or token files in it). | |
| SANDBOX=$(mktemp -d) | |
| trap 'rm -rf "$SANDBOX"' EXIT | |
| printf '%s' "$DIFF" > "$SANDBOX/staged.diff" | |
| ROOT=$(git rev-parse --show-toplevel) | |
| LOG_DIR="$ROOT/.droid-review" | |
| LOG="$LOG_DIR/review.log" | |
| umask 077 | |
| mkdir -p "$LOG_DIR" | |
| chmod 700 "$LOG_DIR" | |
| { | |
| echo "=== $(date '+%Y-%m-%d %H:%M:%S') branch=$(git rev-parse --abbrev-ref HEAD) ===" | |
| git diff --cached --name-only --diff-filter=ACM -- . "${EXCLUDES[@]}" | |
| echo "---" | |
| } >> "$LOG" | |
| echo "[droid-review] reviewing staged diff (log: .droid-review/review.log)..." | |
| OUT=$(cd "$SANDBOX" && droid exec \ | |
| "Review the git diff in ./staged.diff. It is the only file available; do not look elsewhere. Report only; do not modify anything. Flag: hardcoded secrets or credentials, \ | |
| injection/auth/authorization flaws, unsafe deserialization, sensitive data in logs, and likely regressions. \ | |
| The first line of your reply must be exactly 'VERDICT: FAIL' if you have any findings, or 'VERDICT: PASS' if you have none. \ | |
| After that, list findings as file:line - severity - issue." 2>&1) | |
| STATUS=$? | |
| echo "$OUT" | |
| # The log keeps only the verdict and file:line - severity. The issue text can quote a secret. | |
| { | |
| printf '%s\n' "$OUT" | sed -E -n 's/^VERDICT: (PASS|FAIL)[[:space:]]*$/VERDICT: \1/p' | head -n 1 | |
| printf '%s\n' "$OUT" | sed -E -n 's/^([^ ]+:[0-9]+ - [^-]+) - .*/\1 (details: terminal only)/p' | |
| } >> "$LOG" | |
| if [ "$STATUS" -ne 0 ]; then | |
| # Fail open: a broken or offline reviewer must not lock you out of committing. | |
| MSG="[droid-review] review failed to run, exit $STATUS (commit not blocked)" | |
| echo "$MSG"; echo "$MSG" >> "$LOG"; echo >> "$LOG" | |
| exit 0 | |
| fi | |
| echo >> "$LOG" | |
| if printf '%s' "$OUT" | grep -q '^VERDICT: FAIL'; then | |
| echo "[droid-review] BLOCKED: findings above. Fix and re-commit, or bypass with: git commit --no-verify" | |
| exit 1 | |
| fi | |
| if ! printf '%s' "$OUT" | grep -q '^VERDICT: PASS'; then | |
| echo "[droid-review] no verdict line in reply, treating as inconclusive (commit not blocked)" | |
| fi | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment