In this tutorial, you will learn how to create a custom AI code review bot using OpenAI APIs and GitHub Actions. This tool can help automate code reviews, provide suggestions, and improve your development workflow.

Prerequisites

  • A GitHub account with a repository to automate
  • Access to OpenAI API key
  • Basic knowledge of GitHub Actions and YAML
  • Python installed locally for testing (optional)

Step 1: Obtain OpenAI API Key

Sign up at OpenAI Platform and generate an API key. Keep this key secure, as it allows access to your OpenAI account.

Step 2: Create a GitHub Repository

Initialize a new repository or choose an existing one where you want to add the AI review bot. This repository will host the GitHub Actions workflow and related scripts.

Step 3: Add Secrets to GitHub

Navigate to your repository Settings > Secrets and add a new secret named OPENAI_API_KEY. Paste your OpenAI API key here.

Step 4: Create the GitHub Actions Workflow

Create a new file in your repository at .github/workflows/code-review.yml. This workflow will trigger on pull requests and run the review process.

name: AI Code Review

on:
  pull_request:
    types: [opened, synchronize]

jobs:
  review:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.x'

      - name: Install dependencies
        run: |
          pip install openai

      - name: Run AI Code Review
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
        run: |
          python .github/scripts/review.py

Step 5: Write the Review Script

Create a directory at .github/scripts and add a file named review.py. This script will fetch the code, send it to OpenAI, and post the review comments.

import os
import openai
import subprocess

# Fetch the PR diff or code to review
def get_code():
    result = subprocess.run(['git', 'diff', 'origin/main...HEAD'], capture_output=True, text=True)
    return result.stdout

# Send code to OpenAI for review
def review_code(code):
    prompt = f"Review the following code and provide suggestions:\n\n{code}"
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.2,
        max_tokens=500,
    )
    return response.choices[0].message['content']

def main():
    openai.api_key = os.getenv('OPENAI_API_KEY')
    code = get_code()
    review = review_code(code)
    print("AI Review:\n", review)

if __name__ == "__main__":
    main()

Step 6: Automate and Test

Push your changes to GitHub. When a pull request is opened or updated, the workflow will run, analyze the code, and generate review comments using AI.

Conclusion

By integrating OpenAI APIs with GitHub Actions, you can automate code reviews and provide instant feedback to developers. Customize the prompt and scripts further to suit your project needs.