An AI-powered code review application that integrates with GitHub pull requests to automatically analyze code changes using Google Gemini API.
- Connects to GitHub via Personal Access Token (PAT).
- Listens to pull request events (via GitHub Actions workflow).
- Fetches the diff/patch of PR changes.
- Sends the diff to Google Gemini API for AI-powered review.
- Prints AI suggestions for better code quality.
- Initialize a new GitHub repo (e.g.,
AI-code-review-demo). - Add a sample
main.pyfile (or any code file) to test with. - Clone the repo locally:
git clone https://github.com/<your-username>/AI-code-review-demo.git cd AI-code-review-demo `
-
Go to your GitHub account:
Profile > Settings > Developer Settings > Personal Access Tokens > Fine-grained Tokens -
Click Generate new token.
-
Select "Only select repositories" and choose the repository where you want to enable AI code review.
(This ensures the token is restricted to just your project and not all repos.) -
Under Repository permissions, grant the following:
- Contents β Read and Write (needed to fetch and comment on PRs)
- Pull requests β Read and Write (to post AI review comments on PRs)
- Workflows β Read and Write (so Actions can use the token)
-
(Optional) Under Account permissions, you can leave defaults unless your repo setup requires more.
-
Set an expiry date for security (recommended).
-
Generate the token and copy it immediately β GitHub will only show it once.
-
Save this token in your
.envfile as:GITHUB_PAT_TOKEN=ghp_yourGeneratedTokenHere
-
Go to Google AI Studio.
-
Create an API key.
-
Save it in
.envfile.
In the project root, create a .env file:
GITHUB_PAT_TOKEN=ghp_yourgithubtoken GEMINI_API_KEY=AIzaSyDxxxxxxx
To write an automated comment on the PR use this command on git bash:
curl -X POST \
-H "Authorization: token YOUR_GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/OWNER/REPO/issues/PR_NUMBER/comments \
-d '{"body":"π€ AI Code Review Suggestion:\n\nYour review text goes here"}'Inside your project folder:
pip install requests python-dotenv google-generativeai
βββ AI_review/
β βββ review_pr.py # Main script to fetch PR diff and send to Gemini
β βββ script.py # Additional utilities
βββ .env # Environment variables
βββ .gitignore
βββ README.md
βββ main.py # Sample file
Inside .github/workflows/code_review.yml:
name: AI Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install dependencies
run: pip install requests python-dotenv google-generativeai
- name: Run AI Code Review
env:
GITHUB_PAT_TOKEN: ${{ secrets.GITHUB_PAT_TOKEN }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
run: python AI_review/review_pr.pyTo test without workflow:
python AI_review/review_pr.py
It will:
-
Fetch latest PR diff.
-
Send to Gemini.
-
Print AI review in console.
{ "status": "success", "pull_request": 12, "ai_review": "Consider adding error handling for file operations in main.py..." }
-
If you used a dummy webhook earlier, you don't need it now since GitHub Actions workflow handles PR events.
-
Always keep
.envand tokens secure (never commit them). -
For merging branches:
git checkout main git merge branch-2 git push origin main
This project demonstrates end-to-end AI integration with GitHub PRs, automating code reviews using Gemini API.