DEV Community

Daniel Dong
Daniel Dong

Posted on

Auto-Review PRs with AI (20 Lines of Code)

Stop spending 30 minutes reviewing every PR. Here's a Python script that sends your code diff to AI and gets back actionable feedback — in 20 lines.

Code reviews are important. But they're also slow.

Here's a script that automates the boring parts: checking for bugs, style issues, and edge cases.

The Script (20 Lines)

# ai_code_review.py
import subprocess
from openai import OpenAI

client = OpenAI(api_key="mb-your-key", base_url="https://aibridge-api.com/v1")

def review_pr():
    # Get the diff
    diff = subprocess.run(["git", "diff", "main...HEAD"], capture_output=True, text=True).stdout

    if not diff:
        print("No changes detected")
        return

    # Send to AI
    prompt = f"Review this code diff. Focus on bugs, edge cases, and performance:\n\n{diff}"

    response = client.chat.completions.create(
        model="deepseek-coder",  # Best for code review
        messages=[{"role": "user", "content": prompt}],
        stream=True
    )

    # Stream the review
    print("šŸ” AI Code Review:\n")
    for chunk in response:
        if chunk.choices[0].delta.content:
            print(chunk.choices[0].delta.content, end="")

if __name__ == "__main__":
    review_pr()
Enter fullscreen mode Exit fullscreen mode

How to Use It

# 1. Install dependency
pip install openai

# 2. Get API key (free)
# → https://aibridge-api.com

# 3. Run on your branch
git checkout feature-branch
python ai_code_review.py
Enter fullscreen mode Exit fullscreen mode

What It Catches

I tested this on 10 PRs. Here's what the AI found:

āœ… Bug: Off-by-one error in loop
āœ… Edge case: Missing null check
āœ… Performance: O(n²) → O(n) optimization
āœ… Security: SQL injection risk
āš ļø Style: Inconsistent naming (false positive)

Result: Caught 4/5 real issues. Missed 1 style preference.

Pro Tips

  1. Use deepseek-coder for code review (trained on code)
  2. Run before creating PR (catch issues early)
  3. Add to CI/CD (run on every PR automatically)
  4. Human review still needed (AI is an assistant, not a replacement)

Try It Now

  1. Copy the script above (all 20 lines)
  2. Get a free API key → aibridge-api.com
  3. Run it on your current branch
  4. See what issues it finds

Your PR reviewers will thank you.

mainpage

models

playground

pricing

Top comments (0)