|
1 | 1 | # Build Code Review with the Codex SDK |
2 | 2 |
|
3 | | -With [Code Review](https://chatgpt.com/codex/settings/code-review) in Codex Cloud, you can connect your team's cloud hosted Github repository to Codex and received automated code reviews on every PR. But what if your code is hosted on-prem, or you don't have Github as an SCM? |
| 3 | +With [Code Review](https://chatgpt.com/codex/settings/code-review) in Codex Cloud, you can connect your team's cloud hosted Github repository to Codex and receive automated code reviews on every PR. But what if your code is hosted on-prem, or you don't have Github as an SCM? |
4 | 4 |
|
5 | 5 | Luckily, we can replicate Codex's cloud hosted review process in our own CI/CD runners. In this guide, we'll build our own Code Review action using the Codex CLI headless mode with both Github actions and Jenkins. |
6 | 6 |
|
7 | 7 | To build our own Code review, we'll take the following steps: |
8 | | -1. Install the Codex CLI |
9 | | -1. Use the Code Review prompt that ships with the CLI |
10 | | -1. Specify a structured output JSON schema |
11 | | -1. Make API calls to our SCM to create review comments |
| 8 | +1. Install the Codex CLI in our CI/CD runner |
| 9 | +1. Prompt Codex in headless (exec) mode with the Code Review prompt that ships with the CLI |
| 10 | +1. Specify a structured output JSON schema for Codex |
| 11 | +1. Parse the JSON result and use it to make API calls to our SCM to create review comments |
12 | 12 |
|
13 | 13 | Once implemented, Codex will be able to leave inline code review comments: |
14 | 14 | <img src="../../images/codex_code_review.png" alt="Codex Code Review in Github" width="500"/> |
15 | 15 |
|
| 16 | +## The Code Review Prompt |
| 17 | +GPT-5-Codex has received specific training to improve is code review abilities. You can steer GPT-5-Codex to conduct a code review with the following prompt: |
| 18 | + |
| 19 | +``` |
| 20 | +You are acting as a reviewer for a proposed code change made by another engineer. |
| 21 | +Focus on issues that impact correctness, performance, security, maintainability, or developer experience. |
| 22 | +Flag only actionable issues introduced by the pull request. |
| 23 | +When you flag an issue, provide a short, direct explanation and cite the affected file and line range. |
| 24 | +Prioritize severe issues and avoid nit-level comments unless they block understanding of the diff. |
| 25 | +After listing findings, produce an overall correctness verdict (\"patch is correct\" or \"patch is incorrect\") with a concise justification and a confidence score between 0 and 1. |
| 26 | +Ensure that file citations and line numbers are exactly correct using the tools available; if they are incorrect your comments will be rejected. |
| 27 | +``` |
| 28 | +## Codex Structured Outputs |
| 29 | +In order to make comments on code ranges in our pull request, we need to receive Codex's response in a specific format. To do that we can create a file called `codex-output-schema.json` that conforms to OpenAI's [structured outputs](https://platform.openai.com/docs/guides/structured-outputs) format. |
| 30 | + |
| 31 | +To use this file in our workflow YAML, we can call Codex with the `output-schema-file` argument like this: |
| 32 | + |
| 33 | +```yaml |
| 34 | +- name: Run Codex structured review |
| 35 | + id: run-codex |
| 36 | + uses: openai/codex-action@main |
| 37 | + with: |
| 38 | + openai-api-key: ${{ secrets.OPENAI_API_KEY }} |
| 39 | + prompt-file: codex-prompt.md |
| 40 | + sandbox: read-only |
| 41 | + model: ${{ env.CODEX_MODEL }} |
| 42 | + output-schema-file: codex-output-schema.json # <-- Our schema file |
| 43 | + output-file: codex-output.json |
| 44 | +``` |
| 45 | +
|
| 46 | +You can also pass a similar argument to `codex exec` for example: |
| 47 | + |
| 48 | +```bash |
| 49 | +codex exec "Review my pull request!" --output-schema codex-output-schema.json |
| 50 | +``` |
| 51 | + |
16 | 52 | ## Github Actions Example |
17 | | -If you're using Github actions in an on-prem environment, you can tailor this example to your specific workflow. Inline comments highlight the key steps. |
| 53 | +Let's put it all together. If you're using Github actions in an on-prem environment, you can tailor this example to your specific workflow. Inline comments highlight the key steps. |
18 | 54 | ```yaml |
19 | 55 | name: Codex Code Review |
20 | 56 |
|
@@ -183,9 +219,8 @@ jobs: |
183 | 219 | printf '%s\n' "Flag only actionable issues introduced by the pull request." |
184 | 220 | printf '%s\n' "When you flag an issue, provide a short, direct explanation and cite the affected file and line range." |
185 | 221 | printf '%s\n' "Prioritize severe issues and avoid nit-level comments unless they block understanding of the diff." |
186 | | - printf '%s\n' "After listing findings, produce an overall correctness verdict (\"patch is correct\" or \"patch is incorrect\")" |
| 222 | + printf '%s\n' "After listing findings, produce an overall correctness verdict (\"patch is correct\" or \"patch is incorrect\") with a concise justification and a confidence score between 0 and 1." |
187 | 223 | printf '%s\n' "Ensure that file citations and line numbers are exactly correct using the tools available; if they are incorrect your comments will be rejected." |
188 | | - printf '%s\n' "with a concise justification and a confidence score between 0 and 1." |
189 | 224 | } > "$PROMPT_PATH" |
190 | 225 | fi |
191 | 226 |
|
@@ -453,9 +488,8 @@ pipeline { |
453 | 488 | printf '%s\n' "Flag only actionable issues introduced by the pull request." |
454 | 489 | printf '%s\n' "When you flag an issue, provide a short, direct explanation and cite the affected file and line range." |
455 | 490 | printf '%s\n' "Prioritize severe issues and avoid nit-level comments unless they block understanding of the diff." |
456 | | - printf '%s\n' "After listing findings, produce an overall correctness verdict (\\\"patch is correct\\\" or \\\"patch is incorrect\\\")" |
| 491 | + printf '%s\n' "After listing findings, produce an overall correctness verdict (\\\"patch is correct\\\" or \\\"patch is incorrect\\\") with a concise justification and a confidence score between 0 and 1." |
457 | 492 | printf '%s\n' "Ensure that file citations and line numbers are exactly correct using the tools available; if they are incorrect your comments will be rejected." |
458 | | - printf '%s\n' "with a concise justification and a confidence score between 0 and 1." |
459 | 493 | } > "$PROMPT_PATH" |
460 | 494 | fi |
461 | 495 |
|
|
0 commit comments