This repo demonstrates how to use OpenAI's ChatGPT to automatically review pull requests in a GitHub repository. It leverages GitHub Actions, the OpenAI API, and a TypeScript script to analyze code changes and post a summary directly to the PR.
.
├── code
│ └── example.ts # Sample TypeScript function (used to test the review)
├── scripts
│ └── code-review
│ ├── index.ts # The main TypeScript review script
│ ├── package.json # Local dependencies for the review script
│ ├── tsconfig.json # TypeScript config
│ └── yarn.lock
└── workflows
└── chatgpt-review.yml # GitHub Actions workflow that runs the review
Whenever a pull request is opened or updated:
- GitHub Actions triggers the
chatgpt-review.ymlworkflow. - The action checks out the code and runs a TypeScript script using
ts-node. - The script:
- Fetches the code diffs in the PR
- Sends them to OpenAI’s ChatGPT API with a review prompt
- Posts the review summary as a comment on the pull request
cd .github/scripts/code-review
yarn installGo to your repo → Settings → Secrets and variables → Actions → New repository secret
OPENAI_API_KEY— your OpenAI API keyGITHUB_TOKEN— default GitHub token (already available in Actions, but required in the script)
Note: The default
GITHUB_TOKENwill work only for internal PRs. For forked PRs, you'll need to use a GitHub App or bot account.
The file .github/workflows/chatgpt-review.yml should already exist. It’s triggered automatically on:
on:
pull_request:
types: [opened, synchronize]You can edit code/example.ts and open a pull request. The ChatGPT bot will analyze the code and leave a comment on the PR with suggestions, improvements, or warnings.
To allow the GitHub Action to post comments, the workflow must request permissions:
permissions:
contents: read
pull-requests: writeThis is already included in the chatgpt-review.yml.
Want a stricter review? Friendlier tone? Edit the prompt in:
// .github/scripts/code-review/index.ts
const prompt = `
You're a senior software engineer reviewing a code diff.
Please:
- Spot logic or syntax errors
- Identify any risky or suspicious changes
- Recommend improvements
- Be concise but helpful
Code diff:
${codeDiffs}
`;You can test the review script locally (without GitHub) like this:
cd .github/scripts/code-review
OPENAI_API_KEY=your_key_here GITHUB_TOKEN=your_token_here yarn reviewYou’ll need to mock
github.contextor pass in data for local testing.
- Support forked PRs with GitHub App
- Add inline comments instead of summary
- Limit file types (e.g. only
.tsor.js) - Improve prompt templates
MIT (add it in package.json too if you want to avoid warnings)