yu-iskw / gpt-code-review-action

A GitHub Action for GPT to review a pull request
Apache License 2.0
8 stars 9 forks source link

gpt-code-review-action

A container GitHub Action to review a pull request by GPT.

If the size of a pull request is over the maximum chunk size of the OpenAI API, the Action will split the pull request into multiple chunks and generate review comments for each chunk. And then the Action summarizes the review comments and posts a review comment to the pull request.

Pre-requisites

We have to set a GitHub Actions secret OPENAI_API_KEY to use the OpenAI API so that we securely pass it to the Action.

Inputs

As you might know, a model of OpenAI has limitation of the maximum number of input tokens. So we have to split the diff of a pull request into multiple chunks, if the size of the diff is over the limitation. We can tune the chunk size based on the model we use. For instance, gpt-4 can handle larger input tokens than gpt-3.5-turbo. So, we can increase the chunk size for gpt-4 than gpt-3.5-turbo.

Example usage

Here is an example to use the Action to review a pull request of the repository. The actual file is located at .github/workflows/test-action.yml. We set extra_prompt to You are very familiar with python too., as the Action is implemented in Python. We aim to make GPT review a pull request from a point of view of a Python developer.

As a result of an execution of the Action, the Action posts a review comment to the pull request like the following image. An example comment of the code review

name: "Test Code Review"

on:
  pull_request:
    paths-ignore:
      - "*.md"
      - "LICENSE"

jobs:
  review:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    steps:
      - uses: actions/checkout@v3
      - name: "Get diff of the pull request"
        id: get_diff
        shell: bash
        env:
          PULL_REQUEST_HEAD_REF: "${{ github.event.pull_request.head.ref }}"
        run: |-
          git fetch origin "${{ env.PULL_REQUEST_HEAD_REF }}:${{ env.PULL_REQUEST_HEAD_REF }}"
          git checkout "${{ env.PULL_REQUEST_HEAD_REF }}"
          git diff "origin/${{ env.PULL_REQUEST_HEAD_REF }}" > "diff.txt"
          # shellcheck disable=SC2086
          echo "diff=$(cat "diff.txt")" >> $GITHUB_ENV
      - uses: yu-iskw/gpt-code-review-action@v0.3.0
        name: "Code Review by GPT"
        id: review
        with:
          openai_api_key: ${{ secrets.OPENAI_API_KEY }}
          github_token: ${{ secrets.GITHUB_TOKEN }}
          github_repository: ${{ github.repository }}
          github_pull_request_number: ${{ github.event.pull_request.number }}
          git_commit_hash: ${{ github.event.pull_request.head.sha }}
          model: "gpt-3.5-turbo"
          temperature: "0.1"
          max_tokens: "512"
          top_p: "1"
          frequency_penalty: "0.0"
          presence_penalty: "0.0"
          pull_request_diff: |-
            ${{ steps.get_diff.outputs.pull_request_diff }}
          pull_request_chunk_size: "3500"
          extra_prompt: |-
            You are very familiar with python too.
          log_level: "DEBUG"

Known Issues

  1. We may pay for the OpenAI API usage a lot, if we use the Action for a large number of pull requests.
  2. The Action may fail due to the rate limit of the OpenAI API.