✍️

Pull Requestのタイトルと説明文をCodex Github Actionで簡単に自動入力する方法

に公開

こんにちは!株式会社エクスプラザの@JunInaba1です。
今回はCodex GitHub Actionを使った、Pull Request のタイトルと説明文の自動入力を紹介します。

はじめに

Pull Requestを作成する際、その変更内容を的確に説明するタイトルと説明文を書くことは、レビュワーとの円滑なコミュニケーションのために非常に重要です。しかし、変更点を一つ一つまとめて記述するのは、時に手間のかかる作業です。

このプロセスを自動化するために、今回はopenai/codex-actionを利用して、Pull Requestの差分からタイトルと説明文をAIに自動生成させるGitHub Actionsワークフローを構築する方法をご紹介します。
https://github.com/openai/codex-action

Codex GitHub Actionとは

Codex GitHub Actionは、GitHub ActionsのワークフローからCodexを実行するためのアクションです。

公式リポジトリの説明を翻訳し、引用します

GitHub Actions ワークフローから Codex を実行しつつ、Codex が利用できる権限を厳密に制御します。このアクションは、Codex CLI のインストールと、Responses API へのセキュアなプロキシ設定を処理します。
このアクションを使用するには、ユーザーは OPENAI_API_KEY を GitHub Actions のシークレットとして提供する必要があります。

これにより、リポジトリのコード内容をコンテキストとしてAIに渡し、コードレビューやドキュメント生成など、さまざまなタスクを自動化できます。

完成したWorkflow


Codexで生成されたタイトルと説明文

まず、完成したワークフローの全体像を以下に示します。このワークフローは、Pull Requestがオープンされたときにトリガーされます。

workflow
pr-description.yaml
name: "PR description"

on:
  pull_request:
    types: [opened]

jobs:
  codex:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      final_message: ${{ steps.run_codex.outputs.final-message }}
    steps:
      - uses: actions/checkout@v5
        with:
          ref: refs/pull/${{ github.event.pull_request.number }}/merge

      - name: Pre-fetch base and head refs
        run: |
          git fetch --no-tags origin \
            ${{ github.event.pull_request.base.ref }} \
            +refs/pull/${{ github.event.pull_request.number }}/head

      - name: Run Codex
        id: run_codex
        uses: openai/codex-action@v1
        with:
          openai-api-key: ${{ secrets.OPENAI_API_KEY }}
          output-schema: |
            {
              "type": "object",
              "properties": {
                "title": {"type": "string"},
                "body": {"type": "string"}
              },
              "required": ["title", "body"],
              "additionalProperties": false
            }
          prompt: |
            Repo: ${{ github.repository }}
            PR #${{ github.event.pull_request.number }}
            Base SHA: ${{ github.event.pull_request.base.sha }}
            Head SHA: ${{ github.event.pull_request.head.sha }}

            このPRで導入される変更だけを要約してください。
            提供されたスキーマに一致するJSONを出力してください。
            「body」にはGitHub Flavored Markdownを使ってください。

  update-pr:
    runs-on: ubuntu-latest
    needs: codex
    if: needs.codex.outputs.final_message != ''
    permissions:
      pull-requests: write
    steps:
      - name: Update PR title/body
        uses: actions/github-script@v7
        env:
          CODEX_OUTPUT: ${{ needs.codex.outputs.final_message }}
        with:
          github-token: ${{ github.token }}
          script: |
            const out = JSON.parse(process.env.CODEX_OUTPUT);
            const number = context.payload.pull_request.number;

            await github.rest.pulls.update({
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: number,
              title: out.title ?? context.payload.pull_request.title,
              body:  out.body  ?? context.payload.pull_request.body
            });

Workflowの解説

このワークフローは、codexupdate-prという2つのジョブで構成されています。

codex job

このジョブの役割は、Codexを実行してPull Requestのタイトルと説明文をJSON形式で生成することです。

  1. コードのチェックアウト
    actions/checkout@v5 を使って、対象のPull Requestのコードをチェックアウトします。公式のヒント[1]にもあるように、Codex Actionがリポジトリのコンテンツにアクセスできるよう、checkout の後に実行する必要があります。

  2. 差分情報の取得
    git fetch を実行して、PRの比較元(base)と比較先(head)の情報を取得し、Codexが差分を正しく認識できるようにします。

  3. Codexの実行
    openai/codex-action@v1 を実行します。ここで重要なのは with で指定するパラメータです。

    • openai-api-key: OPENAI_API_KEY をGitHubのSecretsに登録して利用します。
    • prompt: Codexに与える指示(プロンプト)です。
    • output-schema: これが今回のキーポイントです。

output-schema で出力を制御する

output-schema を使うことで、Codexの出力を指定したJSONスキーマに沿った形式に強制することができます。
今回はtitlebodyという2つの文字列プロパティを持つJSONを要求しています。

ActionのOutputs

codex-action は、実行結果を final-message という名前の output として提供します。
codex ジョブの outputs セクションで final_message: ${{ steps.run_codex.outputs.final-message }} と定義することで、このジョブの実行結果を他のジョブから参照できるようになります。

update-pr job

このジョブは、codex ジョブの実行結果を受け取り、実際のPull Requestを更新します。

おわりに

今回は Codex GitHub Action を使って、Pull Requestのタイトルと説明文の自動入力するワークフローを紹介しました。output-schema を活用することで、AIからの出力を安定させ、後続の処理とスムーズに連携できるのが大きな利点です。

ぜひ試してみてください。

脚注
  1. https://github.com/openai/codex-action?tab=readme-ov-file#additional-tips ↩︎

株式会社エクスプラザ

Discussion