Tüm Yazılar
KategoriAI
Okuma Süresi
18 dk okuma
Yayın Tarihi
...
Kelime Sayısı
1.582kelime

Kahveni hazırla - bu içerikli bir makale!

Hook sistemi, EventType yapilandirmasi, guvenlik katmanlari ve CI/CD pipeline entegrasyonu. Claude Code workflow otomasyonunun eksiksiz teknik rehberi.

Claude Code Hooks: Pre-Commit ve Post-Commit Otomasyonu

# Claude Code Hooks: Pre-Commit ve Post-Commit Otomasyonu

Claude Code'un gercek gucu sadece kod yazmak degil — workflow'unu otomatiklestirmek. Hook sistemi, Claude Code'un her adimina mudahale edebilmeni, guvenlik katmanlari ekleyebilmeni ve CI/CD pipeline'inla entegre edebilmeni sagliyor. Bu yazida hook mimarisinden production-ready orneklere kadar her seyi kesfedecegiz.

Not: Bu rehber Claude Code resmi dokumantasyonu, Claude Code GitHub reposu ve gercek proje deneyimlerine dayanmaktadir.

Icindekiler


1. Hook Sistemi Nedir?

Claude Code hook sistemi, AI agent'in yasam dongusundeki belirli noktalara mudahale edebilmeni saglayan bir mekanizmadir. Git hook'larindan ilham almis ama cok daha guclu:

Ozellik
Git Hooks
Claude Code Hooks
Tetikleme
git commit/push
Tool use, dosya islem, her adim
Tur
Shell script
Command + Prompt
AI entegrasyon
Yok
AI ile karar verme
Kapsam
Repo bazli
Proje + global
Matcher
Yok
Regex pattern matching

Neden Hook Kullanmalisin?

  1. Guvenlik — Tehlikeli islemleri engellemen veya onay istemen
  2. Kalite — Her degisiklikten sonra lint/test calistirma
  3. Otomasyon — Tekrarlayan islemleri otomatiklestirme
  4. Loglama — Tum AI islemlerini kayit altina alma
  5. Egitim — AI'in belirli pattern'leri ogrenip uygulamasi

2. EventType ve Yasam Dongusu

Claude Code su event'leri destekler:

PreToolUse

Tool cagrilmadan once tetiklenir. Tool cagirisini onaylayabilir, reddedebilir veya degistirebilirsin:

swift
1Kullanici istegi > AI karar > PreToolUse > [Onay/Red] > Tool calisir

PostToolUse

Tool calistiktan sonra tetiklenir. Sonucu inceleyebilir, loglayabilir veya ek islem tetikleyebilirsin:

swift
1Tool calisir > PostToolUse > [Log/Aksiyon] > AI sonucu yorumlar

Notification

Claude Code bildirim gonderdiginde tetiklenir. Uzun sureli islemlerde kullaniciya bilgi verebilirsin.

Stop

Claude Code calismasini bitirdiginde tetiklenir. Ozet olusturma, cleanup veya rapor gonderme icin idealdir.

Pro Tip: PreToolUse en kritik hook tipidir — tehlikeli islemleri calismadan once yakalayabilirsin. PostToolUse ise audit logging ve otomasyon icin mukemmeldir.

3. Hook Tipleri: Command vs Prompt

Command Hook

Shell komutu calistirir. stdin'den JSON alir, stdout'a JSON yazar:

json
1{
2 "type": "command",
3 "command": "node ./hooks/validate-tool.js"
4}

Hook'a gelen JSON yapisi:

typescript
1interface HookInput {
2 session_id: string;
3 tool_name: string;
4 tool_input: Record;
5}

Hook'un dondurebilecegi yanitlar:

typescript
1// Onayla
2{ "decision": "approve" }
3 
4// Reddet (aciklama ile)
5{ "decision": "block", "reason": "Production branch'e push yasak!" }
6 
7// AI'a sor
8{ "decision": "ask", "message": "Bu dosyayi silmek istediginizden emin misiniz?" }

Prompt Hook

Bir AI prompt'u calistirir ve AI'in karar vermesini saglar:

json
1{
2 "type": "prompt",
3 "prompt": "Bu degisiklik guvenli mi? Production veritabanini etkiler mi? approve veya block don."
4}

