#!/usr/bin/env bash # PreToolUse hook for Bash: refuses a PR merge unless the PR's newest review # comment is a pass for its current head commit. # # Why: the /merge skill tells Claude to review before merging. This hook makes # skipping the review, or merging after new commits made a review stale, fail # instead of relying on the instruction alone. It can't check that the review # behind a marker was done properly. The ask rule for merges in # .claude/settings.json stays as a second layer, so every merge also prompts. # # A merge must be exactly one command of the form # .claude/scripts/gitea.sh POST /repos/OWNER/REPO/pulls/N/merge @FILE # where FILE pins head_commit_id. Any other write call whose path mentions # "merge", or a path it can't read safely, is refused. # # Fails closed: exit 2 blocks the tool call, and any unexpected error ends in # exit 2 too, because Claude Code treats other exit codes as non-blocking. set -uo pipefail deny() { echo "Merge refused: $1" >&2 exit 2 } trap 'rc=$?; if [[ $rc -ne 0 && $rc -ne 2 ]]; then echo "Merge refused: guard-merge.sh failed (exit $rc)" >&2; exit 2; fi' EXIT input=$(cat) # Most commands mention neither a merge nor gitea.sh; let them through before parsing. grep -qiE 'merge|gitea\.sh' <<<"$input" || exit 0 cmd=$(jq -r '.tool_input.command // empty' <<<"$input") || deny "could not read the hook input" [[ -z $cmd ]] && exit 0 shopt -s nocasematch # Merges go through gitea.sh only. if [[ $cmd =~ (curl|wget|http) && $cmd =~ /api/v1 && $cmd =~ merge ]]; then deny "merge PRs through .claude/scripts/gitea.sh, not curl or wget" fi # Find gitea.sh write calls. Their path must be a plain literal so it can't # hide a merge (variables, dot segments, encoding). Legitimate calls never need more. write_re="gitea\.sh[\"']?[[:space:]]+[\"']?(POST|PUT|PATCH|DELETE)[\"']?[[:space:]]+[\"']?([^[:space:]\"']*)" [[ $cmd =~ $write_re ]] || exit 0 path="${BASH_REMATCH[2]}" safe_path='^/[A-Za-z0-9_/?=&-]+$' if ! [[ $path =~ $safe_path ]] || [[ $path == *//* ]]; then deny "gitea.sh write call with a path that isn't plain ($path)" fi [[ $path =~ merge ]] || exit 0 shopt -u nocasematch strict_re='^(\.claude/scripts/|/[^[:space:]]*/\.claude/scripts/)gitea\.sh POST (/repos/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)/pulls/([0-9]+)/merge @([^[:space:];&|<>`$]+)$' [[ $cmd =~ $strict_re ]] \ || deny "a merge must be the single command '.claude/scripts/gitea.sh POST /repos/OWNER/REPO/pulls/N/merge @FILE'" repo="${BASH_REMATCH[2]}" pr="${BASH_REMATCH[3]}" body_file="${BASH_REMATCH[4]}" gitea="${CLAUDE_PROJECT_DIR:?}/.claude/scripts/gitea.sh" pr_json=$("$gitea" GET "$repo/pulls/$pr" 2>/dev/null) || deny "could not read PR #$pr" sha=$(jq -r '.head.sha // empty' <<<"$pr_json" 2>/dev/null) || deny "could not parse PR #$pr" [[ $sha =~ ^[0-9a-f]{40}$ ]] || deny "could not read the head commit of PR #$pr" pinned=$(jq -r '.head_commit_id // empty' "$body_file" 2>/dev/null) || deny "could not read $body_file" [[ $pinned == "$sha" ]] || deny "head_commit_id ($pinned) is not the PR's head commit ($sha)" # Collect all comments (oldest first) so the newest marker is really the newest. comments='[]' for page in 1 2 3 4 5 6 7 8 9 10; do batch=$("$gitea" GET "$repo/issues/$pr/comments?page=$page&limit=50" 2>/dev/null) \ || deny "could not read the comments of PR #$pr" count=$(jq 'length' <<<"$batch" 2>/dev/null) || deny "could not parse the comments of PR #$pr" [[ $count -eq 0 ]] && break comments=$(jq -s 'add' <(echo "$comments") <(echo "$batch")) || deny "could not merge comment pages" [[ $page -eq 10 ]] && deny "PR #$pr has too many comments to check" done latest=$(jq -r '[.[] | select(.user.login == "claude-bot" and (.body | startswith("" ]] \ || deny "the newest review of PR #$pr is not a pass for head commit $sha (found: ${latest:-none}). Run the review step of /merge first." exit 0