# AI Code Review Service A FastAPI-based service that automatically reviews Bitbucket Pull Requests using AI (Google Gemini 1.5 Flash) and provides feedback via API or email. ## How it Works 1. **Webhook**: Bitbucket sends a POST request to this service whenever a Pull Request is created or updated. 2. **Diff Fetching**: The service fetches the code diff for the Pull Request from Bitbucket's API. 3. **Filtering**: The diff is filtered to only include relevant code files (e.g., `.py`, `.js`, `.php`). Non-code files like `.md` or `.env` are excluded to save costs. 4. **AI Analysis**: The filtered diff is sent to Google Gemini with a custom prompt focused on Syntax, DRY principles, Clean Code, Performance, and Security. 5. **Output**: The generated review is returned directly via API (for manual requests) or sent to the PR author via email (for webhooks). ## Setup Instructions ### 1. Environment Configuration Create a `.env` file in the root of `code_review_service/` (refer to `.env.example`): - `BITBUCKET_EMAIL` & `BITBUCKET_TOKEN`: Your Bitbucket email and App Password. - `GEMINI_API_KEY`: Your Google Gemini API key. - `GEMINI_MODEL`: Model name (default: `gemini-2.5-flash`). - `FALLBACK_RECIPIENT`: Email address to use if the PR author's email is not found. ### 2. Configure Bitbucket Webhook Follow these steps to connect your repository to this service: 1. Go to your repository on Bitbucket. 2. Click on **Repository settings** (bottom left). 3. Select **Webhooks** under the Workflow section. 4. Click **Add webhook**. 5. Fill in the details: - **Title**: AI Code Review - **URL**: `https://api-service-uat.creditfair.in/webhook/bitbucket` (or your appropriate base URL) - **Status**: Active - **Triggers**: Choose **Repository events** -> **Pull Request** -> **Created** and **Updated**. 6. Click **Save**. ### 3. Manual Review via API (Staging/Testing) You can trigger a review manually without setting up a webhook by calling the review endpoint. **Endpoint**: `POST /api/v0/review` **Body**: ```json { "url": "https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/pullrequests/{pr_id}/diff" } ``` **Testing via Swagger UI**: 1. Run the app: `python app.py` 2. Open `http://127.0.0.1:8006/docs` in your browser. 3. Use the **Try it out** button on the `/api/v0/review` endpoint. ### 3. Local Development / Testing ```powershell # Activate venv .\venv\Scripts\activate # Install dependencies pip install -r requirements.txt # Run service python app.py ``` ### 4. Deployment - This service needs to be accessible from the internet for Bitbucket to send webhooks. - Use a tool like `ngrok` for local testing or deploy to a public server (AWS, etc.). - Ensure port `8006` (or your configured port) is open in the firewall. ## Project Structure - `app.py`: FastAPI application and webhook endpoint. - `src/utils/bitbucket_client.py`: Bitbucket API interactions. - `src/utils/ai_reviewer.py`: LangChain and OpenAI logic. - `src/utils/email_notifier.py`: Email sending logic using `core_utilities`. ## Cost Optimization - **Model**: Uses `gemini-2.5-flash` which is highly cost-effective with a large context window. - **Diff Only**: Only the code changes (diff) are sent to the AI, not the entire files. - **File Filtering**: Only specific extensions ([.py, .js, .php, .html, .css, .ts]) are processed. - **Concise Response**: The prompt enforces a short, high-impact summary to reduce token usage and improve readability.