Building and shipping features fast is the need of the hour. However, maintaining code quality is paramount. Code reviews are critical to every software development process, ensuring that every line of code meets the standards and adheres to the industry’s best practices.
Nevertheless, manual code reviews can be time-consuming and prone to human error. Today, a handful of AI tools are available that automate code reviews and reduce manual effort.
However, creating your own AI code reviewer can provide a tailor-made solution with deeper integration into your workflow. In the three-part blog post series, I’ll show you how to develop your own AI-powered code review system using GPTScript to enhance the consistency and efficiency of your code reviews.
In this first part, we’ll create an AI code review GPTScript that will run in your environment and perform code reviews.
Let’s Build GPTReview
At the heart of GPTReview is GTPScript, a scripting language that automates your interactions with an LLM using natural language. Its natural language syntax is easy to understand and implement. One of the highlights of GPTScript is its ability to mix natural language prompts with traditional scripts, making it extremely flexible and versatile.
Use Case
Before we build GPTReview, let us understand what we will achieve by building this. Our AI-based code reviewer will scan a GitHub repository for open pull requests. It will take the latest pull request, analyze the changed files only, and share its review comments.
As mentioned above, this will be a three-part series:
- Part 1: Running GPTReview locally
- Part 2: Running GPTReview using GitHub actions
- Part 3: Running GPTReview using Jenkins
In this first part, we’ll look at creating the GPTReview AI code review tool and run it locally through CLI. We will create a chat-based assistant that will analyze the pull request and answer any follow-up questions we may have.
Note: Make sure you’re using the latest version of GPTScript to be able to use the chat interface.
Pre-requisites
- An OpenAI API Key.
- GitHub CLI installed on your system
- GPTScript configured.
Writing the GPTScript
The first step is to create the GPTScript. Since GPTScript is written primarily in natural language, writing the script to perform code review is easy. Below is the gptreview.gpt file:
Name: Code Reviewer
Description: A tool to help you perform code review of open PRs
Context: learn-gh
Tools: sys.exec, sys.http.html2text?, sys.find, sys.read, sys.write
Args: PR_URL: The GitHub PR_URL
chat:true
You have the gh cli available to you. Use it to perform code review for a pr.
Perform the following steps in order:
1. Ask the user for the ($PR_URL) and save it.
2. Identify the files changed in the pull request ($PR_URL) using the pr number and perform a diff.
1. Analyze the complete code of each identified file and perform a detailed line by line code review.
2. Repeat the process for each changed file in the pr.
3. Share your review comments separately for each file.
4. In a new line write "Code: Approved" or "Code: Require Changes" based on the review.
---
Name: learn-gh
Description: A tool to help you learn gh cli
#!/usr/bin/env bash
echo "The following is the help text for the gh cli and some of its sub-commands. Use these when figuring out how to construct new commands. Note that the --search flag is used for filtering and sorting as well; there is no dedicated --sort flag."
gh --help
gh repo --help
gh pr --help
gh pr checkout --help
gh pr diff --help
Let us understand what the script does:
- The script takes a "–PR_URL" parameter, which is the URL for the pull request for which you want to perform the code review.
- Notice that we have provided chat:true to enable chat-based assistant for this file.
- Using the GitHub CLI, It identifies the files changed in the pull request and performs a code review for the changed file.
You can find this GPTScript in our examples folder.
Executing the GPTScript
To execute this script, you must first configure the “OPENAI_API_KEY” environment variable.
We will execute the GPTScript using as mode.
Execute the script using the following command:
gptscript -- ui gptreview.gpt
It starts a local node server with the chat UI enabled and opens the same in a new browser window.
It asks for the pull request URL, and we provide that. Click on the Start chat button to start the chat. GPTScript analyzes the pull request based on the script provided and shares the response.
When you run the script for the first time, it will ask whether you want to execute a certain step. You can click the Allow button to ignore it once or press the Allow All button to allow all requests.
It will perform the analysis on the files changed in the PR and share the result.
Since we’re running this in chat mode, we can also ask follow-up questions. Let us ask it to give us the complete file with the changes suggested and proper comments.
Using a few lines of natural text, we were able to build GPTReview and perform code reviews for PRs. You can provide any open pull request URL to perform a code review or tweak the gpt file to perform a review based on your organization’s set guidelines.
Summary
We all agree that manual code reviews are passed and intelligent AI code reviewers are in. They not only help quicken the process but also reduce human errors. While tools are available to do this, in this post, we looked at how to build one using GPTScript.
GPTReview is a simple CLI application that allows you to perform code reviews for any repository. It’s simple, fast, and convenient. In the next blog post, we’ll integrate this into a CI/CD pipeline using GitHub actions.
Until then, check out GPTScript and try building AI-enabled applications with ease. If you’re new here or need guidance, join the GPTScript Discord server and get all your queries answered.
This tutorial is part of a series on GPTReview, an AI code reviewer built on GPTScript. Check out parts 2 & 3 below.