💬

Claude Code ActionでPRの内容を自動入力する

に公開

みなさんはClaude Code Actionを使ってますか?
この記事ではClaude Code ActionでPRの内容を自動入力する方法をご紹介します。

ここでは触れないこと

説明が長くなるので下記は割愛します。

  • Claude Code Actionとは?
  • Claude Code Actionの設定方法
  • Claude Code ActionのAPIキーの発行方法
  • APIキーをGitHubのシークレットに登録する方法

PRの内容を自動入力する方法

まず@claudeのメンションでClaude Code Actionを呼ぶワークフローを作成します。

.github/workflows/claude.yml
name: Claude

on:
  pull_request:
    types:
      - opened
  pull_request_review:
    types:
      - submitted
  pull_request_review_comment:
    types:
      - created
  issues:
    types:
      - opened
      - assigned
  issue_comment:
    types:
      - created

jobs:
  mention:
    if: |
      (github.event_name == 'pull_request' && contains(github.event.pull_request.body, '@claude')) ||
      (github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
      (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'issues' && contains(github.event.issue.body, '@claude')) ||
      (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude'))
    runs-on: ubuntu-latest
    timeout-minutes: 15
    permissions:
      contents: write
      pull-requests: write
      issues: write
      id-token: write
      actions: read
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
      - name: Run Claude Code
        uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          use_sticky_comment: true
          claude_args: >-
            --allowed-tools
            "Bash(gh search:*),
            Bash(gh issue view:*),
            Bash(gh issue comment:*),
            Bash(gh issue list:*),
            Bash(gh pr view:*),
            Bash(gh pr comment:*),
            Bash(gh pr list:*)
            Bash(gh pr edit:*),
            Bash(gh pr diff:*)"
          prompt: ${{ github.event.pull_request.body || '' }}

次にPRのテンプレートファイルに@claudeのメンションでPRを入力してもらう旨のコメントを追加します。
設定はこれだけです。

.github/PULL_REQUEST_TEMPLATE.md
+ <!-- @claude このPRの⚪︎⚪︎と⚪︎⚪︎の項目を日本語で整理し、`gh pr edit`コマンドを使用してPR本文に記載してください。 -->

注意点として、GitHub Actionsの仕様で一部イベントはワークフローがデフォルトブランチにマージされていないとトリガーしないのでマージしてください。
これで次回のPRの作成時にPRの内容が自動入力されます。
https://docs.github.com/ja/actions/reference/workflows-and-actions/events-that-trigger-workflows

ポイント

仕組みとしてはかなり単純で、PR作成時に@claudeのメンションを含むPRの内容をプロンプトとして渡してClaude Code Actionで処理させているだけです。

Issueコメントからの呼び出しとかだとgithub.event.pull_request.bodyにundefinedが入って機能しなくなるので論理和演算子(||)で空値入れてガードしています。

また、use_sticky_commentオプションをtrueにすることでPRやIssueに対するコメントを追記ではなく上書きできるようにし、PR本文にPR内容を記載させています。

.github/workflows/claude.yml
      - name: Run Claude Code
        uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
+         use_sticky_comment: true
          claude_args: >-
            --allowed-tools
            "Bash(gh search:*),
            Bash(gh issue view:*),
            Bash(gh issue comment:*),
            Bash(gh issue list:*),
            Bash(gh pr view:*),
            Bash(gh pr comment:*),
            Bash(gh pr list:*)
            Bash(gh pr edit:*),
            Bash(gh pr diff:*)"
+         prompt: ${{ github.event.pull_request.body || '' }}

所感

Claude CodeでGitHub CLIやGitHub MCP経由で直接PRを作成している人には必要無いかもしれないですが、設定しておくと結構便利です。
ただし、PR作成数が多いと1リポジトリでもAPIコストで1ヶ月あたり2~3万円くらいは飛ぶ可能性があるのでコストには気をつけてください。

ラッコ株式会社

Discussion