📝
プルリクエストをClaudeに書かせるgit aipr
こちら[1]の記事でClaude Codeを用いてコミットメッセージを生成させるgit aliasが紹介されており、同じような感じでPRを生成してくれるaliasがあると便利だなと思ったのが背景です。
以下のようなaliasを.gitconfigに設定してgit aiprを実行すると、ベースブランチとの差分を元にPRのタイトルと本文を生成してくれます。
[alias]
aipr = "!f() { \
TARGET=$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name); \
DIFF=$(git diff origin/$TARGET...HEAD); \
if [ -z \"$DIFF\" ]; then echo \"No changes detected against $TARGET\"; return 1; fi; \
PROMPT='Based on the provided git diff: 1. Line 1: Generate a PR Title in English, strictly following the Conventional Commits format (e.g., feat: ..., fix: ...). 2. Line 2: Blank line. 3. Line 3+: Generate the PR Body in Japanese. - Keep it concise. Output ONLY the raw content without code blocks or conversational text.'; \
echo \"Generating PR content with Claude...\"; \
OUTPUT=$(echo \"$DIFF\" | claude -p \"$PROMPT\"); \
TITLE=$(echo \"$OUTPUT\" | head -n 1); \
BODY=$(echo \"$OUTPUT\" | tail -n +3); \
gh pr create --title \"$TITLE\" --body \"$BODY\" --web; \
}; f"
実行されるgh pr createで--webを指定しているため、作成されたPRの加筆修正をブラウザ上で行うことができます。
aliasの中で指定されているプロンプトをいじればPRのフォーマットも調整できると思うので、ぜひ色々試してみてください!
Discussion