iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📑

Automate Refactoring and PR Title/Description Generation with AI

に公開

Codex

I will show you how to use Codex to automate small refactorings in a repository while simultaneously generating the Pull Request title and description. The goal of this automation is to reduce rework by providing a well-structured explanation before the review process.

Background and Objectives

In the course of daily revisions to Swift and Objective-C code, the task of accumulating small refactorings can become monotonous. I have built a workflow to delegate parts that can be handled by generative AI, allowing developers to focus on areas that require human judgment. By combining Codex's full-auto mode with its JSON output functionality, this objective can be achieved.

Environment Preparation

This guide assumes that the Codex CLI, jq, and gh commands are installed. If you are integrating this into CI, ensure the same set of tools is available in the job environment. The steps described in this article are based on verification performed on a local macOS environment.

Approach to Execution Parameters

By fixing the output format with --output-schema, you eliminate the risk of the generative AI including unnecessary text. The --output-last-message option saves the final response to a file, which is then used to extract PR information in subsequent shell scripts. Specifying --full-auto allows the refactoring to be completed without interaction, following the instructions provided. Only by combining these features do refactoring and metadata generation become a consistent workflow.

JSON Schema Design

PR descriptions are information that is easily templated. As an example, the following schema is prepared, making both the title and description mandatory items.

refactoring-output-schema.json

{
  "type": "object",
  "properties": {
    "title": {
      "type": "string",
      "description": "PR title"
    },
    "description": {
      "type": "string",
      "description": "PR description"
    }
  },
  "required": [
    "title",
    "description"
  ],
  "additionalProperties": false
}

Workflow Steps

Once the schema and prompt are prepared, use the following script to request a refactoring from Codex. The generated JSON is extracted using jq and formatted into a structure that can be used for PR creation.

CODEX_PROMPT="小さなリファクタリングをしてください"
LAST_MSG_FILE=$(mktemp)
SCHEMA_PATH="refactoring-output-schema.json"
codex exec --experimental-json --output-schema "$SCHEMA_PATH" --output-last-message "$LAST_MSG_FILE" --full-auto "$CODEX_PROMPT"
PR_TITLE=$(jq -r '.title // empty' "$LAST_MSG_FILE")
PR_DESCRIPTION=$(jq -r '.description // empty' "$LAST_MSG_FILE")
rm -f "$LAST_MSG_FILE"

In this example, the final response is saved to a temporary file and deleted after extraction is complete. When running in CI, be careful to explicitly specify an artifact directory to avoid file collisions between jobs.

Flow to PR Creation

Once you have the PR title and description, simply use gh pr create to submit it to GitHub. You can adjust the prompt so that the generated description includes bullet points for review perspectives and changes. Reviewing the content yourself and manually adding project-specific labels before the submission will make it even more effective.

gh pr create \
    --title "$PR_TITLE" \
    --body "$PR_DESCRIPTION"

Summary

By combining Codex's automated execution and JSON schema output, refactoring and PR creation can now be handled as a single continuous workflow. By delegating routine tasks to generative AI, developers can focus on decision-making and reviews. Adjust the prompts and schemas to fit your team's workflow to help with continuous improvement.

Discussion