ReviewIt-AI
Tags: githubactions, codereview, openai, langchain
Published: 2024-03-04 23:43:37

A companion to help you write better code!


![Gemini Generated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/08sml2nbaqoxln3yrdng.png)

We all have been in this situation of going through the code and providing our inputs in improving it before it gets to be part of the larger system, but often things get lost in translation to bring forth the idea behind it and the way the reviewer sees it.

With the ever-increasing penetration of GenAI into our lives, it makes more sense to use it to our advantage to help out both the author and the reviewer.

## Welcome to ReviewIt-AI!

[ReviewIt-AI](https://github.com/sachs7/reviewit-ai) takes in your code, provides a summary of what it does, and suggests improvements if needed. It is based on `LangChain`, `OpenAI`, and `Streamlit`


A sample of it:


![ReviewIt-AI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2bcfhzaeqz1lr7kykfq.png)
ReviewIt-AI



![Sample-Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sc2ak40p31v3mgqpc8go.png)
ReviewIt-AI: Sample Code Review

Then a question arises, is this helpful on its own? Sure, if you just need a quick check on your code or need a quick summary of what the code does. But in the longer run, it will be more useful if it is part of your review process🌟 enter…

## ReviewIt-AI GitHub Actions

With a working example in place, we can focus on getting it part of a PR review process using GitHub’s Action. Let me share the [overview](https://github.com/sachs7/reviewit-ai-git-actions/tree/main) of it:

[GitHub Actions](https://docs.github.com/en/actions) are nothing but `workflows` that get triggered based on the event one defines in the `yml` file(s) of the workflows. In our case, we have the directories `actions` and `workflows` defined within the `.github` folder.

`Actions` are where we write/store scripts that will be called by the Workflows during execution, here we have `ai.py` and `reviewit.py`, as the file names suggest, each contains the code that does respective actions, and with `reviewit.py` being our main file, we will call `ai.py` in it and also, define what our review comments look like.

`Workflows` consists of `yml` files that call actions to be performed based on user-defined plans. In our case, it is to execute `actions` on every PR, summarize what the PR is trying to do, and provide an opinion on things to be improved with suggested code changes.

In the code base, you can find two PRs, one corresponds to a [code that can be improved](https://github.com/sachs7/reviewit-ai-git-actions/pull/31#issuecomment-1977448710), and the other one [better code](https://github.com/sachs7/reviewit-ai-git-actions/pull/32#issuecomment-1977451877)

## Overview of Workflow Code

```
name: ReviewIt-AI

on:
  pull_request:
    branches:
      - main
    types: [opened, synchronize]
```
The above code showcases the initial code, we name our workflow as `ReviewIt-AI` and we would like to execute this GitHub action for every PR.

```
jobs:
  review:
    runs-on: ubuntu-latest
    env:
      OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
      GIT_TOKEN: ${{ secrets.GIT_TOKEN}}
      PR_NUMBER: ${{ github.event.number }}
      REPO: ${{ github.repository }}
```
Next, we define the `jobs` that get triggered, one can create multiple `jobs` but here we just need one, we define which OS runs the code on and supply the required environment keys like `OpenAI`, `GitHub Token (PAT)` and we collect the `repo` and `pr_number`

```
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11.5"
          cache: "pip"
```
Next, we checkout the repo using `Python` GitHub Actions and provide the version we need.

```
      - name: Install dependencies
        run: |
          pip install -r requirements.txt --quiet
```
Install required dependencies

```
      - name: Run ReviewIt-AI
        id: review
        run: |
          echo $REPO
          echo $PR_NUMBER
          echo "python .github/actions/reviewit.py -r $REPO -p $PR_NUMBER"
          python .github/actions/reviewit.py -r $REPO -p $PR_NUMBER | tee output.txt
```
This is the section where we call the `actions` to be triggered, here, we call it to execute the `reviewit.py` that gets the PR contents and sends it to `OpenAI` to get a summary, improvements, and reviews and store the output in a file called `output.txt`, also, the output can be viewed in the console of this particular step.


![PR Comment Section](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdcqp9blp6f56wdkeh1t.png)

The final step in our process is to add it as a comment to our PR. In the previous step, we used `output.txt` file to store the results, so here we read it and pass it as a body of the PR.

That’s all we need!

## Things to remember

1. Make sure to generate the [PAT token](https://docs.github.com/en/enterprise-server@3.9/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)
2. Provide required permissions to it, like either to a specific repo or all repos and other fine-grained permissions
3. Go to the repository where you want to enable the GitHub Actions
4. In the repo Settings >> Actions >> provide necessary permissions for the workflows (check the ‘Workflow permissions’ section)
5. Add your `PAT token` as well as the `OPENAI_API_KEY` in the repo's secrets

**_Note: In the current repo, the secrets are dummies, so, the GitHub Actions on any new PRs will fail_**

Go ahead and clone the repo and have it part of your repo to get your PRs auto-reviewed!


## Sample Comments

Code that can be improved:


![code-to-be-improved](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77gipggfymc6kyl5yawh.png)


Better Code:


![Better-Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0f68wpadvd095lzfc9sn.png)


## Code Links

1. [ReviewIt-AI](https://github.com/sachs7/reviewit-ai)
2. [ReviewIt-AI GitHub Actions](https://github.com/sachs7/reviewit-ai-git-actions)