feat(examples): add production-ready commands, hooks, and comprehensive documentation

Added 3 production slash commands:
- /pr: PR creation with complexity scoring and scope analysis
- /release-notes: Generate release notes in 3 formats with migration detection
- /sonarqube: Analyze SonarCloud quality issues for PRs

Added 2 production hooks:
- dangerous-actions-blocker.sh: PreToolUse security hook blocking destructive operations
- notification.sh: Contextual macOS alerts with sound mappings

Created comprehensive hooks documentation (examples/hooks/README.md)

Improved README discoverability:
- Moved "What's Inside" to line 24 for immediate visibility
- Added DeepWiki interactive documentation explorer section
- Added "Ready-to-Use Examples" section with command/hook tables

Extended guide documentation:
- Expanded bash mode (!) with 9 concrete examples
- Documented file references (@) with usage patterns
- Updated statistics: guide now 8,505 lines (+837 lines, +10.9%)

All templates are fully generic with no project-specific references.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Florian BRUNIAUX 2026-01-10 17:30:30 +01:00
parent 2dd2744721
commit 96f0435291
10 changed files with 1698 additions and 19 deletions

View file

@ -6,6 +6,101 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Added
- **Production-ready slash commands** in examples/commands/ (~25 KB)
- **pr.md** (5.8 KB) - PR creation with scope analysis
- Complexity scoring algorithm (code files × 2 + tests × 0.5 + directories × 3 + commits)
- Scope coherence detection (related vs unrelated changes)
- Semi-automatic split suggestions with git commands
- Conventional commit format enforcement
- Complete PR template with TLDR + description + test checklist
- **release-notes.md** (7.2 KB) - Generate release notes in 3 formats
- CHANGELOG.md format (Keep a Changelog standard)
- GitHub Release / PR body format
- User announcement format (tech-to-product language transformation)
- Database migration detection (Prisma, Sequelize, Django, Alembic)
- Semantic versioning determination from commit types
- **sonarqube.md** (11.3 KB) - Analyze SonarCloud quality issues for PRs
- Environment variable configuration ($SONARQUBE_TOKEN, $SONAR_PROJECT_KEY)
- Bash script wrapper to handle zsh authentication issues
- Node.js analysis script for grouping issues by rule and severity
- Executive summary with top violators and action plan
- Severity mapping (BLOCKER/CRITICAL → 🔴, MAJOR → 🟡, MINOR/INFO → 🔵)
- **Production-ready hooks** in examples/hooks/bash/ (~6.5 KB)
- **dangerous-actions-blocker.sh** (5.2 KB) - PreToolUse security hook
- Blocks destructive commands (rm -rf /, fork bombs, dd if=, mkfs)
- Blocks git force push to main/master branches
- Blocks npm/pnpm/yarn publish without confirmation
- Detects secrets in commands (password=, api_key=, token= patterns)
- Protects sensitive files (.env, credentials.json, SSH keys, .npmrc)
- Path validation with $ALLOWED_PATHS environment variable
- Generic implementation using $CLAUDE_PROJECT_DIR with fallback to pwd
- **notification.sh** (1.3 KB) - Notification hook with contextual macOS alerts
- 5 contextual sound mappings (success, error, waiting, warning, default)
- Keyword-based context detection (completed/done → Hero.aiff, error/failed → Basso.aiff)
- Non-blocking background execution
- Native macOS notifications with osascript
- Multi-language support (English/French keywords)
- **Comprehensive hooks documentation**
- **examples/hooks/README.md** (12.4 KB) - Complete hook system guide
- Available hooks table with 6 hook examples across events
- Hook events reference (PreToolUse, PostToolUse, UserPromptSubmit, Notification, SessionStart, SessionEnd, Stop)
- Configuration guide with settings.json examples and matcher patterns
- Creating custom hooks template with environment variables
- Best practices (short timeout, fail gracefully, minimal logging)
- Advanced examples (git context enrichment, activity logger, migration detector)
- Troubleshooting section (permission issues, timeout errors, jq installation)
- **README.md improvements** for better discoverability
- Moved "What's Inside" section to line 24 (immediately after intro, before "About This Guide")
- Added examples/ row to table: "Production-ready commands, hooks, agents | Browse as needed"
- **DeepWiki interactive documentation explorer** section
- Link to https://deepwiki.com/FlorianBruniaux/claude-code-ultimate-guide/1-overview
- 4 bullet points explaining features (natural language queries, contextual navigation, semantic search, on-demand summaries)
- Tagline: "Perfect for quick lookups when you don't want to read the full 7500+ lines"
- **Ready-to-Use Examples** section with comprehensive tables
- Commands table: 6 commands with purpose and highlights (/pr, /release-notes, /sonarqube, /commit, /review-pr, /git-worktree)
- Hooks table: 4 hooks with events and purposes (dangerous-actions-blocker, notification, security-check, auto-format)
- Link to examples/README.md for full catalog
- **Guide documentation extensions** (english-ultimate-claude-code-guide.md)
- **Section 1.3 "Quick Actions & Shortcuts"** expanded (~80 lines)
- New subsection "Shell Commands with `!`" with 9 concrete examples
- Quick status checks (!git status, !npm run test, !docker ps)
- View logs (!tail -f, !cat package.json)
- Quick searches (!grep -r "TODO", !find . -name "*.test.ts")
- Comparison table: when to use `!` vs asking Claude
- Example workflow showing both approaches
- New subsection "File References with `@`" with usage patterns
- Single file, multiple files, wildcards, relative paths
- "Why use `@`" section: precision, speed, context, clarity
- Comparative example showing with/without `@`
- Section 10 TL;DR updated with "Copy ready-to-use templates → examples/ directory"
- Appendix updated with note redirecting to examples/ for production-ready templates
### Changed
- **examples/README.md** updated with new entries
- Commands table: Added /pr, /release-notes, /sonarqube rows
- Hooks table: Added dangerous-actions-blocker.sh, notification.sh rows
- Added note: "See hooks/README.md for complete documentation"
- **README.md restructured** for immediate content comprehension
- "What's Inside" moved from line 72 to line 24 (48 lines higher)
- Removed duplicate "What's Inside" section (was at old location)
- Removed duplicate DeepWiki reference from Resources section
- Optimal information architecture: Title → Author → What's Inside → About
- **Guide statistics updated**
- Guide expanded from 7,668 to 8,505 lines (+837 lines, +10.9%)
- Word count updated to approximately 31,280 words
- Reading time remains 3 hours (comprehensive read-through)
### Stats
- 6 new files created (~43 KB total)
- 3 slash commands (pr.md, release-notes.md, sonarqube.md)
- 2 bash hooks (dangerous-actions-blocker.sh, notification.sh)
- 1 comprehensive documentation (hooks/README.md)
- 3 files modified (README.md, english-ultimate-claude-code-guide.md, examples/README.md)
- Guide grew by 837 lines (10.9% growth from v2.0.0)
- Focus on production-ready templates and improved documentation discoverability
- All commands and hooks fully generic (no project-specific references)
## [2.0.0] - 2026-01-10
### Added

View file

