|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# PreToolUse hook: gh pr create / gh issue create のテンプレート準拠チェック |
| 4 | +# |
| 5 | +# Claude Codeのhookとして実行され、必須セクションが欠けていれば exit 2 でブロックする。 |
| 6 | +# テンプレートファイルから見出しを動的に抽出するため、テンプレート変更時にこのスクリプトの修正は不要。 |
| 7 | + |
| 8 | +REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)" |
| 9 | +if [ -z "$REPO_ROOT" ]; then |
| 10 | + exit 0 |
| 11 | +fi |
| 12 | + |
| 13 | +input=$(cat) |
| 14 | +command=$(printf '%s' "$input" | jq -r '.tool_input.command // empty') |
| 15 | + |
| 16 | +if [ -z "$command" ]; then |
| 17 | + exit 0 |
| 18 | +fi |
| 19 | + |
| 20 | +# テンプレートファイルからMarkdown見出し行を抽出する |
| 21 | +# 引数: $1 = テンプレートファイルパス, $2 = "yaml" (YAML front matterをスキップする場合) |
| 22 | +extract_headings() { |
| 23 | + local template_file="$1" |
| 24 | + local has_frontmatter="$2" |
| 25 | + |
| 26 | + if [ "$has_frontmatter" = "yaml" ]; then |
| 27 | + awk '/^---$/{count++; next} count>=2' "$template_file" | grep -E '^#{1,2} ' |
| 28 | + else |
| 29 | + grep -E '^#{1,2} ' "$template_file" |
| 30 | + fi |
| 31 | +} |
| 32 | + |
| 33 | +# bodyにすべての見出しが含まれているかチェックし、不足があればブロックする |
| 34 | +# 引数: $1 = テンプレートファイルパス, $2 = "yaml" or "", $3 = エラーメッセージのラベル("PR" or "Issue") |
| 35 | +validate_template() { |
| 36 | + local template_file="$1" |
| 37 | + local frontmatter="$2" |
| 38 | + local label="$3" |
| 39 | + |
| 40 | + if [ ! -f "$template_file" ]; then |
| 41 | + return 0 |
| 42 | + fi |
| 43 | + |
| 44 | + local missing=() |
| 45 | + while IFS= read -r heading; do |
| 46 | + [ -z "$heading" ] && continue |
| 47 | + # 見出し行から # マーカーを除去してテキスト部分を取得 |
| 48 | + local heading_text |
| 49 | + heading_text=$(printf '%s' "$heading" | sed 's/^#* //') |
| 50 | + if ! printf '%s' "$command" | grep -qF "$heading_text"; then |
| 51 | + missing+=("$heading") |
| 52 | + fi |
| 53 | + done < <(extract_headings "$template_file" "$frontmatter") |
| 54 | + |
| 55 | + if [ ${#missing[@]} -gt 0 ]; then |
| 56 | + { |
| 57 | + echo "${label}のbodyにテンプレートの必須セクションが含まれていません。" |
| 58 | + echo "不足しているセクション:" |
| 59 | + for section in "${missing[@]}"; do |
| 60 | + echo " - $section" |
| 61 | + done |
| 62 | + echo "" |
| 63 | + echo "テンプレート内容:" |
| 64 | + if [ "$frontmatter" = "yaml" ]; then |
| 65 | + awk '/^---$/{count++; next} count>=2' "$template_file" |
| 66 | + else |
| 67 | + cat "$template_file" |
| 68 | + fi |
| 69 | + } >&2 |
| 70 | + exit 2 |
| 71 | + fi |
| 72 | +} |
| 73 | + |
| 74 | +# gh pr create のバリデーション |
| 75 | +if printf '%s' "$command" | grep -q 'gh pr create'; then |
| 76 | + validate_template "$REPO_ROOT/.github/pull_request_template.md" "" "PR" |
| 77 | +fi |
| 78 | + |
| 79 | +# gh issue create のバリデーション |
| 80 | +if printf '%s' "$command" | grep -q 'gh issue create'; then |
| 81 | + validate_template "$REPO_ROOT/.github/ISSUE_TEMPLATE/default.md" "yaml" "Issue" |
| 82 | +fi |
| 83 | + |
| 84 | +exit 0 |
0 commit comments