Skip to content

Commit 6d3043d

Browse files
Merge pull request #630 from techmaharaj/gptreview-ghaction
Add GPTReview With GitHub Example
2 parents 84b2aa3 + 5fb849f commit 6d3043d

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

examples/gptreview-ghaction/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# GPTReview
2+
3+
This folder contains an example of building and implementing your own code reviewer as part of GitHub Actions.
4+
5+
Below are the files present here:
6+
7+
- `codereview.gpt`: Contains the GPTScript code and prompts.
8+
- `workflow.yaml`: The workflow file for the GitHub action.
9+
10+
## Pre-requisites
11+
12+
- GitHub Account
13+
- OpenAI API Key
14+
15+
## How To Run This Example
16+
17+
- 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.
18+
- 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`.
19+
- 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.
20+
- Create a new branch, and add some code file to the repository and open a new pull request.
21+
- The GitHub Action will trigger and our GPTReview will review your code and provide review comments.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Name: Code Reviewer
2+
Description: A tool to help you perform code review of open PRs
3+
Context: learn-gh
4+
Tools: sys.exec, sys.http.html2text?, sys.find, sys.read, sys.write
5+
Args: PR_URL: The GitHub PR_URL
6+
7+
You have the gh cli available to you. Use it to perform code review for a pr from the $(repo) provided.
8+
9+
Perform the following steps in order:
10+
1. Identify the files changed in the pull request ($PR_URL) using the pr number and perform a diff.
11+
1. Analyze the complete code of each identified file and perform a detailed line by line code review.
12+
2. Repeat the process for each changed file in the pr.
13+
2. Share your review comments separately for each file.
14+
3. In a new line write "Code: Approved" or "Code: Require Changes" based on the review comments.
15+
---
16+
Name: learn-gh
17+
Description: A tool to help you learn gh cli
18+
19+
#!/usr/bin/env bash
20+
21+
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."
22+
gh --help
23+
gh repo --help
24+
gh pr --help
25+
gh pr checkout --help
26+
gh pr diff --help
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: PR Review with GPTScript
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
jobs:
8+
pr_review:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v2
14+
15+
- name: Get PR Details
16+
id: pr_details
17+
run: |
18+
PR_URL=$(jq -r '.pull_request.html_url' $GITHUB_EVENT_PATH)
19+
PR_NUMBER=$(jq -r '.pull_request.number' $GITHUB_EVENT_PATH)
20+
PR_FILES=$(jq -r '.pull_request.changed_files' $GITHUB_EVENT_PATH)
21+
echo "PR_URL=${PR_URL}" >> $GITHUB_ENV
22+
echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_ENV
23+
echo "PR_FILES=${PR_FILES}" >> $GITHUB_ENV
24+
25+
- name: Install GPTScript
26+
run: curl https://get.gptscript.ai/install.sh | sh
27+
28+
- name: Run GPTScript for Code Review
29+
id: run_gptscript
30+
env:
31+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
32+
GH_TOKEN: ${{ secrets.GH_TOKEN }}
33+
run: |
34+
{
35+
echo 'REVIEW<<EOF'
36+
gptscript codereview.gpt --PR_URL=${{ env.PR_URL }}
37+
echo EOF
38+
} >> "$GITHUB_ENV"
39+
40+
41+
- name: Post Review Comment
42+
env:
43+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
44+
run: |
45+
gh pr comment ${{ github.event.pull_request.number }} --body "$REVIEW"
46+
47+
- name: Set PR Status Fail
48+
if: contains(env.REVIEW, 'Require Changes')
49+
run: |
50+
echo "Code Requires Changes"
51+
exit 1
52+
53+
- name: Set PR Status Pass
54+
if: contains(env.REVIEW, 'Approved')
55+
run: |
56+
echo "Code Approved"
57+

0 commit comments

Comments
 (0)