4. settings.json Yapilandirmasi

Hook'lar .claude/settings.json dosyasinda tanimlanir:

json
1{
2 "hooks": {
3 "PreToolUse": [
4 {
5 "matcher": "Bash",
6 "hooks": [
7 {
8 "type": "command",
9 "command": "node .claude/hooks/validate-bash.js"
10 }
11 ]
12 },
13 {
14 "matcher": "Edit|Write",
15 "hooks": [
16 {
17 "type": "command",
18 "command": "node .claude/hooks/validate-file-edit.js"
19 }
20 ]
21 },
22 {
23 "matcher": "mcp__.*__delete.*",
24 "hooks": [
25 {
26 "type": "prompt",
27 "prompt": "Silme islemi tespit edildi. Bu islem geri alinamaz. Guvenli mi?"
28 }
29 ]
30 }
31 ],
32 "PostToolUse": [
33 {
34 "matcher": "Edit|Write",
35 "hooks": [
36 {
37 "type": "command",
38 "command": "node .claude/hooks/post-edit-lint.js"
39 }
40 ]
41 }
42 ],
43 "Stop": [
44 {
45 "hooks": [
46 {
47 "type": "command",
48 "command": "node .claude/hooks/session-summary.js"
49 }
50 ]
51 }
52 ]
53 }
54}

Matcher Pattern Ornekleri

Matcher regex destekler:

Pattern
Eslestigi Araclar
`Bash`
Bash komutu calistirma
`Edit\
Write`
Dosya duzenleme ve yazma
`mcp__.*`
Tum MCP tool'lari
`mcp__playwright__.*`
Sadece Playwright MCP
`.*delete.*`
Adi delete iceren tum tool'lar

5. PreToolUse: Arac Oncesi Mudahale

En guclu hook tipi. Ornek: Tehlikeli bash komutlarini engelleme:

typescript
1// .claude/hooks/validate-bash.js
2import { readFileSync } from 'fs';
3 
4const input = JSON.parse(readFileSync('/dev/stdin', 'utf8'));
5 
6const DANGEROUS_PATTERNS = [
7 'rm -rf /',
8 'git push --force',
9 'git reset --hard',
10 'DROP TABLE',
11 'DELETE FROM',
12 'chmod 777'
13];
14 
15const command = input.tool_input?.command || '';
16 
17const isDangerous = DANGEROUS_PATTERNS.some(pattern =>
18 command.toLowerCase().includes(pattern.toLowerCase())
19);
20 
21if (isDangerous) {
22 const output = {
23 decision: 'block',
24 reason: 'Tehlikeli komut engellendi: ' + command
25 };
26 console.log(JSON.stringify(output));
27} else if (command.includes('sudo')) {
28 const output = {
29 decision: 'ask',
30 message: 'sudo komutu tespit edildi: ' + command
31 };
32 console.log(JSON.stringify(output));
33} else {
34 console.log(JSON.stringify({ decision: 'approve' }));
35}

Bu yaklasim iOS Security Best Practices yazimizda anlattigimiz "defense in depth" prensibinin AI araclarina uygulanmis halidir.


6. PostToolUse: Arac Sonrasi Islem

Her dosya degisikliginden sonra otomatik lint calistirma:

typescript
1// .claude/hooks/post-edit-lint.js
2import { readFileSync } from 'fs';
3import { execFileSync } from 'child_process';
4 
5const input = JSON.parse(readFileSync('/dev/stdin', 'utf8'));
6const filePath = input.tool_input?.file_path || '';
7 
8if (filePath.endsWith('.ts') || filePath.endsWith('.tsx')) {
9 try {
10 const result = execFileSync(
11 'npx', ['eslint', '--fix', filePath],
12 { encoding: 'utf8', timeout: 10000 }
13 );
14 console.log(JSON.stringify({
15 decision: 'approve',
16 message: 'Lint tamamlandi: ' + filePath
17 }));
18 } catch (error) {
19 console.log(JSON.stringify({
20 decision: 'approve',
21 message: 'Lint uyarisi mevcut'
22 }));
23 }
24} else {
25 console.log(JSON.stringify({ decision: 'approve' }));
26}

