AI-Powered Code Review on Github

Sergey (Sagi) Shnaidman
3 min readApr 17, 2023

--

Code review is an essential aspect of collaborating on software, especially when working alone or on personal projects where an external perspective may be lacking. Code review is not only useful for catching bugs (which can be addressed through testing), but also for evaluating design decisions and identifying overlooked parts of the code. Having an additional opinion can help broaden one’s perspective and improve the overall quality of the code.

Let’s leverage the power of AI for code review in repositories. I have created a Github Action app that automatically inspects code changes in pull requests using OpenAI, and posts a review comment. While it is not perfect and may occasionally make mistakes (but who is without sin?), it can provide helpful feedback on your code, commit message, and other aspects of your pull request.

You can find this action in Marketplace: https://github.com/marketplace/actions/openai-code-review-github-action

Simply configure the sshnaidm/gpt-code-review-action in your job action as follows:

uses: sshnaidm/gpt-code-review-action@v1
with:
openai-key: ${{ secrets.OPENAI_API_KEY }}
# model: 'gpt-4'
# prompt: 'Only suggest performance improvements for this code.'

You have the option to configure the model and prompt settings to suit your needs. For an example of a job configuration, please refer to the Marketplace page or the repo itself. The resulting review comments can be viewed in pull requests on the same repository, such as this one:

Based on the code change provided, here are my feedback and suggestions:
The code change seems to be adding a new optional input parameter called post-if-error. This input parameter determines whether to post a comment if there was an error while running the action. This is a useful addition as it provides more control over how the action behaves in case of errors.
The code change also adds a new step called Show output in case of failure. This step is executed only if the previous step fails. It prints the error message to the console and also sets the errorresult output variable. This is a good addition as it helps to debug the errors more easily.
The Create comment step has been modified to use the post-if-error input parameter to determine whether to post a comment in case of errors. This is a good improvement as it provides more flexibility in how the action behaves.
In the Create comment step, the review result is now concatenated with the error result if there was an error. This is a useful improvement as it provides more information to the users in case of errors.
Overall, the code change seems to be well thought out and adds useful functionality to the action. However, it would be helpful to see the entire codebase to get a better understanding of the context and how this change fits in.

All that’s left to do is configure the OPENAI_API_KEY variable in the repository secrets, and you’ll be good to go!

Full job example:

on: [pull_request]

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
# This step checks out a copy of your repository.
- uses: actions/checkout@v3
- uses: sshnaidm/gpt-code-review-action
with:
openai-key: ${{ secrets.OPENAI_API_KEY }}
# Post comment with text even if step failed
post-if-error: 'true'

--

--