/merge takes changes from commit to merged PR in one run. A PreToolUse hook refuses any merge without a passing review comment for the PR's current head commit, so the review can't be skipped or go stale after new commits.
53 lines
2.1 KiB
Bash
Executable File
53 lines
2.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# PreToolUse hook for Bash: refuses a PR merge through gitea.sh unless the PR
|
|
# has a passing review comment 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 checks that a claude-bot
|
|
# comment with `<!-- claude-review: pass <head sha> -->` exists and that the
|
|
# merge body pins `head_commit_id` to that same SHA. It can't check that the
|
|
# review behind the marker was done properly.
|
|
#
|
|
# Fails closed: if the PR or its comments can't be read, the merge is refused.
|
|
set -euo pipefail
|
|
|
|
input=$(cat)
|
|
cmd=$(jq -r '.tool_input.command // empty' <<<"$input")
|
|
|
|
merge_re="gitea\.sh[[:space:]]+POST[[:space:]]+[\"']?(/repos/[^/[:space:]]+/[^/[:space:]]+)/pulls/([0-9]+)/merge"
|
|
[[ $cmd =~ $merge_re ]] || exit 0
|
|
repo="${BASH_REMATCH[1]}"
|
|
pr="${BASH_REMATCH[2]}"
|
|
|
|
deny() {
|
|
jq -n --arg reason "Merge refused: $1" '{
|
|
hookSpecificOutput: {
|
|
hookEventName: "PreToolUse",
|
|
permissionDecision: "deny",
|
|
permissionDecisionReason: $reason
|
|
}
|
|
}'
|
|
exit 0
|
|
}
|
|
|
|
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")
|
|
[[ $sha =~ ^[0-9a-f]{40}$ ]] || deny "could not read the head commit of PR #$pr"
|
|
|
|
body_re="[[:space:]]@([^[:space:]\"']+)"
|
|
[[ $cmd =~ $body_re ]] || deny "the merge body must be a file (@file) with head_commit_id"
|
|
body_file="${BASH_REMATCH[1]}"
|
|
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)"
|
|
|
|
comments=$("$gitea" GET "$repo/issues/$pr/comments" 2>/dev/null) || deny "could not read the comments of PR #$pr"
|
|
marker="<!-- claude-review: pass $sha -->"
|
|
jq -e --arg m "$marker" 'any(.[]; .user.login == "claude-bot" and (.body | startswith($m)))' \
|
|
<<<"$comments" >/dev/null \
|
|
|| deny "no passing review for head commit $sha. Run the review step of /merge first."
|
|
|
|
exit 0
|