🤖

Cursor CLIでPRの説明文を自動投稿する

に公開

モチベーション

現在弊社ではこちらの記事でご紹介した方法で、Cursor Agentを使ったPRの説明文の自動作成を取り入れています。
実際に運用する中で、「PRの編集自体も自動でできたら嬉しい」という声が出てきました。
本記事ではこれをCursor-CLIとGithub Actionsを使って実現する方法を考えます。

前提

Cursor-CLIとは、Cursorが提供するCLIツールです。
CLI上でcursor-agent コマンドを通じて任意のプロンプトを与え、処理を実行できます。
Cursor-CLIでは、公式からGithub Actionsの中で利用する例がいくつか紹介されています。
https://docs.cursor.com/ja/cli/cookbook/fix-ci
https://docs.cursor.com/ja/cli/cookbook/code-review
今回はこれらをもとに、PRの作成をトリガーとしてCI上で説明文を作成し、本文を直接編集するワークフローを構築しました。

コード

実際に作成したワークフローが以下です

name: Auto Create PR Description

on:
  pull_request:
    types: [opened, reopened]

permissions:
  contents: read
  pull-requests: write

jobs:
  create-description:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install Cursor CLI
        run: |
          curl https://cursor.com/install -fsS | bash
          echo "$HOME/.cursor/bin" >> $GITHUB_PATH

      - name: Configure git identity
        run: |
          git config user.name "Cursor Agent"
          git config user.email "cursoragent@cursor.com"

      - name: Create description
        env:
          CURSOR_API_KEY: ${{ secrets.CURSOR_API_KEY }}
          MODEL: claude-4-sonnet
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          set +e
          timeout 3m cursor-agent -p "あなたはリポジトリのPR description自動作成を行うGitHub Actionsランナーで動作しています。

          GitHub CLIは\`gh\`として利用可能で、\`GH_TOKEN\`で認証済みです。リポジトリの読み取り権限とプルリクエストのdescription更新権限があります。

          # コンテキスト:
          - リポジトリ: ${{ github.repository }}
          - PR番号: ${{ github.event.pull_request.number }}
          - PR URL: ${{ github.event.pull_request.html_url }}

          # 目標:
          リポジトリのPR作成ルールに従って、包括的なPR descriptionを作成する。

          # 要件:
          1) .cursor/rules/pr.mdc ルールを使用してPR descriptionをフォーマットする
          2) \`gh pr view\`と\`gh pr diff\`を使用してPRの変更を分析する
          3) 以下の形式でdescriptionを生成する:
             - チケット概要
             - DB変更
             - 技術的ポイント
             - 本番反映時作業
             - 周辺影響・テスト
          4) 生成したコンテンツでPR descriptionを更新する

          # 指示:
          - \`gh pr view ${{ github.event.pull_request.number }} --json title,body,url\`でPRの詳細を取得
          - \`gh pr diff ${{ github.event.pull_request.number }}\`でコード変更を分析
          - Ruby on Railsのコード変更、データベースマイグレーション、サービス実装に注目
          - .cursor/rules/pr.mdc ルールで指定されたmarkdown形式に従う
          - \`gh pr edit ${{ github.event.pull_request.number }} --body\`でPR descriptionを更新
          " --force --model "$MODEL" --output-format=text
          exit_code=$?
          
          if [ $exit_code -eq 124 ]; then
            echo "✅ Cursor agent completed"
            exit 0
          elif [ $exit_code -eq 0 ]; then
            echo "✅ Cursor agent completed"
            exit 0
          else
            echo "❌ Cursor agent failed"
            exit $exit_code
          fi

スクリプトを作る中で特に苦労したのは、Create descriptionのstepがいつまでも止まらず、CIが完了しなかった点です。
Cursor CLIでは、処理終端として改行を吐き出すようなのですが、このスクリプトではそれをうまくキャッチできていないようでした。

そこで、今回は以下のような手法でcursor-agentを強制終了 & stepを正常終了させるようにしました

  1. timeoutコマンドで、3mでjobをtimeout (exit_code 124)するように変更
  2. exit_code 124をcatchした際に、jobが正常終了するように修正
# 3分でtimeoutさせる
set +e timeout 3m cursor-agent -p

# exit_codeが124(timeout)であれば正常終了扱いにする
if [ $exit_code -eq 124 ]; then
    echo "✅ Cursor agent completed"
    exit 0
elif [ $exit_code -eq 0 ]; then
    echo "✅ Cursor agent completed"
    exit 0
else
    echo "❌ Cursor agent failed"
    exit $exit_code
fi

まとめ

本記事では、Cursor-CLIとGithub Actionsを用いたPR説明文の作成・編集の自動化についてご紹介しました。
CI上でAgentを動かす手法は、ドキュメントの自動更新など様々な場面で利用できそうなので、今後も使い道を模索していきます!


Bizibl では開発エンジニアを絶賛採用しています!カジュアル面談に興味がある方はこちらから!
https://open.talentio.com/r/1/c/bizibl/pages/99945

株式会社Bizibl Technologies テックブログ

Discussion