Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GPTReview With GitHub Example #630

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/gptreview-ghaction/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# GPTReview

This folder contains an example of building and implementing your own code reviewer as part of GitHub Actions.

Below are the files present here:

- `codereview.gpt`: Contains the GPTScript code and prompts.
- `workflow.yaml`: The workflow file for the GitHub action.

## Pre-requisites

- GitHub Account
- OpenAI API Key

## How To Run This Example

- Create a new repository in your GitHub account and create a `codereview.gpt` file in the root of that repo based on the contents provided in this file.
- Congfigure a GitHub Action for that repository. To do so, navigate to the "Actions" tab and then click on "setup a workflow yourself" link. This will create a new `main.yaml` inside `.github/workflows` path. Copy the contents from `workflow.yaml` to your `main.yaml`.
- Configure your `OPENAI_API_KEY` and `GH_TOKEN` as environment variables in your GitHub repo. Refer to [these steps](https://docs.github.com/en/actions/learn-github-actions/variables#creating-configuration-variables-for-a-repository) to create environment variables for your repository.
- Create a new branch, and add some code file to the repository and open a new pull request.
- The GitHub Action will trigger and our GPTReview will review your code and provide review comments.
26 changes: 26 additions & 0 deletions examples/gptreview-ghaction/codereview.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Name: Code Reviewer
Description: A tool to help you perform code review of open PRs
Context: learn-gh
Tools: sys.exec, sys.http.html2text?, sys.find, sys.read, sys.write
Args: PR_URL: The GitHub PR_URL

You have the gh cli available to you. Use it to perform code review for a pr from the $(repo) provided.

Perform the following steps in order:
1. Identify the files changed in the pull request ($PR_URL) using the pr number and perform a diff.
1. Analyze the complete code of each identified file and perform a detailed line by line code review.
2. Repeat the process for each changed file in the pr.
2. Share your review comments separately for each file.
3. In a new line write "Code: Approved" or "Code: Require Changes" based on the review comments.
---
Name: learn-gh
Description: A tool to help you learn gh cli

#!/usr/bin/env bash

echo "The following is the help text for the gh cli and some of its sub-commands. Use these when figuring out how to construct new commands. Note that the --search flag is used for filtering and sorting as well; there is no dedicate --sort flag."
gh --help
gh repo --help
gh pr --help
gh pr checkout --help
gh pr diff --help
57 changes: 57 additions & 0 deletions examples/gptreview-ghaction/workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: PR Review with GPTScript

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
pr_review:
runs-on: ubuntu-latest

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

- name: Get PR Details
id: pr_details
run: |
PR_URL=$(jq -r '.pull_request.html_url' $GITHUB_EVENT_PATH)
PR_NUMBER=$(jq -r '.pull_request.number' $GITHUB_EVENT_PATH)
PR_FILES=$(jq -r '.pull_request.changed_files' $GITHUB_EVENT_PATH)
echo "PR_URL=${PR_URL}" >> $GITHUB_ENV
echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_ENV
echo "PR_FILES=${PR_FILES}" >> $GITHUB_ENV

- name: Install GPTScript
run: curl https://get.gptscript.ai/install.sh | sh

- name: Run GPTScript for Code Review
id: run_gptscript
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
{
echo 'REVIEW<<EOF'
gptscript codereview.gpt --PR_URL=${{ env.PR_URL }}
echo EOF
} >> "$GITHUB_ENV"


- name: Post Review Comment
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
gh pr comment ${{ github.event.pull_request.number }} --body "$REVIEW"

- name: Set PR Status Fail
if: contains(env.REVIEW, 'Require Changes')
run: |
echo "Code Requires Changes"
exit 1

- name: Set PR Status Pass
if: contains(env.REVIEW, 'Approved')
run: |
echo "Code Approved"