Skip to main content

AI code reviewer

Project description

Git Commit File Reviewer

This project provides a Python script that retrieves files modified in the latest Git commit and displays the updated lines for each file, along with a review of the code with the updates. It is designed to be used in Continuous Integration (CI) environments, such as GitHub Actions and GitLab CI.

Features

  • Get a list of files modified in the latest commit.
  • Retrieve updated lines for each modified file.
  • Review code changes using the LLaMA model from Ollama.
  • Easy integration with CI/CD pipelines.
  • New: Automatically reviews changes with the LLaMA 3.2 model after retrieving updated lines.
  • New: GitHub CLI (gh) for adding comments to pull requests

Prerequisites

  • Docker
  • Git
  • Python 3.11 or higher (for local testing)
  • New: GitHub CLI (gh) for adding comments to pull requests

Environment Variables

To run this project, you need to set the following environment variables:

export OLLAMA_API_BASE_URL="https://ollama.kabla.xyz"
export IGNORED_FILES="['README.md', '.gitignore', 'requirements.txt']"
export OLLAMA_MODEL="codellama"

Setup

  1. Clone the repository:

    git clone <repository-url>
    cd <repository-directory>
    
  2. Build the Docker image:

    docker build -t git-commit-file-tracker .
    
  3. Run the Docker container:

    docker run --rm git-commit-file-tracker
    

Usage in CI/CD

GitHub Actions

You can use the following example workflow to integrate this script into your GitHub Actions:

name: CI

on:
  push:
    branches:
      - main

jobs:
  track-commits:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Build and run Docker container
        run: |
          docker build -t git-commit-file-tracker .
          docker run --rm git-commit-file-tracker

GitLab CI

For GitLab CI, you can add the following configuration to your .gitlab-ci.yml file:

stages:
  - track

track_commits:
  stage: track
  image: python:3.11
  before_script:
    - apt-get update && apt-get install -y git
  script:
    - python main.py

Python Script: main.py

The main.py script is responsible for fetching pull request data, parsing the changes, and generating suggestions for improvements. It utilizes the LLaMA model for code review and integrates with the GitHub CLI to add comments to pull requests

Key Functions

  • init_cache_db: Initializes a SQLite database for caching.
  • get_pull_request: Fetches pull request data from Git.
  • parse_diff: Parses the Git diff output to extract modified lines.
  • propose_updates: Generates suggestions for modified lines using the LLaMA model.
  • add_comments_to_pr: Adds comments to the pull request with suggestions.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

name: LLM Code Review

on:
  pull_request:
    types: [opened, synchronize]

jobs:
  code_review:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.x'

    - name: Install dependencies
      run: |
        pip install requests langchain_core

    - name: Run code review script
      env:
        OLLAMA_MODEL: ${{ secrets.OLLAMA_MODEL }}
        OLLAMA_API_BASE_URL: ${{ secrets.OLLAMA_API_BASE_URL }}
        IGNORED_FILES: ${{ secrets.IGNORED_FILES }}
      run: script.python

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai-reviewer-1.0.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ai_reviewer-1.0.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file ai-reviewer-1.0.0.tar.gz.

File metadata

  • Download URL: ai-reviewer-1.0.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.5

File hashes

Hashes for ai-reviewer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f2913dbb23a54fdac7753da70d7a09c17504d3fde771e206f5d5b118275f2b46
MD5 884ba55afb42ac50c607f71d8868976f
BLAKE2b-256 bc84e7f90f3e13c6c0ea83f623a3dfd188e96f4b4b360ddd202c734ade065f5d

See more details on using hashes here.

File details

Details for the file ai_reviewer-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ai_reviewer-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.5

File hashes

Hashes for ai_reviewer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5de49ae2e1c079f4a07d6e97ec7284e0e63264ac96ca8597d8da82f02023f181
MD5 4e130ed437161adeca1e5729dc121a48
BLAKE2b-256 d40218ca9fdc454ed09d23f89cffe0d1fc27eb51b852ba032df6541eb146f5e0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page