Supercharge Your Workflow: Google Gemini for GitHub Code Review
In team-based GitHub projects, pull requests ensure code quality by enabling thorough reviews before merging. Getting feedback from your Code Reviewers is crucial but you can speed up the contribution by a “self-service” review before involving your team.
What if you could get instant, AI-powered code review feedback directly in your Pull Requests? Now you can have it with my open-source GitHub action https://github.com/marketplace/actions/gemini-ai-code-reviewer
This article will provide a practical, step-by-step guide to using the gemini-ai-code-reviewer GitHub Action. I’ll demystify the process, even if you’re new to open source or AI-assisted development.
First things first
Gemini is a large language model (LLM) developed by Google. It’s designed to be the largest and most capable model yet, excelling in various tasks ranging from understanding and generating human-like text to complex problem-solving.
Regarding to the scope of this topic, think of it as an incredibly intelligent coding buddy who can analyze your code and offer helpful suggestions. It’s like having an experienced reviewer always on call!
How does the gemini-ai-code-reviewer Work?
The Action simplifies the code review process into four key steps:
- Analyzing the Changes: The Action identifies the code modifications within your pull request. It intelligently filters out irrelevant files, focusing only on the changes you’ve made.
- Consulting the Gemini Model: The Action breaks down your code changes into smaller, digestible chunks. These chunks are then sent to the Gemini API for analysis. This allows Gemini to provide focused feedback on specific sections of your code.
- Providing Feedback: Gemini scrutinizes each code chunk, looking for potential improvements, bugs, and style inconsistencies. It then generates review comments based on its analysis.
- Delivering the Review: The Action posts Gemini’s feedback directly as comments within your GitHub pull request. This integrates seamlessly into your existing workflow, making it easy to review and address the suggestions.
Getting Started: Integrating the Action
Let’s get your hands dirty! Follow these steps to integrate the gemini-ai-code-reviewer Action into your repository.
- To use this GitHub Action, you need an Gemini API key. If you don’t have one, sign up for an API key at Google AI Studio.
- Add the Gemini API key as a GitHub Secret in your repository with the name
GEMINI_API_KEY
. You can find more information about GitHub Secrets here. - Create a
.github/workflows/code-review.yml
file in your repository and add the following content:
name: Gemini AI Code Reviewer
on:
issue_comment:
types: [created]
permissions: write-all
jobs:
gemini-code-review:
runs-on: ubuntu-latest
if: |
github.event.issue.pull_request &&
contains(github.event.comment.body, '/gemini-review')
steps:
- name: PR Info
run: |
echo "Comment: ${{ github.event.comment.body }}"
echo "Issue Number: ${{ github.event.issue.number }}"
echo "Repository: ${{ github.repository }}"
- name: Checkout Repo
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Get PR Details
id: pr
run: |
PR_JSON=$(gh api repos/${{ github.repository }}/pulls/${{ github.event.issue.number }})
echo "head_sha=$(echo $PR_JSON | jq -r .head.sha)" >> $GITHUB_OUTPUT
echo "base_sha=$(echo $PR_JSON | jq -r .base.sha)" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: truongnh1992/gemini-ai-code-reviewer@latest
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
GEMINI_MODEL: gemini-1.5-pro-002 # Optional, default is `gemini-1.5-flash-002`
INPUT_EXCLUDE: "*.md,*.txt,package-lock.json,*.yml,*.yaml"
If you don’t set
GEMINI_MODEL
, the default model isgemini-1.5-flash-002
.gemini-1.5-flash-002
can be used for generating code, extracting data, edit text, and more. Best for tasks balancing performance and cost. For the detailed information about the models, please refer to Gemini models.
4. Commit codes to your repository, and working on your pull requests.
5. When you’re ready to review the PR, you can trigger the workflow by commenting /gemini-review
in the PR.
Benefits and Use Cases:
- Faster Feedback Loops: Get instant feedback on your code, speeding up development and contribution processes.
- Improved Code Quality: Gemini can catch potential issues and suggest best practices, helping you write cleaner, more maintainable code.
- Learning and Growth: Gemini’s suggestions can help you learn new coding techniques and improve your skills.
- Reduced Reviewer Burden: Automate parts of the code review process, freeing up human reviewers to focus on more complex issues.
Conclusion
The gemini-ai-code-reviewer GitHub Action empowers you to supercharge your workflow with AI-driven code reviews. By following these simple steps, you can integrate Gemini into your projects and experience the benefits of faster feedback, improved code quality, and a smoother open-source contribution experience. So, go ahead and give it a try! Your code (and your reviewers) will thank you.