Add claude-code-spring module: CLAUDE.md, a path-scoped rule, two skills, four hooks run against real hook JSON, project .mcp.json from the claude mcp CLI, and tests that lint the setup on Spring Boot 4.2.0
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b86yyhhudsSLnwNUFY
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
a620d91d0a
commit
3aa20a1dce
@@ -0,0 +1,15 @@
|
||||
// The same deny rules as guard-bash.sh, written as a JDK 25 compact source file (JEP 512, final in 25).
|
||||
// Run by the hook as: java ${CLAUDE_PROJECT_DIR}/.claude/hooks/GuardBash.java
|
||||
// It exists to measure what a JVM hook costs next to bash + jq. It does not parse JSON properly.
|
||||
void main() throws Exception {
|
||||
String in = new String(System.in.readAllBytes());
|
||||
var m = java.util.regex.Pattern.compile("\"command\"\\s*:\\s*\"((?:[^\"\\\\]|\\\\.)*)\"").matcher(in);
|
||||
String cmd = m.find() ? m.group(1) : "";
|
||||
String reason = null;
|
||||
if (cmd.contains("mvn deploy") || cmd.contains("mvn release:")) reason = "Publishing artifacts is a human decision.";
|
||||
else if (cmd.contains("git push --force") || cmd.contains("git push -f")) reason = "Force-pushing is blocked in this project.";
|
||||
else if (cmd.contains("rm -rf")) reason = "rm -rf is blocked.";
|
||||
if (reason != null) {
|
||||
System.out.println("{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"deny\",\"permissionDecisionReason\":\"" + reason + "\"}}");
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
#!/bin/bash
|
||||
# PreToolUse hook for the Bash tool. Reads the hook JSON on stdin (needs jq).
|
||||
# Denies a few commands by returning a permissionDecision, which Claude sees as the reason.
|
||||
COMMAND=$(jq -r '.tool_input.command // empty')
|
||||
|
||||
deny() {
|
||||
jq -n --arg reason "$1" '{
|
||||
hookSpecificOutput: {
|
||||
hookEventName: "PreToolUse",
|
||||
permissionDecision: "deny",
|
||||
permissionDecisionReason: $reason
|
||||
}
|
||||
}'
|
||||
exit 0
|
||||
}
|
||||
|
||||
case "$COMMAND" in
|
||||
*"mvn deploy"*|*"mvn release:"*) deny "Publishing artifacts is a human decision. Run mvn verify instead." ;;
|
||||
*"git push --force"*|*"git push -f"*) deny "Force-pushing is blocked in this project." ;;
|
||||
*"rm -rf"*) deny "rm -rf is blocked. Delete specific files instead." ;;
|
||||
*"| sh"*|*"| bash"*) deny "Piping downloaded text into a shell is blocked." ;;
|
||||
esac
|
||||
exit 0
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
#!/bin/bash
|
||||
# PreToolUse hook for Edit|Write|MultiEdit. Exit code 2 blocks the call and sends stderr to Claude.
|
||||
INPUT=$(cat)
|
||||
TOOL=$(echo "$INPUT" | jq -r '.tool_name // empty')
|
||||
FILE=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
|
||||
|
||||
# Production config is never edited by the agent.
|
||||
case "$FILE" in
|
||||
*application-prod.*|*/.env|*/.env.*)
|
||||
echo "Blocked: $FILE is production configuration. Ask the user to change it." >&2
|
||||
exit 2 ;;
|
||||
esac
|
||||
|
||||
# An applied Flyway migration is immutable. Creating a new V<n> file is fine, changing an existing one is not.
|
||||
case "$FILE" in
|
||||
*/db/migration/V*.sql)
|
||||
if [ "$TOOL" != "Write" ] || [ -e "$FILE" ]; then
|
||||
echo "Blocked: $FILE is an existing Flyway migration. Add a new V<next>__*.sql file instead." >&2
|
||||
exit 2
|
||||
fi ;;
|
||||
esac
|
||||
exit 0
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
# Stop hook. Refuses to let Claude finish while the full suite is red.
|
||||
INPUT=$(cat)
|
||||
if [ "$(echo "$INPUT" | jq -r '.stop_hook_active')" = "true" ]; then
|
||||
exit 0 # we already pushed Claude back once this turn; let it stop and report
|
||||
fi
|
||||
|
||||
ROOT="${CLAUDE_PROJECT_DIR:-$(pwd)}"
|
||||
cd "$ROOT" || exit 0
|
||||
OUT=$(mvn -B -q test 2>&1)
|
||||
if [ $? -ne 0 ]; then
|
||||
SUMMARY=$(echo "$OUT" | awk '/^\[ERROR\] (Failures|Errors):/{p=1} p{print} p&&/^\[ERROR\] Tests run:/{exit}' | head -20)
|
||||
[ -z "$SUMMARY" ] && SUMMARY=$(echo "$OUT" | grep -E '^\[ERROR\]' | grep -v -E 'Help 1|re-run Maven|For more information|full stack trace|See .*surefire|MojoFailure|^\[ERROR\] *$' | head -8)
|
||||
[ -z "$SUMMARY" ] && SUMMARY=$(echo "$OUT" | tail -8)
|
||||
jq -n --arg r "The test suite is red. Fix it before finishing:
|
||||
$SUMMARY" '{decision: "block", reason: $r}'
|
||||
fi
|
||||
exit 0
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
#!/bin/bash
|
||||
# PostToolUse hook for Edit|Write|MultiEdit. Runs the test class that matches the edited Java file.
|
||||
# Green: silent, exit 0. Red: failure lines on stderr and exit 2, which Claude Code shows to Claude.
|
||||
INPUT=$(cat)
|
||||
FILE=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
|
||||
case "$FILE" in *.java) ;; *) exit 0 ;; esac
|
||||
|
||||
BASE=$(basename "$FILE" .java)
|
||||
case "$BASE" in
|
||||
*Test) TESTCLASS="$BASE" ;;
|
||||
*) TESTCLASS="${BASE}Test" ;;
|
||||
esac
|
||||
|
||||
ROOT="${CLAUDE_PROJECT_DIR:-$(pwd)}"
|
||||
cd "$ROOT" || exit 0
|
||||
if ! find src/test -name "${TESTCLASS}.java" | grep -q .; then
|
||||
exit 0 # no matching test class: nothing to run
|
||||
fi
|
||||
|
||||
OUT=$(mvn -B -q test -Dtest="$TESTCLASS" -Dsurefire.failIfNoSpecifiedTests=false 2>&1)
|
||||
STATUS=$?
|
||||
if [ $STATUS -ne 0 ]; then
|
||||
echo "Tests failed after editing $(basename "$FILE") ($TESTCLASS):" >&2
|
||||
# Surefire's "Failures:" block carries the assertion messages (expected/actual); prefer it.
|
||||
FOUND=$(echo "$OUT" | awk '/^\[ERROR\] (Failures|Errors):/{p=1} p{print} p&&/^\[ERROR\] Tests run:/{exit}' | head -20)
|
||||
[ -z "$FOUND" ] && FOUND=$(echo "$OUT" | grep -E '^\[ERROR\]' | grep -v -E 'Help 1|re-run Maven|For more information|full stack trace|See .*surefire|MojoFailure|^\[ERROR\] *$' | head -15)
|
||||
if [ -n "$FOUND" ]; then echo "$FOUND" >&2; else echo "$OUT" | tail -15 >&2; fi
|
||||
exit 2
|
||||
fi
|
||||
exit 0
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
paths:
|
||||
- "src/test/**/*.java"
|
||||
---
|
||||
|
||||
# Test rules
|
||||
- JUnit 6 and AssertJ. Static-import `assertThat`.
|
||||
- Name tests as sentences: `tenUnitsGetTenPercentOff`, not `testDiscount`.
|
||||
- Unit-test services without Spring. Use `@SpringBootTest` only for controller wiring.
|
||||
- One behaviour per test. No logic (loops, ifs) inside a test.
|
||||
@@ -0,0 +1,53 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/claude-code-settings.json",
|
||||
"permissions": {
|
||||
"allow": [
|
||||
"Bash(mvn -B -q test *)",
|
||||
"Bash(mvn -B -q test)",
|
||||
"Bash(mvn -B verify)",
|
||||
"Bash(git status)",
|
||||
"Bash(git diff *)"
|
||||
],
|
||||
"deny": [
|
||||
"Read(./.env)",
|
||||
"Read(./.env.*)",
|
||||
"Read(./src/main/resources/application-prod.properties)"
|
||||
]
|
||||
},
|
||||
"hooks": {
|
||||
"PreToolUse": [
|
||||
{
|
||||
"matcher": "Bash",
|
||||
"hooks": [
|
||||
{ "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/guard-bash.sh", "timeout": 10 }
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": "Edit|Write|MultiEdit",
|
||||
"hooks": [
|
||||
{ "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/protect-files.sh", "timeout": 10 }
|
||||
]
|
||||
}
|
||||
],
|
||||
"PostToolUse": [
|
||||
{
|
||||
"matcher": "Edit|Write|MultiEdit",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/test-after-edit.sh",
|
||||
"timeout": 180,
|
||||
"statusMessage": "Running the matching test class..."
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"Stop": [
|
||||
{
|
||||
"hooks": [
|
||||
{ "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/stop-gate.sh", "timeout": 300 }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
name: new-endpoint
|
||||
description: Adds a REST endpoint to the orders service the way this project does it (controller, service, tests). Use when asked to add or extend an endpoint.
|
||||
argument-hint: "[what the endpoint should do]"
|
||||
allowed-tools: Bash(mvn *)
|
||||
---
|
||||
|
||||
Add an endpoint for: $ARGUMENTS
|
||||
|
||||
Follow these steps in order.
|
||||
|
||||
1. Read `OrderController` and `OrderService` first and copy their style.
|
||||
2. Put business logic in `OrderService`, a unit test in `OrderServiceTest` first (red), then the code (green).
|
||||
3. Add the controller method and a MockMvc case in `OrderControllerTest`.
|
||||
4. Run `mvn -B -q test`. Do not report done until it is green.
|
||||
5. Reply with the new route, the status codes it returns, and the test names you added.
|
||||
@@ -0,0 +1,18 @@
|
||||
---
|
||||
name: review-diff
|
||||
description: Reviews the uncommitted changes for bugs, missing tests and Spring Boot 4 import mistakes. Use before committing.
|
||||
disable-model-invocation: true
|
||||
allowed-tools: Bash(git diff *) Bash(git status *)
|
||||
---
|
||||
|
||||
## Changes under review
|
||||
|
||||
!`git diff HEAD`
|
||||
|
||||
## What to check
|
||||
1. Behaviour changes without a matching test.
|
||||
2. Boot 3 imports that no longer exist in Boot 4 (see CLAUDE.md).
|
||||
3. `double` used for money, field injection, entities returned from controllers.
|
||||
4. Edits to files under `db/migration` that already exist.
|
||||
|
||||
Report findings as a list: file, line, problem, suggested fix. If there are none, say so in one line.
|
||||
Reference in New Issue
Block a user