Back to directory
llnhnv avatar
llnhnv / ai-code-reviewer

ai-code-reviewer

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

4

Stars

1

Forks

0

Watchers

โ€”

License

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]

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!