CI/CD Pipeline yazimizda anlattigimiz otomatik kalite kontrol adimlarini Claude Code hook'lariyla da uygulayabilirsin.


7. Guvenlik Hook Ornekleri

Secret Detection Hook

typescript
1// .claude/hooks/secret-detection.js
2import { readFileSync } from 'fs';
3 
4const input = JSON.parse(readFileSync('/dev/stdin', 'utf8'));
5const content = input.tool_input?.new_string ||
6 input.tool_input?.content || '';
7 
8const SECRET_PATTERNS = [
9 /api[_-]?key.*[:=].*['"][A-Za-z0-9]{20,}['"]/i,
10 /secret.*[:=].*['"][^'"]{8,}['"]/i,
11 /-----BEGIN (?:RSA |EC )?PRIVATE KEY-----/,
12 /ghp_[A-Za-z0-9]{36}/,
13 /sk-[A-Za-z0-9]{48}/,
14 /AKIA[A-Z0-9]{16}/
15];
16 
17const hasSecret = SECRET_PATTERNS.some(pattern =>
18 pattern.test(content)
19);
20 
21if (hasSecret) {
22 console.log(JSON.stringify({
23 decision: 'block',
24 reason: 'Potansiyel secret tespit edildi! .env dosyasini kullanin.'
25 }));
26} else {
27 console.log(JSON.stringify({ decision: 'approve' }));
28}

Dosya Koruma Hook'u

typescript
1// .claude/hooks/protect-files.js
2import { readFileSync } from 'fs';
3 
4const input = JSON.parse(readFileSync('/dev/stdin', 'utf8'));
5const filePath = input.tool_input?.file_path || '';
6 
7const PROTECTED = [
8 '.env', '.env.local', 'firebase-adminsdk',
9 'service-account.json', 'credentials.json', 'id_rsa'
10];
11 
12const isProtected = PROTECTED.some(pf => filePath.includes(pf));
13 
14if (isProtected) {
15 console.log(JSON.stringify({
16 decision: 'block',
17 reason: 'Korunmus dosya: ' + filePath
18 }));
19} else {
20 console.log(JSON.stringify({ decision: 'approve' }));
21}
Pro Tip: Secret detection hook'unu PreToolUse'da hem Edit hem Write matcher'lariyla kullan. Tek bir satir bile sizdirabilir.

8. CI/CD Entegrasyonu

GitHub Actions ile Claude Code

yaml
1# .github/workflows/claude-code.yml
2name: Claude Code CI
3on:
4 push:
5 branches: [main]
6 
7jobs:
8 claude-review:
9 runs-on: ubuntu-latest
10 steps:
11 - uses: actions/checkout@v4
12 - name: Claude Code Review
13 run: |
14 claude --print "Bu PR degisikliklerini review et" \
15 --model claude-sonnet-4-5 \
16 --output-format json > review.json
17 - name: Post Review Comment
18 run: node scripts/post-review.js review.json

Session Ozet Hook'u

typescript
1// .claude/hooks/session-summary.js — Stop event'inde calisir
2import { readFileSync, appendFileSync } from 'fs';
3 
4const input = JSON.parse(readFileSync('/dev/stdin', 'utf8'));
5 
6const summary = [
7 '## Session: ' + new Date().toISOString(),
8 '- ID: ' + input.session_id,
9 '- Dosya: ' + (input.files_changed?.length || 0),
10 '- Sure: ' + Math.round((input.duration_seconds || 0) / 60) + ' dk',
11 ''
12].join('\n');
13 
14appendFileSync('worklog.md', summary);
15console.log(JSON.stringify({ decision: 'approve' }));

Bu yaklasim Mobile DevOps Best Practices yazimizda ele aldigimiz otomasyon prensibinin AI versiyonudur.


9. Gercek Dunya Ornekleri

Ornek 1: Conventional Commits Zorunlulugu

typescript
1// Pre-commit hook: commit mesaji format kontrolu
2const input = JSON.parse(readFileSync('/dev/stdin', 'utf8'));
3const command = input.tool_input?.command || '';
4 
5if (command.startsWith('git commit')) {
6 const messageMatch = command.match(/-m ["'](.+?)["']/);
7 if (messageMatch) {
8 const message = messageMatch[1];
9 const pattern = /^(feat|fix|docs|style|refactor|test|chore)(\(.+))?: .{10,}/;
10 
11 if (!pattern.test(message)) {
12 console.log(JSON.stringify({
13 decision: 'block',
14 reason: 'Conventional Commits formatinda olmali: type(scope): description'
15 }));
16 } else {
17 console.log(JSON.stringify({ decision: 'approve' }));
18 }
19 } else {
20 console.log(JSON.stringify({ decision: 'approve' }));
21 }
22} else {
23 console.log(JSON.stringify({ decision: 'approve' }));
24}

Ornek 2: Dosya Boyutu Kontrolu

typescript
1// Post-edit hook: 800 satir kontrolu
2const input = JSON.parse(readFileSync('/dev/stdin', 'utf8'));
3const filePath = input.tool_input?.file_path || '';
4 
5if (filePath) {
6 try {
7 const content = readFileSync(filePath, 'utf8');
8 const lineCount = content.split('\n').length;
9 
10 if (lineCount > 800) {
11 console.log(JSON.stringify({
12 decision: 'approve',
13 message: 'UYARI: ' + filePath + ' ' + lineCount + ' satir. Dosyayi bolmeyi dusun.'
14 }));
15 } else {
16 console.log(JSON.stringify({ decision: 'approve' }));
17 }
18 } catch {
19 console.log(JSON.stringify({ decision: 'approve' }));
20 }
21} else {
22 console.log(JSON.stringify({ decision: 'approve' }));
23}

Ornek 3: MCP Tool Loglama

json
1{
2 "hooks": {
3 "PreToolUse": [
4 {
5 "matcher": "mcp__github__.*",
6 "hooks": [
7 {
8 "type": "command",
9 "command": "echo \"GitHub tool called: $(jq -r '.tool_name')\" >&2"
10 }
11 ]
12 }
13 ]
14 }
15}

Claude Code MCP yazimizdaki MCP tool'larini hook'larla izleyerek kapsamli bir audit log olusturabilirsin.


10. Sonuc ve Oneriler

Claude Code hook sistemi, AI destekli gelistirmeyi kontrol altinda tutmanin en etkili yoludur. Dogru yapilandirilmis hook'lar ile guvenlik, kalite ve otomasyon saglarsin. Hook'lari multi-agent team'lerle birlestirdiginde otomasyon gucunu katlayabilirsin.

Baslangic Onerileri:

  1. Secret detection — Ilk gun ekle, hayat kurtarir
  2. Dosya koruma — .env ve credential dosyalarini koru
  3. Post-edit lint — Otomatik kalite kontrolu
  4. Session summary — Her calisma sonunda ozet
  5. Conventional commits — Commit mesaji standartlastirma

ALTIN İPUCU

Bu yazının en değerli bilgisi

Bu ipucu, yazının en önemli çıkarımını içeriyor.

Easter Egg

Gizli bir bilgi buldun!

Bu bölümde gizli bir bilgi var. Keşfetmek ister misin?

Okuyucu Ödülü

🎉 **Sonuna Kadar Okuyan Kahraman!** Tebrikler! Iste sana ozel bir kaynak: [Claude Code Hook SDK](https://github.com/mizunashi-mana/claude-code-hook-sdk) — TypeScript ile tip-guvenligi olan hook'lar yazmak icin community SDK. Zod validasyon, test helper'lar ve dependency injection dahil.

Etiketler

#Claude Code#Hooks#CI/CD#Automation#TypeScript#DevOps
Muhittin Çamdalı

Muhittin Çamdalı

Senior iOS Developer

12+ yıllık deneyime sahip iOS Developer. Swift, SwiftUI ve modern iOS mimarileri konusunda uzman. Apple platformlarında performanslı ve kullanıcı dostu uygulamalar geliştiriyorum.

iOS Geliştirme Haberleri

Haftalık Swift tips, SwiftUI tricks ve iOS best practices. Spam yok, sadece değerli içerik.

Gizliliğinize saygı duyuyoruz. İstediğiniz zaman abonelikten çıkabilirsiniz.

Paylaş

Bunu da begenebilirsiniz