chore(claude): make the merge gate fail closed
The first review found ways around the hook: unexpected errors exited with non-blocking codes, unusual spellings of the merge path skipped it, and an older pass outlived a newer fail. The ask rule for merges is back as a second layer, so merging outside /merge is enforced, not just instructed.
This commit is contained in:
1 parent
737a3a29ec
commit
f45fb7421a
5 files changed
+73
-39
No files matched your search
@@ -1,52 +1,84 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# PreToolUse hook for Bash: refuses a PR merge through gitea.sh unless the PR
|
# PreToolUse hook for Bash: refuses a PR merge unless the PR's newest review
|
||||||
# has a passing review comment for its current head commit.
|
# comment is a pass for its current head commit.
|
||||||
#
|
#
|
||||||
# Why: the /merge skill tells Claude to review before merging. This hook makes
|
# 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
|
# 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
|
# instead of relying on the instruction alone. It can't check that the review
|
||||||
# comment with `<!-- claude-review: pass <head sha> -->` exists and that the
|
# behind a marker was done properly. The ask rule for merges in
|
||||||
# merge body pins `head_commit_id` to that same SHA. It can't check that the
|
# .claude/settings.json stays as a second layer, so every merge also prompts.
|
||||||
# review behind the marker was done properly.
|
|
||||||
#
|
#
|
||||||
# Fails closed: if the PR or its comments can't be read, the merge is refused.
|
# A merge must be exactly one command of the form
|
||||||
set -euo pipefail
|
# .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
|
||||||
input=$(cat)
|
# "merge", or a path it can't read safely, is refused.
|
||||||
cmd=$(jq -r '.tool_input.command // empty' <<<"$input")
|
#
|
||||||
|
# Fails closed: exit 2 blocks the tool call, and any unexpected error ends in
|
||||||
merge_re="gitea\.sh[[:space:]]+POST[[:space:]]+[\"']?(/repos/[^/[:space:]]+/[^/[:space:]]+)/pulls/([0-9]+)/merge"
|
# exit 2 too, because Claude Code treats other exit codes as non-blocking.
|
||||||
[[ $cmd =~ $merge_re ]] || exit 0
|
set -uo pipefail
|
||||||
repo="${BASH_REMATCH[1]}"
|
|
||||||
pr="${BASH_REMATCH[2]}"
|
|
||||||
|
|
||||||
deny() {
|
deny() {
|
||||||
jq -n --arg reason "Merge refused: $1" '{
|
echo "Merge refused: $1" >&2
|
||||||
hookSpecificOutput: {
|
exit 2
|
||||||
hookEventName: "PreToolUse",
|
|
||||||
permissionDecision: "deny",
|
|
||||||
permissionDecisionReason: $reason
|
|
||||||
}
|
|
||||||
}'
|
|
||||||
exit 0
|
|
||||||
}
|
}
|
||||||
|
trap 'rc=$?; if [[ $rc -ne 0 && $rc -ne 2 ]]; then echo "Merge refused: guard-merge.sh failed (exit $rc)" >&2; exit 2; fi' EXIT
|
||||||
|
|
||||||
gitea="$CLAUDE_PROJECT_DIR/.claude/scripts/gitea.sh"
|
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"
|
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=$(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"
|
[[ $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=$(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)"
|
[[ $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"
|
# Collect all comments (oldest first) so the newest marker is really the newest.
|
||||||
marker="<!-- claude-review: pass $sha -->"
|
comments='[]'
|
||||||
jq -e --arg m "$marker" 'any(.[]; .user.login == "claude-bot" and (.body | startswith($m)))' \
|
for page in 1 2 3 4 5 6 7 8 9 10; do
|
||||||
<<<"$comments" >/dev/null \
|
batch=$("$gitea" GET "$repo/issues/$pr/comments?page=$page&limit=50" 2>/dev/null) \
|
||||||
|| deny "no passing review for head commit $sha. Run the review step of /merge first."
|
|| 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("<!-- claude-review: ")))]
|
||||||
|
| sort_by(.created_at, .id) | last | .body // "" | split("\n")[0]' <<<"$comments") \
|
||||||
|
|| deny "could not read the review markers of PR #$pr"
|
||||||
|
[[ $latest == "<!-- claude-review: pass $sha -->" ]] \
|
||||||
|
|| 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
|
exit 0
|
||||||
@@ -5,10 +5,11 @@
|
|||||||
# This script calls the Gitea API instead, as the claude-bot account, so PRs
|
# This script calls the Gitea API instead, as the claude-bot account, so PRs
|
||||||
# show up as the bot's work and it only has the rights you gave it.
|
# show up as the bot's work and it only has the rights you gave it.
|
||||||
#
|
#
|
||||||
# Scope: used for PRs only (list, open, check CI status, merge after your OK).
|
# Scope: PRs (list, open, check CI status, comment, merge), and issues when the
|
||||||
# Technically it can call any endpoint; CLAUDE.md limits its use to PRs.
|
# user asks. Technically it can call any endpoint; CLAUDE.md limits its use.
|
||||||
# In .claude/settings.json, GET and POST (e.g. opening a PR) run without a
|
# In .claude/settings.json, GET and POST (e.g. opening a PR) run without a
|
||||||
# prompt. Merging a PR (POST .../merge), PATCH, PUT and DELETE always ask first.
|
# prompt. Merging a PR (POST .../merge), PATCH, PUT and DELETE always ask first,
|
||||||
|
# and .claude/hooks/guard-merge.sh refuses a merge without a passing review.
|
||||||
#
|
#
|
||||||
# Usage: gitea.sh METHOD PATH [BODY]
|
# Usage: gitea.sh METHOD PATH [BODY]
|
||||||
# METHOD GET | POST | PATCH | PUT | DELETE
|
# METHOD GET | POST | PATCH | PUT | DELETE
|
||||||
|
|||||||
@@ -60,6 +60,7 @@
|
|||||||
"Bash(pct *)",
|
"Bash(pct *)",
|
||||||
"Bash(qm *)",
|
"Bash(qm *)",
|
||||||
"Bash(curl *)",
|
"Bash(curl *)",
|
||||||
|
"Bash(.claude/scripts/gitea.sh POST */merge*)",
|
||||||
"Bash(.claude/scripts/gitea.sh PATCH *)",
|
"Bash(.claude/scripts/gitea.sh PATCH *)",
|
||||||
"Bash(.claude/scripts/gitea.sh PUT *)",
|
"Bash(.claude/scripts/gitea.sh PUT *)",
|
||||||
"Bash(.claude/scripts/gitea.sh DELETE *)"
|
"Bash(.claude/scripts/gitea.sh DELETE *)"
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ Never write a `pass` marker for a SHA you did not review, or when an important f
|
|||||||
|
|
||||||
## 8. Merge
|
## 8. Merge
|
||||||
|
|
||||||
1. Run `.claude/scripts/gitea.sh POST $REPO/pulls/<number>/merge @<json file>` with `{"Do":"merge","head_commit_id":"<sha>"}`, so only the commit CI and the review checked can merge. The `guard-merge.sh` hook refuses the call without a matching `pass` comment.
|
1. Run `.claude/scripts/gitea.sh POST $REPO/pulls/<number>/merge @<json file>` with `{"Do":"merge","head_commit_id":"<sha>"}`, so only the commit CI and the review checked can merge. Run it exactly in this form, alone. The `guard-merge.sh` hook refuses any other form, and refuses the call unless the newest review comment is a `pass` for this SHA. This call asks the user for permission: that prompt is the one intended pause of the run. If the user declines, stop.
|
||||||
2. Run `git fetch origin main:main`, then `git switch main`.
|
2. Run `git fetch origin main:main`, then `git switch main`.
|
||||||
|
|
||||||
## 9. Report
|
## 9. Report
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ Only commit when I explicitly say "commit" for that change. A yes to a question
|
|||||||
|
|
||||||
The remote is a self-hosted Gitea, so `gh` doesn't work. For PRs and their CI status use `.claude/scripts/gitea.sh` (acts as `claude-bot`), e.g. `.claude/scripts/gitea.sh GET '/repos/kasun/homelab-ansible/pulls?state=open'`. Use it for PRs, and for issues when I ask. Reading from Gitea is free.
|
The remote is a self-hosted Gitea, so `gh` doesn't work. For PRs and their CI status use `.claude/scripts/gitea.sh` (acts as `claude-bot`), e.g. `.claude/scripts/gitea.sh GET '/repos/kasun/homelab-ansible/pulls?state=open'`. Use it for PRs, and for issues when I ask. Reading from Gitea is free.
|
||||||
|
|
||||||
Open and merge PRs through `/merge`: typing it is my OK to commit, open a PR and merge for that run. Outside `/merge`, ask first. Every merge needs a passing review comment for the PR's head commit; `.claude/hooks/guard-merge.sh` refuses the merge otherwise. Before deploy-on-merge is enabled, revisit this: PRs that deploy will need my approval.
|
Open and merge PRs through `/merge`: typing it is my OK to commit, open a PR and merge for that run. Outside `/merge`, ask first. Every merge call asks for permission (also inside `/merge`), and `.claude/hooks/guard-merge.sh` refuses it unless the PR's newest review comment is a pass for its head commit. Before deploy-on-merge is enabled, revisit this: PRs that deploy will need my approval.
|
||||||
|
|
||||||
## Verifying changes
|
## Verifying changes
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user