Skip to main content

Auto-code-review for GitHub Pull Requests via ChatGPT

Project description

Automatic Code Review

This repository uses GitHub Actions for automatic code review on every Pull Request. The code review process is powered by the auto-code-review library, which connects to the OpenAI API to generate inline comments and suggestions for code improvements.

GitHub Actions Configuration

Below is the configuration for the GitHub Actions workflow:

name: Auto Code Review

on:
  pull_request:
    types:  [opened, synchronize, reopened]
    branches:
      - '*'

jobs:
  review:
    runs-on: ubuntu-latest

    permissions:
      contents: write
      pull-requests: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.12"

      - name: Install auto-code-review
        run: pip install auto-code-review

      - name: Run auto code review
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          GITHUB_HEAD_REF: ${{ github.head_ref }}
          GITHUB_BASE_REF: ${{ github.base_ref }}
          REPO_OWNER: ${{ github.repository_owner }}
          REPO_NAME: ${{ github.event.repository.name }}
          PULL_NUMBER: ${{ github.event.number }}
        run: python -m auto_code_review.cli --config config.yaml

How It Works

  1. Trigger on pull_request: The workflow is triggered when a Pull Request is created, updated, or reopened.
  2. Set Up Python Environment: The workflow sets up Python version 3.12 and installs the auto-code-review package from PyPI.
  3. Run Auto Code Review: The auto_code_review.cli command is executed to analyze the changes in the Pull Request using OpenAI’s API. The tool will leave inline comments on the code changes with potential issues.

Prerequisites

Before using this workflow, you need to add the following secrets to your repository settings:

  • GITHUB_TOKEN: A GitHub token automatically provided in your repository (no extra setup needed).
  • OPENAI_API_KEY: Your OpenAI API key, which must be added manually in Settings -> Secrets and variables -> Actions -> New repository secret.

Example Usage

To enable automatic code review, simply create a Pull Request from any branch in your repository. GitHub Actions will automatically trigger the analysis, and inline comments will be added directly to the Pull Request with feedback on code style, potential optimizations, and any detected issues.

Additional Notes

  • This workflow supports any branch (branches: ['*']), meaning it will run for Pull Requests involving any source or target branch.
  • The tool is configured via a config.yaml file, which should be present in the root directory of your repository. This file allows you to specify various settings such as the AI model, temperature and file extensions

Sample Config File (config.yaml)

# config.yml

analysis:
  file_extensions: [".py", ".cpp",".yaml"]

ai_settings:
  ai_model: gpt-4o-mini
  temperature: 0.7
  max_tokens: 1000

In this configuration:

  • ai_model sets the chatgpt model which will be used to create comments(gpt-4o-mini by default)
  • file_extensions sets the extensions of files which will be auto-reviewed(Necessary to write extensions)
  • temperature controls the randomness of the suggestions - higher values make responses more creative(0.7 by default).
  • max_tokens limits the number of tokens generated by the AI.(1000 by default)
  • code_style specifies the coding style to follow (the most basic code styles by default).

Troubleshooting

If the workflow fails with a 422 Unprocessable Entity error, it could be due to:

  • An incorrect position value for inline comments.
  • An invalid or outdated commit_id.
  • Missing or incorrect path for the file being commented on.

Ensure that your configuration and API keys are set up correctly, and check the logs in GitHub Actions for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

auto_code_review-0.1.3.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

auto_code_review-0.1.3-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file auto_code_review-0.1.3.tar.gz.

File metadata

  • Download URL: auto_code_review-0.1.3.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for auto_code_review-0.1.3.tar.gz
Algorithm Hash digest
SHA256 09dc687a8c6a9b4fd4b5f3a2842faf87291da8d6c9a1f56e7429a8b226ddc0d4
MD5 01ac52c71090067d299e87d62a5e3cac
BLAKE2b-256 e97ed24b1cb66b5a4feeed0655f4298e8ddb6f391e7d911d27505e926bebc8be

See more details on using hashes here.

File details

Details for the file auto_code_review-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for auto_code_review-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 96f4fb72ac50887d51b7e45d965dc51e4498019e9ee2bf87d8b5415e9aa376b2
MD5 244b04cd3a46206467e92d5207a8edc9
BLAKE2b-256 21bfb26152c910834b961f9b8ee294142f30c0cbfefc02549624c545fb568a95

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page