A companion to help you write better code!
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 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: 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 of it:
GitHub 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, and the other one better code
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.
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
- Make sure to generate the PAT token
- Provide required permissions to it, like either to a specific repo or all repos and other fine-grained permissions
- Go to the repository where you want to enable the GitHub Actions
- In the repo Settings >> Actions >> provide necessary permissions for the workflows (check the ‘Workflow permissions’ section)
- Add your
PAT token
as well as theOPENAI_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:
Better Code:
Top comments (0)