Skip to content

Add warn_gh_api PreToolUse hook to defer gh api calls#277

Merged
ikuwow merged 2 commits into
mainfrom
add-gh-api-warn-hook
Jul 9, 2026
Merged

Add warn_gh_api PreToolUse hook to defer gh api calls#277
ikuwow merged 2 commits into
mainfrom
add-gh-api-warn-hook

Conversation

@ikuwow

@ikuwow ikuwow commented Jul 9, 2026

Copy link
Copy Markdown
Owner

Summary

Add a PreToolUse Bash hook that intercepts gh api / gh api graphql ... and, on the first occurrence per session per normalized command, denies with a message pointing at high-level gh subcommands (gh pr view, gh pr diff, gh pr checks, gh issue view --comments, gh run view, and the gh pr-review extension). Re-running the exact same command lets it through, so the assistant can proceed after confirming gh api is actually needed.

Mechanizes the AIRULES.md rule that high-level gh subcommands are preferred and gh api should be a fallback. State management follows the same pattern as claude/hooks/warn_scripting_oneliners.py.

Detection matches gh at a shell segment head (after splitting on &&, ||, ;, newline, | outside quotes) and requires the first non-flag token after gh to be exactly api. This keeps gh pr view, github-api, gh api-helper, echo "gh api hi", and commit messages mentioning gh api from tripping the hook.

Test plan

  • python3 -m doctest claude/hooks/warn_gh_api.py -v — 22 doctests pass
  • Manual JSON payload tests via stdin: first gh api ... denies, second run of same command allows, gh pr view/echo "gh api ..." pass through
  • pre-commit run --all-files — all hooks pass
  • python3 -c "import json; json.load(open('claude/settings.json'))" — parses OK
  • End-to-end: after merge and ./scripts/deploy.sh, invoke gh api in a real Claude Code session and confirm the deny message appears, then verify the same command succeeds on the second attempt

ikuwow and others added 2 commits July 9, 2026 13:56
Follows the same first-occurrence-deny, whitespace-normalized-key
pattern as warn_scripting_oneliners.py, but targets `gh api` /
`gh api graphql` invocations and points at high-level gh subcommands
instead.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Wires the new PreToolUse Bash hook in after block_raw_github_fetch.py.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@ikuwow ikuwow marked this pull request as ready for review July 9, 2026 05:50
@ikuwow ikuwow merged commit 068882d into main Jul 9, 2026
6 of 10 checks passed
@ikuwow ikuwow deleted the add-gh-api-warn-hook branch July 9, 2026 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant