DEV Community

Louis Liu
Louis Liu

Posted on

Let AI Do Code Review For You

Sometimes, I make some stupid mistakes in the code review process, such as typos, wrong variable names, etc. These mistakes remain even though I have used some code editor plug-ins and done a self-code review before sending the code to my colleagues.

TL;DR

Let AI do Code Review for you.

The AI Code Review Tool

I developed this AI code review tool. It can be integrated with GitHub Actions, once you create a pull request it will automatically review your code and provide comments.

How to use it

You can use ChatGPT or the ChatGPT hosted on Azure as the engine. The company I work for has a strict AI policy; we have a self-hosted ChatGPT service on Azure to keep confidential information internally. Therefore, I'm using a ChatGPT service deployed on Azure in the following example.

First, you need to acquire an API key to call the ChatGPT service. In your GitHub repository, Go to Settings -> Secrets and variables -> Actions -> New repository secret. Create a repository secret named AZURE_OPENAI_API_KEY (use OPENAI_API_KEY if you're using OpenAI) and fill it with your API key.

set api key

Create a GitHub workflow and add the configurations to .github/workflows/pr-code-review.yml:

name: Code Review
permissions:
  contents: read
  pull-requests: write
on:
  pull_request:
    types: [opened, synchronize]
jobs:
  code-review:
    runs-on: ubuntu-latest
    steps:
      - uses: Louis-7/ai-code-review@v0.4
        env:
          ENGINE: 'azureopenai'
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          AZURE_OPENAI_BASE_URL: [base_url]
          AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }}
          AZURE_API_VERSION: [api_version]
          AZURE_DEPLOYMENT: [deployment_name]
          PATH_TO_EXCLUDE: action/**/*, package-lock.json, package.json
          MAX_PATCH_PER_FILE: 2000
          MAX_FILE_PER_PR: 20
Enter fullscreen mode Exit fullscreen mode

Replace base_url, api_version, and deployment_name based on your environment.

It will be easier if you're using the OpenAI 👉👉 open-ai-example.yml

Now, you can create a pull request in your repo, and AI will help you review the code.

open pr example

Features

The tool provides various options to let you customize the code review experience. You can set them in your repository settings.

You can set the PATH_TO_EXCLUDE, MAX_PATCH_PER_FILE, and MAX_PATCH_PER_PR to prevent AI from reviewing some large pull requests. Keeping your pull requests in good shape is a good practice.

You can also customize the prompt through CUSTOMIZED_PROMPT.

Get a full list of settings here.

set environment variables

Self-host solution

The tool is built on Probot. You can deploy it as a GitHub bot to provide a better experience.

Welcome to join me in improving the tool. My initial motivation for doing this was to use AI tools within the company. Now, I'm thinking maybe I can plug some other LLM into this tool. If you're interested, welcome to join me.

Top comments (2)

Collapse
 
pavelee profile image
Paweł Ciosek

Great job 👏

It would be great to allow extend prompt

Here I extracting prompt for other readers, you can test it in your local files using copilot or other tool you use

You're a code reviewer in a software development team. Your responsibility is:
          - read through the code patch I give you.
          - give suggestions for improvements.
          - identify bugs and risks in the code patch.
          - only response with code review comments, don't rely the code patch again.

        Give me your comments in ${this.LANGUAGE}.

        Below is the code patch:
Enter fullscreen mode Exit fullscreen mode
Collapse
 
radek_beran_c7b0c7f09a9e4 profile image
Radek Beran

Good job. There is a small typo in the extracted prompt: It should be: "don't reply the code patch again".