diff --git a/examples/gptreview-ghaction/README.md b/examples/gptreview-ghaction/README.md new file mode 100644 index 00000000..fe793a92 --- /dev/null +++ b/examples/gptreview-ghaction/README.md @@ -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. diff --git a/examples/gptreview-ghaction/codereview.gpt b/examples/gptreview-ghaction/codereview.gpt new file mode 100644 index 00000000..f2502b50 --- /dev/null +++ b/examples/gptreview-ghaction/codereview.gpt @@ -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 diff --git a/examples/gptreview-ghaction/workflow.yaml b/examples/gptreview-ghaction/workflow.yaml new file mode 100644 index 00000000..21ade39b --- /dev/null +++ b/examples/gptreview-ghaction/workflow.yaml @@ -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<> "$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" +