Skip to content

πŸ” Automated GPT-4-powered code reviews for GitLab Merge Requests. Inline comments, auto-fix suggestions, feedback dashboard, and CI/CD integration

Notifications You must be signed in to change notification settings

llnhnv/ai-code-reviewer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

GitLab AI Code Reviewer

GitLab Demo

πŸ–Ό Architecture Flow

flowchart TD
    A[Developer opens Merge Request] --> B[CI triggered]
    B --> C[Run AI Review Job]
    C --> D[Flask API: Receives MR ID & Project ID]
    D --> E[Fetch diff & description from GitLab API]
    E --> F[Send to OpenAI GPT-4 API]
    F --> G[Result: pass/fail + comments]
    G --> H[Post comments to GitLab MR]
    G --> I[Log result into Postgres]
    I --> J[Dashboard UI with Chart.js]
Loading

CI β†’ FastAPI β†’ GPT β†’ GitLab comment + DB log β†’ Dashboard

✨ Features

  • βœ… Auto-fetch MR diff & description
  • πŸ€– GPT-4-based code review [OpenAI / Github Models]
  • πŸ’¬ Inline comments to GitLab MR
  • 🧩 GitLab CI/CD ready
  • 🏷️ Adds badge label ai-reviewed:pass|fail
  • πŸ“₯ Logs feedback rating (1–5) to DB
  • πŸ› οΈ Optional: Auto-fix patch block suggestion

πŸ“¦ Project Structure

β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api.py 
β”‚   β”œβ”€β”€ main.py                # Main script for review flow
β”œβ”€β”€ config/                 # Static files and charts
β”œβ”€β”€ docker/                   
β”‚   β”œβ”€β”€ Dockerfile             # Dockerized deployment
β”‚   └── nginx.conf             # Proxy for Flask
β”œβ”€β”€ fine-tune/                
β”‚   β”œβ”€β”€ train_code.py          # Fine-tune CodeT5/StarCoder (optional)
β”‚   └── train.json             # Training samples
β”œβ”€β”€ infra/
β”‚   β”œβ”€β”€ .gitlab-ci.yml         # GitLab CI job integration
β”‚   └── docker-compose.yml     # Local/dev deployment
└── README.md                  # Project overview

βš™οΈ Usage

1. Install dependencies

pip install -r api/requirements.txt

2. Set environment variables

export GITLAB_TOKEN=your_token
export OPENAI_API_KEY=your_openai_key
export CI_PROJECT_ID=your_project_id
export CI_MERGE_REQUEST_IID=your_mr_iid

3. Run the review script

python api/main.py

This will:

  • Fetch diff and description
  • Send to OpenAI
  • Post comments to GitLab
  • Log to database

πŸš€ GitLab CI Integration

Example .gitlab-ci.yml job:

stages:
  - ai_review

auto_review:
  stage: ai_review
  image: python:3.10
  script:
    - pip install -r api/requirements.txt
    - python api/main.py
  only:
    - merge_requests

πŸ“Š Dashboard (Optional)

Flask + Jinja2 + Chart.js based UI

Features:

  • MR history
  • PASS/FAIL filter
  • Comments per MR
  • Excel/PDF export
  • Submit feedback rating

Feedback Form:

<form method="POST">
  <label for="rating">Was the AI suggestion helpful?</label>
  <select name="rating">
    <option value="5">Excellent</option>
    <option value="4">Good</option>
    <option value="3">Average</option>
    <option value="2">Poor</option>
    <option value="1">Wrong</option>
  </select>
  <button type="submit">Submit</button>
</form>

Useful for building fine-tuning dataset


🧠 Advanced Extensions

πŸ›  Auto-fix Patch Suggestion

Append this to your prompt:

If fixable, include a "fix" field:
--- path/to/file.ext
+++ path/to/file.ext
@@ -42,7 +42,7 @@
- old code
+ new code

Use GitLab markdown to render in MR comment.

⭐ Feedback Rating API

@app.route("/feedback", methods=["GET", "POST"])
def feedback():
    if request.method == "POST":
        rating = int(request.form["rating"])
        mr_id = request.form["mr_id"])
        cur = conn.cursor()
        cur.execute("UPDATE ai_reviews SET feedback_rating = %s WHERE mr_iid = %s", (rating, mr_id))
        conn.commit()
        return "Thank you!"
    return render_template("feedback_form.html")

🏷 Add GitLab MR Label

def add_label(label):
    url = f"{GITLAB_API}/projects/{PROJECT_ID}/merge_requests/{MR_IID}/labels"
    headers = {"PRIVATE-TOKEN": GITLAB_TOKEN}
    payload = {"labels": label}
    requests.put(url, headers=headers, json=payload)

# Usage:
add_label("ai-reviewed:pass")

πŸ“Œ Notes

  • Ensure GitLab token has api scope
  • OpenAI usage may incur cost
  • Prompt/model can be adjusted to suit context

Feel free to fork, contribute, or open issues!

About

πŸ” Automated GPT-4-powered code reviews for GitLab Merge Requests. Inline comments, auto-fix suggestions, feedback dashboard, and CI/CD integration

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published