Code Review GPT is a NodeJS package that uses LLMs to provide feedback on code. It is designed to be used in a CI environment to provide feedback on pull requests.
- Node 18+ or Bun 1.0+
- Git
- Github or Gitlab CLI (optional for configure tool)
In the root of your git repository run:
bun install code-review-gpt
bun code-review-gpt configureIf you are running this tool in Gitlab, you will need to do some additional setup. You will need to create a access token in Gitlab and store it in your CI/CD variables to allow the bot access to you Gitlab account. Follow the steps below.
- Log in to your GitLab account.
- Go to your Repo settings by clicking on the repository, and selecting Settings -> Access Tokens.
- In this section, you can generate a new access token.
- Name your token something relevant and understandable ie.
CODE_REVIEW-GPT-TOKEN. Set the scope to beapionly. - Click the "Create personal access token" button. GitLab will generate the token and display it to you once. Make sure to copy this value, we are going to use it in the next step.
-
Navigate to the project where you want to add the code review bot.
-
In the left sidebar, click the Settings drop down, then click CI/CD
-
Scroll down to the Variables section and click the Expand button.This is where you can manage your CI/CD variables.
-
Create a new variable by clicking the Add Variable button in the CI/CD Variable table.
-
Paste your previously copied access token into the Value box. Name the variable
GITLAB_TOKEN. Under the Flags section, make sure to tick theMask variableoption.- [Un-tick the
Protect variableif your branches are not protected, otherwise this variable won't be availiable for the bot to use.]
- [Un-tick the
-
Save you changes. Now you can go ahead and run the following commands in you project directory.
bun install code-review-gpt
bun code-review-gpt configure --setupTarget=gitlabSee templates for example yaml files. Copy and paste them to perform a manual setup.
If you are running this tool in Azure DevOps, you will need to do some additional setup.
The code-reivew-gpt needs additional Git history available for affected to function correctly. Make sure Shallow fetching is disabled in your pipeline settings UI. For more info, check out this article from Microsoft doc.
You will need to create a personal access token in Azure DevOps and store it in your CI/CD variables to allow the bot access to your Azure DevOps account. Follow the steps below.
- Sign in to Azure DevOps: Go to the Azure DevOps portal and sign in to your account.
- Navigate to User Settings: Click on your profile picture in the top right corner and select "Security" from the dropdown menu.
- Generate Personal Access Token (PAT): In the Security page, select "Personal access tokens" and click on the "+ New Token" button.
- Configure Token Details: Provide a name for your token, choose the organization, and set the expiration date.
- Define Token Permissions: Specify the necessary permissions for the token based on the tasks you want to perform. For pipeline access, you might need to select "Read & manage" under "Build" and "Release."
- Create Token: Click on the "Create" button to generate the token.
- Copy Token: Copy the generated token immediately, as it will not be visible again.
- Add Token as YAML Pipeline Variable: Go to your Azure DevOps project, open the pipeline for which you want to use the PAT, and select "Edit."
- Navigate to Variables: In the pipeline editor, go to the "Variables" tab.
- Add New Variable: Add a new variable with a relevant name (e.g.,
API_TOKEN) and paste the copied PAT as the value. - Save Changes: Save the pipeline changes, ensuring that the PAT is securely stored as a variable.
- Use Variable in Pipeline: Modify your YAML pipeline code to reference the variable where needed, replacing hard-coded values with the variable (e.g.,
$(API_TOKEN)).
npm install code-review-gpt
npx code-review-gpt configure --setupTarget=azdevSee templates for example yaml files. Copy and paste them to perform a manual setup.
Code Review GPT works locally to review files staged for commit:
Run npm i code-review-gpt && npx code-review-gpt review in the root directory of a git repository.
Run npm i -g code-review-gpt to install the tool globally.
You can now run code-review-gpt review in the root directory of any git-enabled repository on your machine.
code-review-gpt review- Runs the code review on the staged files.code-review-gpt configure- Runs a setup tool to configure the application.
Run code-review-gpt --help to see all the options available.
or check out the code here
CUSTOM_LANGUAGE_MAP- JSON string containing custom file extension to language mappings. Example:{".mjs": "JavaScript", ".tsx": "TypeScript React"}. These mappings will be merged with the default language map.
When used in GitHub Actions, the review result is automatically saved to the review_result output variable. You can access this result in several ways:
- In the same job:
steps:
- name: Install and Run Code Review GPT
id: code_review
run: bun run review --ci=github
- name: Use Review Result
run: echo "${{ steps.code_review.outputs.review_result }}"