@ -21,6 +21,31 @@
---
## What's Inside
| File | Description | Reading Time |
|------|-------------|--------------|
| [`english-ultimate-claude-code-guide.md`](./english-ultimate-claude-code-guide.md) | Complete guide (7500+ lines, 27K+ words) | ~3 hours |
| [`cheatsheet-en.md`](./cheatsheet-en.md) | 1-page printable daily reference | 5 minutes |
| [`claude-setup-audit-prompt.md`](./claude-setup-audit-prompt.md) | Self-audit prompt for your setup | ~10 minutes |
| [`examples/`](./examples/) | Production-ready commands, hooks, agents | Browse as needed |
| [`claude-code-ultimate-guide_notebooklm.pdf`](./claude-code-ultimate-guide_notebooklm.pdf) | NotebookLM slides export | ~20 minutes |
| [`claude-code-ultimate-guide_kimi.pdf`](./claude-code-ultimate-guide_kimi.pdf) | PDF version (Kimi export) | ~3 hours |
### 🔍 Explore Interactively
**[DeepWiki: Interactive Documentation](https://deepwiki.com/FlorianBruniaux/claude-code-ultimate-guide/1-overview)**
Transform this repository into an interactive AI-powered documentation explorer:
- **Ask questions** in natural language about the guide
- **Navigate contextually** through interconnected concepts
- **Search semantically** beyond keyword matching
- **Get summaries** of specific sections on demand
Perfect for quick lookups when you don't want to read the full 7500+ lines.
---
## About This Guide
### Our Pedagogical Approach
@ -69,15 +94,31 @@ If you find value in this guide, have questions, or want to discuss Claude Code
---
## What's Inside
## Ready-to-Use Examples
| File | Description | Reading Time |
|------|-------------|--------------|
| [`english-ultimate-claude-code-guide.md`](./english-ultimate-claude-code-guide.md) | Complete guide (7500+ lines, 27K+ words) | ~3 hours |
| [`cheatsheet-en.md`](./cheatsheet-en.md) | 1-page printable daily reference | 5 minutes |
| [`claude-setup-audit-prompt.md`](./claude-setup-audit-prompt.md) | Self-audit prompt for your setup | ~10 minutes |
| [`claude-code-ultimate-guide_notebooklm.pdf`](./claude-code-ultimate-guide_notebooklm.pdf) | NotebookLM slides export | ~20 minutes |
| [`claude-code-ultimate-guide_kimi.pdf`](./claude-code-ultimate-guide_kimi.pdf) | PDF version (Kimi export) | ~3 hours |
The [`examples/`](./examples/) directory contains production-ready templates you can copy directly into your projects:
### 🎯 Commands (Slash Commands)
| Command | Purpose | Highlights |
|---------|---------|------------|
| [/pr](./examples/commands/pr.md) | Create PRs with scope analysis | Complexity scoring, auto-detect scope issues, split suggestions |
| [/release-notes](./examples/commands/release-notes.md) | Generate release notes (3 formats) | CHANGELOG + PR body + Slack announcement, migration detection |
| [/sonarqube](./examples/commands/sonarqube.md) | Analyze SonarCloud quality issues | Executive summary, top violators, action plan |
| [/commit](./examples/commands/commit.md) | Conventional commit messages | Follows team conventions, auto-formats |
| [/review-pr](./examples/commands/review-pr.md) | PR review workflow | Structured feedback, security checks |
| [/git-worktree](./examples/commands/git-worktree.md) | Isolated git worktree setup | Safe parallel development |
### 🛡️ Hooks (Event Automation)
| Hook | Event | Purpose |
|------|-------|---------|
| [dangerous-actions-blocker.sh](./examples/hooks/bash/dangerous-actions-blocker.sh) | PreToolUse | Block `rm -rf /`, force push, secrets, dangerous edits |
| [notification.sh](./examples/hooks/bash/notification.sh) | Notification | macOS sound alerts (success, error, warning) |
| [security-check.sh](./examples/hooks/bash/security-check.sh) | PreToolUse | Detect secrets in commands |
| [auto-format.sh](./examples/hooks/bash/auto-format.sh) | PostToolUse | Auto-format with Prettier |
> **📖 See [examples/README.md](./examples/README.md) for complete catalog including agents, skills, and config templates**
---
@ -235,9 +276,6 @@ Want to know if your Claude Code setup follows best practices?
## Resources
### Explore This Guide
- [DeepWiki](https://deepwiki.com/FlorianBruniaux/claude-code-ultimate-guide) — AI-powered exploration of this repository (ask questions, get summaries)
### Official
- [Claude Code Documentation](https://docs.anthropic.com/en/docs/claude-code) — Official Anthropic docs

View file

@ -292,15 +292,87 @@ These 7 commands cover 90% of daily usage:
| `/plan` | Enter Plan Mode | Safe exploration |
| `/rewind` | Undo changes | Made a mistake |
### Quick Actions
### Quick Actions & Shortcuts
| Shortcut | Action |
|----------|--------|
| `!command` | Run shell command directly |
| `@file.ts` | Reference a specific file |
| `Ctrl+C` | Cancel current operation |
| `Ctrl+R` | Retry last operation |
| `Esc` | Dismiss current suggestion |
| Shortcut | Action | Example |
|----------|--------|---------|
| `!command` | Run shell command directly | `!git status`, `!npm test` |
| `@file.ts` | Reference a specific file | `@src/app.tsx`, `@README.md` |
| `Ctrl+C` | Cancel current operation | Stop long-running analysis |
| `Ctrl+R` | Retry last operation | Retry after fixing error |
| `Esc` | Dismiss current suggestion | Skip unwanted changes |
#### Shell Commands with `!`
Execute commands immediately without asking Claude to do it:
```bash
# Quick status checks
!git status
!npm run test
!docker ps
# View logs
!tail -f logs/app.log
!cat package.json
# Quick searches
!grep -r "TODO" src/
!find . -name "*.test.ts"
```
**When to use `!` vs asking Claude**:
| Use `!` for... | Ask Claude for... |
|----------------|-------------------|
| Quick status checks (`!git status`) | Git operations requiring decisions |
| View commands (`!cat`, `!ls`) | File analysis and understanding |
| Already-known commands | Complex command construction |
| Fast iteration in terminal | Commands you're unsure about |
**Example workflow**:
```
You: !git status
Output: Shows 5 modified files
You: Create a commit with these changes, following conventional commits
Claude: [Analyzes files, suggests commit message]
```
#### File References with `@`
Reference specific files in your prompts for targeted operations:
```bash
# Single file
Review @src/auth/login.tsx for security issues
# Multiple files
Refactor @src/utils/validation.ts and @src/utils/helpers.ts to remove duplication
# With wildcards (in some contexts)
Analyze all test files @src/**/*.test.ts
# Relative paths work
Check @./CLAUDE.md for project conventions
```
**Why use `@`**:
- **Precision**: Target exact files instead of letting Claude search
- **Speed**: Skip file discovery phase
- **Context**: Claude loads file content automatically
- **Clarity**: Makes your intent explicit
**Example**:
```
# Without @
You: Fix the authentication bug
Claude: Which file contains the authentication logic? [Wastes time searching]
# With @
You: Fix the authentication bug in @src/auth/middleware.ts
Claude: [Immediately loads file and proposes fix]
```
## 1.4 Permission Modes
@ -6296,6 +6368,7 @@ _Quick jump:_ [Commands Table](#101-commands-table) · [Keyboard Shortcuts](#102
| Fix an error | [10.4 Troubleshooting](#104-troubleshooting) |
| Quick daily reference | [10.5 Cheatsheet](#105-cheatsheet) |
| Set up workflow | [10.6 Daily Workflow](#106-daily-workflow--checklists) |
| **Copy ready-to-use templates** | **[examples/ directory](../examples/)** — Commands, hooks, agents |
### Most Common Lookups:
- **Context full?** → [10.4.1 Context Issues](#context-issues)
@ -7141,6 +7214,8 @@ Use this before sending complex requests:
# Appendix: Templates Collection
> **💡 Production-Ready Examples**: For complete, battle-tested templates including advanced commands (`/pr`, `/release-notes`, `/sonarqube`) and security hooks, see the [`examples/`](../examples/) directory. The templates below are minimal starting points.
## A.1 Agent Template
```markdown

View file

@ -52,14 +52,22 @@ Ready-to-use templates for Claude Code configuration.
| File | Trigger | Purpose |
|------|---------|---------|
| [commit.md](./commands/commit.md) | `/commit` | Conventional commit messages |
| [pr.md](./commands/pr.md) | `/pr` | Create well-structured PRs with scope analysis |
| [review-pr.md](./commands/review-pr.md) | `/review-pr` | PR review workflow |
| [release-notes.md](./commands/release-notes.md) | `/release-notes` | Generate release notes in 3 formats |
| [sonarqube.md](./commands/sonarqube.md) | `/sonarqube` | Analyze SonarCloud quality issues for PRs |
| [generate-tests.md](./commands/generate-tests.md) | `/generate-tests` | Test generation |
| [git-worktree.md](./commands/git-worktree.md) | `/git-worktree` | Isolated git worktree setup |
### Hooks
| File | Event | Purpose |
|------|-------|---------|
| [dangerous-actions-blocker.sh](./hooks/bash/dangerous-actions-blocker.sh) | PreToolUse | Block dangerous commands/edits |
| [security-check.*](./hooks/) | PreToolUse | Block secrets in commands |
| [auto-format.*](./hooks/) | PostToolUse | Auto-format after edits |
| [notification.sh](./hooks/bash/notification.sh) | Notification | Contextual macOS sound alerts |
> **See [hooks/README.md](./hooks/README.md) for complete documentation and examples**
### Config
| File | Purpose |

195
examples/commands/pr.md Normal file
View file

@ -0,0 +1,195 @@
# Create Pull Request
Analyze changes, detect scope issues, and create a well-structured PR following project conventions.
## Process
1. **Analyze Changes**: Calculate complexity score from files, commits, and directories
2. **Detect Scope Issues**: Warn if PR is too large or mixes unrelated changes
3. **Suggest Split**: If needed, group commits by scope and propose separate PRs
4. **Collect Info**: Ask for type, target branch, draft status, labels
5. **Generate Content**: Create TLDR + description + checklist
6. **Create PR**: Execute `gh pr create` with proper formatting
7. **Remind Follow-up**: Display post-PR checklist (SonarQube, Claude Review)
## Complexity Score
Calculate PR complexity to detect if split is needed:
| Criterion | Weight | Description |
|-----------|--------|-------------|
| Code files | x2 | `*.ts, *.tsx` (excluding tests) |
| Test files | x0.5 | `*.test.ts, *.spec.ts` |
| Config files | x1 | `*.json, *.yml, *.md` |
| Directories | x3 | Distinct `src/*` directories |
| Commits | x1 | Number of commits |
**Thresholds**: 0-15 ✅ Normal | 16-25 ⚠️ Large | 26+ 🔴 Split recommended
## Scope Coherence
| Pattern | Verdict |
|---------|---------|
| Single scope | ✅ OK |
| Related scopes (sessions + calendar) | ✅ OK |
| Unrelated scopes (payments + auth) | 🔴 Split |
| feat + fix same scope | ✅ OK |
| feat + fix different scopes | 🔴 Split |
## Split Suggestion Format
When split is recommended, display:
```
🔴 Scope trop large (score: 32)
Commits par scope :
├── payments (5 commits, 8 fichiers)
│ ├── feat(payments): add Stripe checkout
│ └── fix(payments): handle currency
└── notifications (3 commits, 6 fichiers)
└── feat(notifications): add email templates
💡 Suggestion :
1. PR #1 : feature/payments-stripe → Commits payments
2. PR #2 : feature/notifications → Commits notifications
Options :
[A] Continuer avec une seule PR (non recommandé)
[B] Découper (semi-auto - commandes git fournies)
[C] Voir détail fichiers
```
**Semi-auto split** provides copy-paste commands:
```bash
git checkout develop
git checkout -b feature/payments-stripe
git cherry-pick abc1234 def5678
git push -u origin feature/payments-stripe
```
## Questions to Ask
1. **Type**: feature | fix | tech | docs | security
2. **Target Branch**: Show recent branches (develop, main, others)
3. **Draft**: Yes (WIP) | No (ready for review)
4. **Labels**: Based on type + optional (breaking-change, security)
## PR Title Format
```
<type>(<scope>): <description>
```
Examples:
- `feat(payments): add Stripe checkout integration`
- `fix(sessions): resolve timezone calculation bug`
## PR Body Template
```markdown
## TLDR
<!-- 2 lines max - Executive summary -->
---
## Type
{Feature | Fix | Tech | Docs | Security}
## Description
{Context and changes}
## Technical Changes
{List of main modifications}
## Tests
- [ ] Unit tests added/passing
- [ ] Manual testing completed
## Checklist
- [ ] Code follows conventions
- [ ] No console.log left
- [ ] Types OK (`pnpm typecheck`)
---
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
```
## Available Labels
| Label | Color | Use When |
|-------|-------|----------|
| `feature` | 🟢 | New functionality |
| `fix` | 🔴 | Bug fix |
| `tech` | 🔵 | Refactoring, tech debt |
| `docs` | 📘 | Documentation only |
| `security` | 🟣 | Security fix |
| `breaking-change` | ⚫ | Breaking changes |
| `WIP` | 🟡 | Work in progress (draft) |
## Commands to Execute
```bash
# 1. Get base branch (usually develop)
BASE_BRANCH="develop"
# 2. Calculate complexity score
CODE=$(git diff --name-only $BASE_BRANCH..HEAD | grep -E '\.(ts|tsx)$' | grep -v test | wc -l)
TESTS=$(git diff --name-only $BASE_BRANCH..HEAD | grep -E '\.test\.|\.spec\.' | wc -l)
DIRS=$(git diff --name-only $BASE_BRANCH..HEAD | cut -d'/' -f1-2 | sort -u | wc -l)
COMMITS=$(git rev-list --count $BASE_BRANCH..HEAD)
SCORE=$((CODE * 2 + TESTS / 2 + DIRS * 3 + COMMITS))
# 3. Get scopes from commits
git log --oneline $BASE_BRANCH..HEAD --format="%s" | sed -n 's/^\w*(\([^)]*\)).*/\1/p' | sort | uniq -c
# 4. Recent branches for selection
git branch --sort=-committerdate --format='%(refname:short)' | head -5
# 5. Create PR
gh pr create \
--title "<type>(<scope>): <description>" \
--body "$BODY" \
--base $BASE_BRANCH \
--label "<label>" \
--draft # if WIP
```
## Post-PR Output
After PR creation, ALWAYS display:
```
✅ PR créée : https://github.com/org/repo/pull/XXX
📋 Prochaines étapes automatiques :
• SonarQube analysera la qualité du code (bugs, vulnérabilités, code smells)
• Claude Code Review fournira un feedback IA sur votre PR
⏳ Pensez à surveiller ces analyses dans les prochaines minutes.
Si des problèmes sont détectés, corrigez-les avant de demander une review humaine.
```
## Edge Cases
| Situation | Behavior |
|-----------|----------|
| No scope in commits | Analyze by directories |
| Non-conventional commits | Warn + ask type manually |
| No commits (same as base) | Error: "Aucun changement" |
| Single commit | Use commit message as title |
| Merge commits | Ignore (`--no-merges`) |
## Usage
```
/pr
/pr --base main
/pr --draft
```
Target: $ARGUMENTS (optional: --base, --draft)

View file

@ -0,0 +1,239 @@
# Release Notes Generator
Generate release notes in 3 formats from git commits for production releases.
## Process
1. **Analyze Git History**: Scan commits since last release tag
2. **Fetch PR Details**: Get titles, descriptions via `gh api`
3. **Categorize Changes**: Group by type (feat, fix, perf, etc.)
4. **Check Migrations**: Detect database migration files
5. **Generate 3 Outputs**: CHANGELOG, PR body, communication message
6. **Transform Language**: Convert tech jargon to product language
## Output Formats
### 1. CHANGELOG.md Section
```markdown
## [X.Y.Z] - YYYY-MM-DD
### Summary
[1-2 sentence overview of this release]
### New Features
#### [Feature Name] (#PR)
- **Description**: User-facing functionality added
- **Impact**: How it benefits users
### Bug Fixes
- **[Module]**: Description (#issue, tracking-ID)
### Technical Improvements
- [Internal improvements, refactoring, performance]
### Database Migrations
[If applicable - list migration files]
### Statistics
- PRs: X | Features: Y | Fixes: Z | Files changed: N
```
### 2. PR Release Body
Uses your project's release template:
- `.github/PULL_REQUEST_TEMPLATE/release.md`
- `.github/pull_request_template_release.md`
- Or custom location specified in project config
### 3. Communication Announcement
Generate user-facing announcement (Slack, email, etc.):
- Non-technical language
- Focus on user impact
- Readable formatting (emojis optional)
Template location examples:
- `.github/COMMUNICATION_TEMPLATE/slack-release.md`
- `docs/templates/release-announcement.md`
## Migration Alert
**If migrations detected:**
```
╔══════════════════════════════════════════════════════════════════╗
║ ⚠️ [ATTENTION] DATABASE MIGRATIONS REQUIRED ║
╠══════════════════════════════════════════════════════════════════╣
║ This release contains X migration(s): ║
║ • 20250110_add_user_preferences ║
║ • 20250112_create_audit_log_table ║
║ Action required: Run migration command after deployment ║
╚══════════════════════════════════════════════════════════════════╝
```
**If no migrations:**
```
✅ [OK] No database migrations required
```
## Tech-to-Product Transformation
Convert technical commits to user-friendly descriptions:
| Technical | Product/User Language |
|-----------|----------------------|
| "Optimize N+1 queries with DataLoader" | "Faster loading times for lists" |
| "Implement AI embeddings with pgvector" | "New intelligent search feature" |
| "Fix permissions scope bug" | "Resolved access issue for certain users" |
| "Migration webpack -> Turbopack" | *Internal only - don't communicate* |
| "Refactor React hooks architecture" | *Internal only - don't communicate* |
| "Add rate limiting to API endpoints" | "Improved system stability and security" |
## Commit Categories
| Prefix | Category | Include in Announcement? |
|--------|----------|--------------------------|
| `feat:` | New Features | Yes |
| `fix:` | Bug Fixes | Yes (if user-facing) |
| `perf:` | Performance | Yes (simplified) |
| `security:` | Security | Yes |
| `refactor:` | Architecture | No |
| `chore:` | Maintenance | No |
| `docs:` | Documentation | No |
| `test:` | Tests | No |
| `build:` | Build System | No |
| `ci:` | CI/CD | No |
## Commands to Execute
```bash
# 1. Get last release tag
LAST_TAG=$(git tag --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1)
# 2. List commits since tag (excluding merges)
git log $LAST_TAG..HEAD --oneline --no-merges
# 3. Get commit details with PR numbers
git log $LAST_TAG..HEAD --format="%h %s" --no-merges
# 4. Check for migrations (adjust path to your ORM)
# Prisma:
git diff $LAST_TAG..HEAD --name-only -- prisma/migrations/
# Sequelize:
git diff $LAST_TAG..HEAD --name-only -- migrations/
# Django:
git diff $LAST_TAG..HEAD --name-only -- '**/migrations/*.py'
# Alembic:
git diff $LAST_TAG..HEAD --name-only -- alembic/versions/
# 5. Get PR details via GitHub CLI
gh api repos/{owner}/{repo}/pulls/{number}
# 6. Count statistics
TOTAL_PRS=$(git log $LAST_TAG..HEAD --oneline --merges | wc -l)
FEATURES=$(git log $LAST_TAG..HEAD --oneline --no-merges | grep -c 'feat:')
FIXES=$(git log $LAST_TAG..HEAD --oneline --no-merges | grep -c 'fix:')
```
## Semantic Versioning
Determine version number based on changes:
| Change Type | Version Bump | Example |
|-------------|--------------|---------|
| Breaking change | MAJOR (X.0.0) | API removed, incompatible change |
| New feature | MINOR (0.X.0) | New functionality, backward-compatible |
| Bug fix / patch | PATCH (0.0.X) | Bug fixes only |
**Indicators**:
- `BREAKING CHANGE:` in commit body → MAJOR
- `feat:` commits present → MINOR
- Only `fix:` / `perf:` → PATCH
## Workflow Integration
Typical release workflow:
```
1. Verify all PRs merged to develop branch
2. Run: /release-notes (or specify version/range)
3. Review generated outputs for accuracy
4. Create PR: develop -> main with "release" label
5. Add generated CHANGELOG section to CHANGELOG.md
6. Use generated PR body as PR description
7. After merge: Create and push git tag
8. Post communication announcement (Slack/email/etc.)
9. Monitor deployment and migrations
```
## Git Tag Creation
After PR merge, create annotated tag:
```bash
# Create annotated tag
git tag -a v1.2.3 -m "Release v1.2.3: Brief description"
# Push tag to remote
git push origin v1.2.3
# Or push all tags
git push --tags
```
## Project-Specific Customization
Adapt these paths to your project:
```
# Migration detection (adjust ORM path)
prisma/migrations/ → Your ORM migration directory
db/migrate/ → Rails migrations
alembic/versions/ → Alembic migrations
# Template files (create if needed)
.github/PULL_REQUEST_TEMPLATE/release.md
.github/COMMUNICATION_TEMPLATE/announcement.md
docs/templates/release-notes.md
```
## Tips
- **Run from repository root**: Ensures git commands work correctly
- **Authenticate GitHub CLI**: Run `gh auth login` if needed
- **Review before publishing**: Always verify generated content
- **Breaking changes**: Search commit messages for `BREAKING CHANGE:`
- **Linked issues**: Include issue/ticket numbers for traceability
- **Database migrations**: Test in staging before production
## Edge Cases
| Scenario | Behavior |
|----------|----------|
| No tags found | Start from first commit |
| No commits since last tag | Error: "No changes to release" |
| Multiple tags on same commit | Use most recent by date |
| Pre-release tags (v1.0.0-beta.1) | Exclude from "last release" search |
| Commits without conventional format | Categorize as "Other Changes" |
## Usage Examples
```bash
# Generate release notes from last tag to HEAD
/release-notes
# Specify version manually
/release-notes v1.5.0
# Specify range
/release-notes from v1.4.0 to HEAD
# Preview without creating files
/release-notes --preview
# Include pre-release commits
/release-notes --include-pre-release
```
Version/Range: $ARGUMENTS

View file

@ -0,0 +1,367 @@
# SonarQube Analysis
Analyze SonarCloud quality issues for a specific PR. Generates comprehensive report with metrics, top violators, and action plan.
**Core principle:** Analysis-only = no code changes, pure insight.
## Process
1. **Verify Token**: Check `$SONARQUBE_TOKEN` environment variable
2. **Fetch Issues**: Call SonarCloud API for PR issues
3. **Parse Data**: Group by severity, type, file, rule
4. **Generate Report**: Structured output with action plan
5. **Cleanup**: Remove temporary files
## Prerequisites
### Environment Variable
```bash
# Set SonarQube token (add to ~/.bashrc or ~/.zshrc)
export SONARQUBE_TOKEN="your_token_here"
# Verify token is set
echo $SONARQUBE_TOKEN
```
**To get token:**
1. Go to SonarCloud → My Account → Security
2. Generate new token
3. Copy and export as environment variable
### Project Configuration
Configure your SonarCloud project details:
```bash
# Add to project's CLAUDE.md or as environment variables
SONAR_ORGANIZATION="your-org-name"
SONAR_PROJECT_KEY="your-org_your-project"
SONAR_BASE_URL="https://sonarcloud.io/api"
```
**If not set:** Ask user to provide organization and project key.
## Fetch Issues
**Important:** Direct curl with `-u "$SONARQUBE_TOKEN:"` fails in zsh due to authentication parsing. Use bash script wrapper:
```bash
# Create temporary bash script to handle authentication
cat > /tmp/fetch_sonar.sh << 'SCRIPT'
#!/bin/bash
curl -s -u "${SONARQUBE_TOKEN}:" \
"https://sonarcloud.io/api/issues/search?componentKeys=${SONAR_PROJECT_KEY}&pullRequest=$1&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true&ps=500"
SCRIPT
chmod +x /tmp/fetch_sonar.sh
/tmp/fetch_sonar.sh $PR_NUMBER > /tmp/sonar_pr_$PR_NUMBER.json
```
**API Parameters:**
- `componentKeys`: Your project key
- `pullRequest`: PR number
- `issueStatuses`: OPEN,CONFIRMED (exclude resolved)
- `sinceLeakPeriod`: Only new issues in this PR
- `ps`: Page size (max 500)
## Analysis Script
Create Node.js analysis script at `/tmp/sonar_analyze.js`:
```javascript
const fs = require('fs');
const prNumber = process.argv[2];
const data = JSON.parse(fs.readFileSync(`/tmp/sonar_pr_${prNumber}.json`, 'utf8'));
const issues = data.issues || [];
// Group by severity
const bySeverity = issues.reduce((acc, i) => {
acc[i.severity] = (acc[i.severity] || 0) + 1;
return acc;
}, {});
// Group by type
const byType = issues.reduce((acc, i) => {
acc[i.type] = (acc[i.type] || 0) + 1;
return acc;
}, {});
// Group by file
const byFile = issues.reduce((acc, i) => {
const file = i.component.split(':')[1] || i.component;
acc[file] = (acc[file] || 0) + 1;
return acc;
}, {});
// Group by rule
const byRule = issues.reduce((acc, i) => {
if (!acc[i.rule]) {
acc[i.rule] = {
count: 0,
severity: i.severity,
message: i.message
};
}
acc[i.rule].count++;
return acc;
}, {});
// Output structured data
console.log(JSON.stringify({
total: data.total,
bySeverity,
byType,
topFiles: Object.entries(byFile)
.sort((a, b) => b[1] - a[1])
.slice(0, 10),
topRules: Object.entries(byRule)
.map(([rule, d]) => ({ rule, ...d }))
.sort((a, b) => b.count - a.count)
.slice(0, 5)
}, null, 2));
```
**Run analysis:**
```bash
node /tmp/sonar_analyze.js $PR_NUMBER > /tmp/sonar_analysis_$PR_NUMBER.json
```
## Report Format
Generate formatted report from analysis:
```
📊 SonarCloud Analysis - PR #XXX
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📈 EXECUTIVE SUMMARY
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Total Issues: {TOTAL}
By Severity:
🔴 Blocker/Critical: {COUNT} ({PERCENTAGE}%)
🟡 Major: {COUNT} ({PERCENTAGE}%)
🔵 Minor/Info: {COUNT} ({PERCENTAGE}%)
By Type:
🐛 Bugs: {COUNT}
🛡️ Vulnerabilities: {COUNT}
🧹 Code Smells: {COUNT}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📂 TOP 10 FILES WITH ISSUES
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1. src/components/UserProfile.tsx - 8 issues
2. src/services/auth.service.ts - 5 issues
3. src/utils/validation.ts - 4 issues
...
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠️ TOP 5 VIOLATED RULES
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1. typescript:S1854 (MAJOR) - 12 occurrences
"Dead stores should be removed"
2. typescript:S3776 (CRITICAL) - 8 occurrences
"Cognitive Complexity of functions should not be too high"
3. typescript:S1186 (MINOR) - 6 occurrences
"Functions should not be empty"
...
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✅ ACTION PLAN
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Priority 1 - CRITICAL/BLOCKER ({COUNT} issues):
• Fix immediately before merge
• Focus on: {TOP_FILES}
Priority 2 - MAJOR ({COUNT} issues):
• Address in this PR if possible
• Consider technical debt ticket if extensive
Priority 3 - MINOR/INFO ({COUNT} issues):
• Can be addressed in follow-up PR
• Add to backlog for refactoring sprint
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔗 LINKS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
View in SonarCloud:
https://sonarcloud.io/project/pull_requests_list?id={PROJECT_KEY}&pullRequest={PR_NUMBER}
```
## Severity Mapping
| SonarCloud | Symbol | Priority | Action |
|------------|--------|----------|--------|
| BLOCKER | 🔴 | P0 | Fix immediately |
| CRITICAL | 🔴 | P0 | Fix immediately |
| MAJOR | 🟡 | P1 | Fix in this PR |
| MINOR | 🔵 | P2 | Consider for follow-up |
| INFO | 🔵 | P3 | Optional improvement |
## Issue Types
| Type | Symbol | Description |
|------|--------|-------------|
| BUG | 🐛 | Code that is demonstrably wrong |
| VULNERABILITY | 🛡️ | Security issues |
| CODE_SMELL | 🧹 | Maintainability issue |
| SECURITY_HOTSPOT | 🔒 | Security-sensitive code to review |
## Cleanup
Always clean up temporary files after execution:
```bash
rm -f /tmp/fetch_sonar.sh
rm -f /tmp/sonar_pr_$PR_NUMBER.json
rm -f /tmp/sonar_analyze.js
rm -f /tmp/sonar_analysis_$PR_NUMBER.json
```
## Error Handling
| Error | Cause | Action |
|-------|-------|--------|
| Token not set | `$SONARQUBE_TOKEN` missing | Ask user to export token |
| 401 Unauthorized | Invalid or expired token | Request new token from SonarCloud |
| 404 Not Found | PR doesn't exist in SonarCloud | Verify PR number and project key |
| Empty response | No issues found | Report clean PR, congratulate team |
| >500 issues | Pagination limit reached | Warn about incomplete data, suggest filtering |
| Network error | API unreachable | Check internet connection, retry |
## Configuration Options
### Project-Level Configuration
Create `.sonarcloud.properties` or add to `CLAUDE.md`:
```properties
# SonarCloud Configuration
SONAR_ORGANIZATION=your-org
SONAR_PROJECT_KEY=your-org_your-project
SONAR_EXCLUSIONS=**/*.test.ts,**/*.spec.ts,**/migrations/**
SONAR_COVERAGE_EXCLUSIONS=**/*.test.ts,src/test/**
```
### API Rate Limits
SonarCloud API limits:
- Free tier: 10,000 requests/day
- Paid tier: Unlimited
**Tip:** Cache results for repeated queries to same PR.
## Integration Examples
### GitHub Actions
```yaml
- name: SonarQube Analysis
run: |
export SONARQUBE_TOKEN=${{ secrets.SONAR_TOKEN }}
export SONAR_PROJECT_KEY="${{ secrets.SONAR_PROJECT }}"
claude -p "/sonarqube ${{ github.event.pull_request.number }}"
```
### Pre-merge Hook
Add to `.claude/hooks/pre-merge.sh`:
```bash
#!/bin/bash
PR_NUMBER=$(gh pr view --json number -q .number)
claude -p "/sonarqube $PR_NUMBER"
```
## Red Flags - NEVER Do
**Never:**
- ❌ Modify code or auto-fix issues (analysis-only command)
- ❌ Skip token verification (security risk)
- ❌ Leave temp files in `/tmp` (cleanup required)
- ❌ Commit SonarQube token to repository (use env vars)
- ❌ Run without checking token expiration
**Always:**
- ✅ Generate structured, actionable report
- ✅ Clean up after execution
- ✅ Handle API errors gracefully
- ✅ Verify token is valid before API calls
- ✅ Parse and present data clearly
## Advanced Usage
### Custom Filters
```bash
# Only show critical/blocker issues
/sonarqube 123 --severity BLOCKER,CRITICAL
# Only show bugs and vulnerabilities
/sonarqube 123 --types BUG,VULNERABILITY
# Specific file pattern
/sonarqube 123 --files "src/services/**"
```
### Multiple PRs
```bash
# Compare issues across PRs
/sonarqube 123,124,125 --compare
```
## Troubleshooting
### Issue: "curl: (22) The requested URL returned error: 401"
**Cause:** Invalid or missing token
**Fix:**
```bash
# Regenerate token in SonarCloud
# Export new token
export SONARQUBE_TOKEN="new_token_here"
```
### Issue: "Empty response or no issues"
**Cause:** Analysis not yet complete or PR not analyzed
**Fix:** Wait for SonarCloud analysis to complete (~2-5 minutes after PR creation)
### Issue: "componentKeys not found"
**Cause:** Wrong project key
**Fix:** Verify project key in SonarCloud URL:
```
https://sonarcloud.io/project/overview?id=YOUR_PROJECT_KEY
```
## Usage Examples
```bash
# Basic usage
/sonarqube 170
# With PR prefix
/sonarqube PR #234
# Using PR URL
/sonarqube https://github.com/org/repo/pull/170
# Custom severity filter (if implemented)
/sonarqube 170 --critical-only
```
PR Number: $ARGUMENTS

449
examples/hooks/README.md Normal file
View file

@ -0,0 +1,449 @@
# Claude Code Hooks
Hooks are scripts that execute automatically on Claude Code events. They enable automation, block dangerous operations, and enrich context.
## Available Hooks
| Hook | Event | Purpose | Platform |
|------|-------|---------|----------|
| [dangerous-actions-blocker.sh](./bash/dangerous-actions-blocker.sh) | PreToolUse | Block dangerous commands/edits | Bash |
| [security-check.sh](./bash/security-check.sh) | PreToolUse | Block secrets in commands | Bash |
| [auto-format.sh](./bash/auto-format.sh) | PostToolUse | Auto-format after edits | Bash |
| [notification.sh](./bash/notification.sh) | Notification | Contextual macOS sound alerts | Bash (macOS) |
| [security-check.ps1](./powershell/security-check.ps1) | PreToolUse | Block secrets in commands | PowerShell |
| [auto-format.ps1](./powershell/auto-format.ps1) | PostToolUse | Auto-format after edits | PowerShell |
## Hook Events
| Event | When | Typical Use Cases |
|-------|------|-------------------|
| `PreToolUse` | Before a tool executes | Validation, blocking dangerous operations |
| `PostToolUse` | After a tool executes | Formatting, logging, cleanup |
| `UserPromptSubmit` | When user sends a message | Context enrichment, preprocessing |
| `Notification` | When Claude sends a notification | Sound alerts, external notifications |
| `SessionStart` | At session start | Initialization, environment setup |
| `SessionEnd` | At session end | Cleanup, session summary |
| `Stop` | User interrupts operation | State saving, graceful shutdown |
## Security Hooks
### dangerous-actions-blocker.sh
**Event**: `PreToolUse` (Bash, Edit, Write)
Comprehensive protection against dangerous operations:
**Bash - Blocked Commands**:
- System destruction: `rm -rf /`, `rm -rf ~`, `sudo rm`
- Disk operations: `dd if=`, `mkfs`, `> /dev/sda`
- Fork bombs: `:(){:|:&};:`
- Database drops: `DROP DATABASE`, `DROP TABLE`
- Force pushes: `git push --force main/master`
- Package publishing: `npm publish`, `pnpm publish`
- Secret patterns: `password=`, `api_key=`, `token=`
**Edit/Write - Protected Files**:
- Environment: `.env`, `.env.local`, `.env.production`
- Credentials: `credentials.json`, `serviceAccountKey.json`
- SSH keys: `id_rsa`, `id_ed25519`, `id_ecdsa`
- Config: `.npmrc`, `.pypirc`, `secrets.yml`
**Edit/Write - Allowed Paths**:
- `$CLAUDE_PROJECT_DIR` (current project)
- `~/.claude/` (Claude config)
- `/tmp/` (temporary files)
- Additional paths via `$ALLOWED_PATHS` environment variable
**Exit Codes**:
```bash
exit 0 # Allow operation
exit 2 # Block (stderr message shown to Claude)
```
**Configuration**:
```bash
# Add custom allowed paths (colon-separated)
export ALLOWED_PATHS="/custom/path:/another/path"
```
### security-check.sh
**Event**: `PreToolUse` (Bash)
Focused on detecting secrets in commands:
- Password patterns
- API keys (common formats like `sk-xxx`, `pk-xxx`)
- AWS credentials
- Private keys
- Hardcoded tokens
## Productivity Hooks
### auto-format.sh / auto-format.ps1
**Event**: `PostToolUse` (Edit, Write)
Automatically format files after editing:
| Extension | Formatter |
|-----------|-----------|
| `.ts`, `.tsx`, `.js`, `.jsx` | Prettier |
| `.json`, `.css`, `.scss`, `.md` | Prettier |
| `.prisma` | `prisma format` |
| `.py` | Black / autopep8 |
| `.go` | `go fmt` |
**Silent Operation**: No output, failures ignored to avoid blocking Claude.
**Requirements**: Install formatters in your project:
```bash
# Node.js projects
npm install -D prettier
# Python projects
pip install black
# Go projects (built-in)
go fmt
```
### notification.sh
**Event**: `Notification` (macOS only)
Contextual sound alerts based on notification content:
| Context | Sound | Triggered By |
|---------|-------|--------------|
| Success | Hero.aiff | "completed", "done", "success" |
| Error | Basso.aiff | "error", "failed", "problem" |
| Waiting | Submarine.aiff | "waiting", "permission", "input" |
| Warning | Sosumi.aiff | "warning", "attention", "alert" |
| Default | Ping.aiff | Other notifications |
**Features**:
- Non-blocking (plays in background)
- Native macOS notifications
- Automatic context detection via keywords
- Multi-language support (English/French)
**Requirements**: macOS with `afplay` and `osascript` (built-in)
## Configuration
Hooks are configured in `.claude/settings.json`:
```json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash|Edit|Write",
"hooks": [{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/dangerous-actions-blocker.sh",
"timeout": 5000
}]
}
],
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/auto-format.sh",
"timeout": 10000
}]
}
],
"Notification": [
{
"hooks": [{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/notification.sh",
"timeout": 5000
}]
}
]
}
}
```
**Matcher Patterns**:
- `".*"` - Match all tools
- `"Bash"` - Match only Bash tool
- `"Edit|Write"` - Match Edit OR Write tools
- `"Bash|Edit|Write"` - Match multiple tools
## Creating Custom Hooks
### Basic Template
```bash
#!/bin/bash
# Hook: [EventType] - Description
# Exit 0 = success/allow, Exit 2 = block (PreToolUse only)
set -e
# Read JSON from stdin
INPUT=$(cat)
# Extract data
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name // empty')
TOOL_INPUT=$(echo "$INPUT" | jq -r '.tool_input // empty')
# Your logic here...
# Return optional JSON
cat << EOF
{
"systemMessage": "Message displayed to Claude",
"hookSpecificOutput": {
"additionalContext": "Extra context added"
}
}
EOF
exit 0
```
### Environment Variables
Available in hook scripts:
| Variable | Description |
|----------|-------------|
| `CLAUDE_PROJECT_DIR` | Current project path |
| `CLAUDE_FILE_PATHS` | Files passed with `-f` flag |
| `CLAUDE_TOOL_INPUT` | Tool input as JSON |
| `HOME` | User home directory |
### Best Practices
1. **Short Timeout**: Max 5-10s to avoid blocking Claude
2. **Fail Gracefully**: Use `|| true` for non-critical operations
3. **Minimal Logging**: Avoid stdout except structured JSON
4. **Require jq**: Parse JSON with `jq` for reliability
5. **Test Thoroughly**: Test with various inputs before deploying
6. **Document Behavior**: Clear comments on what hook does
7. **Handle Errors**: Proper error messages for debugging
### Example: Git Context Enrichment
Create `git-context.sh` (UserPromptSubmit event):
```bash
#!/bin/bash
# Hook: UserPromptSubmit - Add Git context to prompts
set -e
INPUT=$(cat)
# Get Git information
BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
LAST_COMMIT=$(git log -1 --oneline 2>/dev/null || echo "none")
UNCOMMITTED=$(git status --short 2>/dev/null | wc -l | tr -d ' ')
STAGED=$(git diff --cached --name-only 2>/dev/null | wc -l | tr -d ' ')
# Return enriched context
cat << EOF
{
"systemMessage": "[Git] Branch: $BRANCH | Last: $LAST_COMMIT | Uncommitted: $UNCOMMITTED files | Staged: $STAGED files"
}
EOF
exit 0
```
Register in settings:
```json
{
"hooks": {
"UserPromptSubmit": [
{
"hooks": [{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/git-context.sh",
"timeout": 3000
}]
}
]
}
}
```
## Installation
### Project-Level (Shared with Team)
1. Create hooks directory:
```bash
mkdir -p .claude/hooks
```
2. Copy hook from examples:
```bash
cp /path/to/examples/hooks/bash/dangerous-actions-blocker.sh .claude/hooks/
chmod +x .claude/hooks/*.sh
```
3. Configure in `.claude/settings.json` (see Configuration section above)
4. Commit to repository:
```bash
git add .claude/hooks/ .claude/settings.json
git commit -m "Add Claude Code hooks"
```
### Personal/Global (Your Machine Only)
1. Create global hooks directory:
```bash
mkdir -p ~/.claude/hooks
```
2. Copy hook:
```bash
cp /path/to/examples/hooks/bash/notification.sh ~/.claude/hooks/
chmod +x ~/.claude/hooks/*.sh
```
3. Configure in `~/.claude/settings.json`
**Priority**: Project hooks override global hooks.
## Platform-Specific Notes
### macOS / Linux (Bash)
- Use `.sh` extension
- Requires `chmod +x` for execution
- Path separator: `/`
- Home directory: `~` or `$HOME`
### Windows (PowerShell)
- Use `.ps1` extension
- May require execution policy: `Set-ExecutionPolicy RemoteSigned`
- Path separator: `\`
- Home directory: `$env:USERPROFILE`
## Troubleshooting
### Hook Not Executing
**Cause**: Not registered in settings.json or wrong path
**Fix**: Verify configuration and use absolute paths or `$CLAUDE_PROJECT_DIR`
### Permission Denied
**Cause**: Hook not executable
**Fix**:
```bash
chmod +x .claude/hooks/*.sh
```
### Hook Blocks Everything
**Cause**: Exit code 2 without conditions
**Fix**: Check logic, ensure `exit 0` is default case
### Timeout Errors
**Cause**: Hook takes too long (>timeout value)
**Fix**: Optimize hook performance or increase timeout in settings
### jq Not Found
**Cause**: `jq` not installed
**Fix**: Install jq:
```bash
# macOS
brew install jq
# Ubuntu/Debian
sudo apt-get install jq
# Windows
choco install jq
```
## Advanced Examples
### Activity Logger
Log all Claude operations to JSONL file:
```bash
#!/bin/bash
# PostToolUse - Log all activities
set -e
INPUT=$(cat)
TOOL=$(echo "$INPUT" | jq -r '.tool_name')
LOG_FILE="$HOME/.claude/logs/activity-$(date +%Y-%m-%d).jsonl"
mkdir -p "$(dirname "$LOG_FILE")"
# Create log entry
cat << EOF >> "$LOG_FILE"
{"timestamp":"$(date -u +%Y-%m-%dT%H:%M:%SZ)","tool":"$TOOL","session":"$CLAUDE_SESSION_ID"}
EOF
exit 0
```
### Database Migration Detector
Alert when migrations are created:
```bash
#!/bin/bash
# PostToolUse - Detect database migrations
set -e
INPUT=$(cat)
TOOL=$(echo "$INPUT" | jq -r '.tool_name')
if [[ "$TOOL" == "Write" ]]; then
FILE=$(echo "$INPUT" | jq -r '.tool_input.file_path')
if [[ "$FILE" == *"/migrations/"* ]]; then
cat << EOF
{
"systemMessage": "⚠️ Database migration created: $FILE\n\nReminder:\n1. Review migration carefully\n2. Test on dev database first\n3. Run 'prisma migrate deploy' after merge"
}
EOF
fi
fi
exit 0
```
## Security Considerations
1. **Never Store Secrets in Hooks**: Use environment variables
2. **Validate Input**: Always sanitize data from stdin
3. **Limit Hook Scope**: Use specific matchers, not `".*"`
4. **Review Blocked Operations**: Log when hooks block actions
5. **Test in Isolation**: Test hooks outside Claude first
6. **Version Control**: Commit hooks to repository for team sharing
## Resources
- [Main Guide - Section 7: Hooks](../../english-ultimate-claude-code-guide.md#7-hooks)
- [Claude Code Documentation](https://docs.anthropic.com/en/docs/claude-code)
- [Hook Event Reference](../../english-ultimate-claude-code-guide.md#71-hook-events)
---
*See the [main guide](../../english-ultimate-claude-code-guide.md) for detailed explanations and advanced patterns.*

View file

@ -0,0 +1,151 @@
#!/bin/bash
# Hook: PreToolUse - Block dangerous actions
# Exit 0 = allow, Exit 2 = block (stderr message shown to Claude)
#
# Place in: .claude/hooks/dangerous-actions-blocker.sh
# Register in: .claude/settings.json under PreToolUse event
set -e
# Read JSON from stdin
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name // empty')
TOOL_INPUT=$(echo "$INPUT" | jq -r '.tool_input // empty')
# === BASH: Dangerous commands ===
if [[ "$TOOL_NAME" == "Bash" ]]; then
COMMAND=$(echo "$TOOL_INPUT" | jq -r '.command // empty')
# Dangerous patterns
DANGEROUS_PATTERNS=(
"rm -rf /"
"rm -rf ~"
"rm -rf \$HOME"
"dd if="
"mkfs"
":(){:|:&};:" # Fork bomb
"> /dev/sda"
"chmod -R 777 /"
"chown -R"
"sudo rm"
"DROP DATABASE"
"DROP TABLE"
"--no-preserve-root"
)
for pattern in "${DANGEROUS_PATTERNS[@]}"; do
if [[ "$COMMAND" == *"$pattern"* ]]; then
echo "BLOCKED: Dangerous command detected: '$pattern'" >&2
exit 2
fi
done
# Block force push to main/master
if echo "$COMMAND" | grep -qE "git push.*(-f|--force).*(main|master)"; then
echo "BLOCKED: Force push to main/master is forbidden" >&2
exit 2
fi
# Block npm publish without confirmation
if echo "$COMMAND" | grep -qE "npm publish|pnpm publish|yarn publish"; then
echo "BLOCKED: Package publication requires manual confirmation" >&2
exit 2
fi
# Check for potential secrets in command
SECRET_PATTERNS=(
"password="
"secret="
"api_key="
"apikey="
"token="
"aws_access_key"
"aws_secret"
"private_key"
)
for pattern in "${SECRET_PATTERNS[@]}"; do
if echo "$COMMAND" | grep -qi "$pattern"; then
echo "BLOCKED: Potential secret detected in command: '$pattern'" >&2
exit 2
fi
done
fi
# === EDIT/WRITE: Sensitive files ===
if [[ "$TOOL_NAME" == "Edit" || "$TOOL_NAME" == "Write" ]]; then
FILE_PATH=$(echo "$TOOL_INPUT" | jq -r '.file_path // empty')
# Protected files
PROTECTED_FILES=(
".env"
".env.local"
".env.production"
".env.development"
"credentials.json"
"serviceAccountKey.json"
"id_rsa"
"id_ed25519"
"id_ecdsa"
".npmrc"
".pypirc"
"secrets.yml"
"secrets.yaml"
)
FILENAME=$(basename "$FILE_PATH")
for protected in "${PROTECTED_FILES[@]}"; do
if [[ "$FILENAME" == "$protected" ]]; then
echo "BLOCKED: Editing sensitive file '$FILENAME' is forbidden" >&2
exit 2
fi
done
# Block editing outside project (with configurable exceptions)
PROJECT_DIR="${CLAUDE_PROJECT_DIR:-$(pwd)}"
CLAUDE_HOME="${HOME}/.claude"
# Allowed paths (configurable via environment variable)
# Format: colon-separated paths - e.g., ALLOWED_PATHS="/custom/path:/other/path"
EXTRA_ALLOWED="${ALLOWED_PATHS:-}"
# Check if path is allowed
is_allowed=false
# Current project
[[ "$FILE_PATH" == "$PROJECT_DIR"* ]] && is_allowed=true
# Claude Code directory (~/.claude/) - plans, logs, settings
[[ "$FILE_PATH" == "$CLAUDE_HOME"* ]] && is_allowed=true
# Temporary files
[[ "$FILE_PATH" == "/tmp"* ]] && is_allowed=true
# Additional configured paths
if [[ -n "$EXTRA_ALLOWED" ]]; then
IFS=':' read -ra EXTRA_PATHS <<< "$EXTRA_ALLOWED"
for allowed_path in "${EXTRA_PATHS[@]}"; do
[[ "$FILE_PATH" == "$allowed_path"* ]] && is_allowed=true
done
fi
if [[ "$is_allowed" == "false" ]]; then
echo "BLOCKED: Editing outside project is forbidden: $FILE_PATH" >&2
echo "Allowed paths: $PROJECT_DIR, $CLAUDE_HOME, /tmp" >&2
[[ -n "$EXTRA_ALLOWED" ]] && echo "Additional allowed: $EXTRA_ALLOWED" >&2
exit 2
fi
fi
# === DELETE: Always warn ===
if [[ "$TOOL_NAME" == "Bash" ]]; then
COMMAND=$(echo "$TOOL_INPUT" | jq -r '.command // empty')
if echo "$COMMAND" | grep -qE "rm -r|rmdir|unlink"; then
# Warning but not blocking (exit 0)
echo '{"systemMessage": "Warning: File deletion detected. Verify this is intentional."}'
fi
fi
# Allow by default
exit 0

View file

@ -0,0 +1,62 @@
#!/bin/bash
# Hook: Notification - macOS alerts with contextual sounds
# Plays different sound based on notification type
#
# Place in: .claude/hooks/notification.sh
# Register in: .claude/settings.json under Notification event
#
# Note: macOS only - requires afplay and osascript
set -e
# Read JSON from stdin
INPUT=$(cat)
# Extract message and title
MESSAGE=$(echo "$INPUT" | jq -r '.message // "Claude Code Notification"')
TITLE=$(echo "$INPUT" | jq -r '.title // "Claude Code"')
# Select sound based on context
select_sound() {
local msg="$1"
local msg_lower=$(echo "$msg" | tr '[:upper:]' '[:lower:]')
# Success / Completion
if echo "$msg_lower" | grep -qE "(completed|terminé|fini|done|success|réussi|validé|finished)"; then
echo "/System/Library/Sounds/Hero.aiff"
return
fi
# Error / Failure
if echo "$msg_lower" | grep -qE "(error|erreur|failed|échec|échoué|problem|problème|failure)"; then
echo "/System/Library/Sounds/Basso.aiff"
return
fi
# Waiting / Permission
if echo "$msg_lower" | grep -qE "(waiting|attente|permission|approval|input|question|prompt)"; then
echo "/System/Library/Sounds/Submarine.aiff"
return
fi
# Warning / Attention
if echo "$msg_lower" | grep -qE "(warning|attention|caution|alert|avertissement)"; then
echo "/System/Library/Sounds/Sosumi.aiff"
return
fi
# Default
echo "/System/Library/Sounds/Ping.aiff"
}
SOUND_FILE=$(select_sound "$MESSAGE")
# Play sound (in background to avoid blocking)
if [[ -f "$SOUND_FILE" ]]; then
afplay "$SOUND_FILE" &
fi
# Display macOS notification
osascript -e "display notification \"$MESSAGE\" with title \"$TITLE\" sound name \"\"" 2>/dev/null || true
exit 0