Automated code review system for Bitbucket Cloud pull requests using Claude Opus 4.5 via Anthropic API.
- Webhook-based PR detection for
feature/*andfix/*branches - Manual PR review trigger via CLI tool (for missed PRs when server is offline)
- Automatic repository cloning and diff extraction
- AI-powered code review using Claude Opus 4.5 via Anthropic API
- Flutter/Dart focused review criteria (Clean Architecture, BLoC/Cubit patterns)
- Automatic PR comment posting with review results
- Multi-language support (Turkish & English)
- Email notifications with PR links (Gmail OAuth2)
- Dual mode: API (default) or CLI
Bitbucket PR → Webhook → FastAPI Server → Clone Repo → Claude API (Opus 4.5) → PR Comment + Email
- Python 3.10+
- Anthropic API Key (for Claude API access)
- ngrok or cloudflared (for webhook tunneling)
- Bitbucket Cloud account with API token
- (Optional) Claude CLI for CLI mode
git clone https://github.com/yourusername/claude-pr-reviewer.git
cd claude-pr-reviewerpython3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activatepip install -r requirements.txtcp .env.example .envEdit .env with your credentials:
BITBUCKET_EMAIL=your-email@example.com
BITBUCKET_API_TOKEN=your-api-token
WEBHOOK_SECRET=your-webhook-secret
# Claude Configuration
CLAUDE_USE_CLI=false
ANTHROPIC_API_KEY=sk-ant-api03-your-api-key
# Email notifications (optional - run setup script first)
EMAIL_ENABLED=true
EMAIL_PROVIDER=gmail
EMAIL_SENDER_ADDRESS=your-email@gmail.com
EMAIL_OAUTH_CLIENT_ID=your-client-id.apps.googleusercontent.com
EMAIL_OAUTH_CLIENT_SECRET=your-client-secret
EMAIL_OAUTH_REFRESH_TOKEN=your-refresh-token
EMAIL_FIXED_RECIPIENT=team-lead@company.com- Go to Anthropic Console
- Sign in or create an account
- Navigate to API Keys
- Click Create Key
- Copy the API key (starts with
sk-ant-api03-) - Add to
.env:ANTHROPIC_API_KEY=sk-ant-api03-your-api-key
Note: The system uses Claude Opus 4.5 model by default for high-quality code reviews.
If you prefer to use Claude CLI instead of API:
-
Install Claude CLI:
npm install -g @anthropic-ai/claude-code
-
Authenticate:
claude auth login
-
Set in
.env:CLAUDE_USE_CLI=true
python main.pyngrok http 8000- Go to Repository Settings → Webhooks
- Add webhook:
- URL:
https://your-ngrok-url.ngrok-free.app/webhook - Secret: Same as
WEBHOOK_SECRETin.env - Triggers: Select both:
Pull Request: CreatedPull Request: Updated
- URL:
- Go to Personal Settings → API tokens
- Create token with scopes:
read:repository:bitbucketread:pullrequest:bitbucketwrite:pullrequest:bitbucket
Email notifications send a brief message with a PR link after each review is completed using Gmail API with OAuth2 authentication.
Run the interactive setup script to configure Gmail OAuth2:
python tools/setup_gmail_oauth.pyThis script will:
- Ask for your Google Cloud Console Client ID and Client Secret
- Open a browser for Google authorization
- Generate a refresh token
- Provide configuration to add to your
.envfile
If you prefer manual setup:
- Go to Google Cloud Console
- Create a new project or select an existing one
- Project name: "PR Review Automation" (or any name)
- In Google Cloud Console, go to APIs & Services → Library
- Search for "Gmail API"
- Click Enable
- Go to APIs & Services → Credentials
- Click Create Credentials → OAuth client ID
- If asked, configure OAuth consent screen:
- User Type: External
- App name: "PR Review Bot"
- Add your email as developer contact
- Save and continue
- Back to credentials creation:
- Application type: Desktop app
- Name: "PR Review Bot"
⚠️ IMPORTANT: Add Authorized redirect URIs:http://localhost:8080/http://localhost:8080
- Click Create
- Copy the Client ID and Client Secret (you'll need these)
Run the setup script:
python tools/setup_gmail_oauth.pyOr manually use Google OAuth Playground:
- Go to OAuth 2.0 Playground
- Click settings icon (⚙️) → Check "Use your own OAuth credentials"
- Enter your Client ID and Client Secret
- In Step 1: Select
https://www.googleapis.com/auth/gmail.send - Click "Authorize APIs" and sign in
- In Step 2: Click "Exchange authorization code for tokens"
- Copy the Refresh token
Add these to your .env:
EMAIL_ENABLED=true
EMAIL_PROVIDER=gmail
EMAIL_SENDER_ADDRESS=your-email@gmail.com
EMAIL_OAUTH_CLIENT_ID=your-client-id.apps.googleusercontent.com
EMAIL_OAUTH_CLIENT_SECRET=your-client-secret
EMAIL_OAUTH_REFRESH_TOKEN=your-refresh-token
EMAIL_FIXED_RECIPIENT=team-lead@company.comSet EMAIL_ENABLED=false in .env to disable email notifications.
"400: redirect_uri_mismatch" error:
- Go to Google Cloud Console → Credentials
- Click on your OAuth Client ID
- Under "Authorized redirect URIs", add:
http://localhost:8080/http://localhost:8080
- Click Save and try again
"OAuth2 authentication failed":
- Verify Client ID and Client Secret are correct
- Ensure Gmail API is enabled in Google Cloud Console
- Check refresh token is valid (regenerate if needed)
"Invalid grant" error:
- Refresh token may have expired
- Run
python tools/setup_gmail_oauth.pyagain to get a new token
Email not received:
- Check spam/junk folder
- Verify
EMAIL_FIXED_RECIPIENTaddress is correct - Check server logs for error messages
- Ensure sender email matches the Google account used for OAuth
"Access blocked" during authorization:
- Your app is not verified by Google (expected for personal use)
- Click "Advanced" → "Go to [Your App Name] (unsafe)"
- This is safe if you created the app yourself
Note: Email failures do not block the PR review process. If email sending fails, the error is logged and the review continues normally.
├── main.py # Entry point (server)
├── src/
│ ├── server.py # FastAPI webhook handler
│ ├── pr_processor.py # PR review orchestrator
│ ├── bitbucket_client.py # Bitbucket API client
│ ├── git_operations.py # Git operations
│ ├── claude_reviewer.py # Claude CLI integration
│ ├── trigger_review.py # Manual PR review trigger CLI
│ └── utils/
│ ├── retry.py # Retry decorator
│ └── notifications.py
├── config/
│ ├── logging_config.py
│ └── review_prompts.py
└── requirements.txt
If the server is offline when a PR is created, you can manually trigger the review using the CLI tool:
python -m src.trigger_review --workspace <workspace> --repo <repo-slug> --pr-id <pr-id>Example:
python -m src.trigger_review --workspace mycompany --repo mobile-app --pr-id 123Short form:
python -m src.trigger_review -w mycompany -r mobile-app -p 123Requirements:
- Environment variables must be set (
BITBUCKET_EMAIL,BITBUCKET_API_TOKEN) - The tool will fetch PR information from Bitbucket API and trigger the same review process
Help:
python -m src.trigger_review --help- PR is created or updated from
feature/*orfix/*todevelopment - Bitbucket sends webhook to your server
- Server clones the repository to temp directory
- Extracts diff for Dart files
- Sends diff to Claude Opus 4.5 via Anthropic API for review
- Posts review comment back to PR
- Sends email notification with PR link
- Cleans up temp directory
- Run
python -m src.trigger_reviewwith PR information - Tool fetches PR details from Bitbucket API
- Same review process as webhook-based flow
- Useful for missed PRs when server was offline
MIT