This GitHub Action automatically performs a code review for incoming Pull Requests using the ChatGPT (O1) model via the OpenAI API. It reads your pre-defined coding rules, examines diffs of changed files, generates inline comments, and posts them on GitHub PRs. If there are no suggestions, it automatically approves the Pull Request.
-
Pull Request Info
When a Pull Request (PR) event triggers this Action, it uses PyGithub to:- Retrieve the PR details (title, body, diffs).
- Check whether the current bot user is requested for a review (or if a recent review has already been made).
-
Diff Parsing
It collects the patch (diff) of each modified file in the PR and processes it using the unidiff library. -
Coding Rules
The script looks for a.github/coding-rules.mdfile (in your repo) that contains your project's or organization's coding guidelines. -
Sending to ChatGPT
Using the OpenAI API (ChatGPT/O1 model):- It constructs a prompt that includes:
- The coding rules
- The PR body
- The diff contents
- The model responds with JSON-formatted comments (inline suggestions, warnings, etc.).
- It constructs a prompt that includes:
-
Posting Review Comments
The returned comments are posted back to the PR using PyGithub.
If there are no AI suggestions, it automatically approves the PR.
- Create a GitHub Workflow
In your repository, create a YAML workflow file (e.g.,.github/workflows/ai-code-reviewer.yml) with the following example content:
name: AI Code Reviewer
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review, review_requested]
permissions:
contents: write
pull-requests: write
jobs:
ai-code-reviewer:
name: ai-code-reviewer
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run ChatGPT Code Review
uses: team-monolith-product/ai-code-reviewer@main
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
PR_NUMBER: ${{ github.event.number }}
SYSTEM_PROMPT: Always answer in Korean.
-
Add Your Coding Rules
- Create a file in your repo at
.github/coding-rules.md. - Write down your coding standards, best practices, etc.
- Create a file in your repo at
-
Secrets and Environment
GITHUB_TOKEN: Any GitHub token with appropriate permissions.GITHUB_TOKENis automatically provided by GitHub Actions. If you have a bot account, you can use its token. It is usually better, because you can re-request the review from the bot account.OPENAI_API_KEY: Your OpenAI API key (stored as a secret in your repo).
Enjoy automated code reviews with ChatGPT!