Skip to content

Commit

Permalink
Merge pull request #6 from mono-chrome/feature/patch-review
Browse files Browse the repository at this point in the history
Building new patch based workflow
  • Loading branch information
mono-chrome authored Feb 2, 2023
2 parents c407b28 + 05879f0 commit a99830d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 78 deletions.
25 changes: 15 additions & 10 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@ jobs:
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 2
- id: diff
- id: patch
run: |
git fetch origin main
diff=$(git diff origin/main)
echo $diff
echo "DIFF=$(echo $diff)" >> $GITHUB_ENV
patch_output=$(curl --silent --request GET \
--url https://api.github.com/repos/$PATCH_REPO/pulls/$PATCH_PR \
--header "Accept: application/vnd.github.v3.patch" \
--header "Authorization: Bearer $PATCH_GITHUB_TOKEN")
echo $patch_output
echo "GIT_PATCH_OUTPUT=$(echo $patch_output)" >> $GITHUB_ENV
env:
PATCH_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PATCH_PR: ${{ github.event.pull_request.number }}
PATCH_REPO: ${{ github.repository }}
- id: review
uses: mono-chrome/GPTReviewWorkflow@main
with:
GIT_COMMIT_HASH: ${{ github.event.pull_request.head.sha }}
GIT_PATCH_OUTPUT: ${{ env.GIT_PATCH_OUTPUT }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_ORG_KEY: ${{ secrets.OPENAI_ORG_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GIT_COMMIT_HASH: ${{ github.event.pull_request.head.sha }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_TITLE: ${{ github.event.pull_request.title }}
PR_BODY: ${{ github.event.pull_request.body }}
REPOSITORY_NAME: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
DIFF: ${{ env.DIFF }}
36 changes: 16 additions & 20 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
name: 'GPTReviewWorkflow'
description: 'Let AI review your code'
description: 'GPT-3 reviews your PR'
branding:
icon: 'eye'
color: 'gray-dark'
inputs:
GIT_COMMIT_HASH:
description: 'SHA of the pull request-head to attach a review comment'
required: true
GIT_PATCH_OUTPUT:
description: 'The pull request in patch format for prompt interpretation'
required: true
GITHUB_TOKEN:
description: 'Derivative token for using the GitHub REST API'
required: true
OPENAI_API_KEY:
description: 'OpenAI API Key'
required: true
OPENAI_ORG_KEY:
description: 'OpenAI Organization ID'
required: true
GITHUB_TOKEN:
description: 'pass secrets.GITHUB_TOKEN'
required: true
GIT_COMMIT_HASH:
description: 'pass github.event.pull_request.head.sha'
PR_NUMBER:
description: 'The pull request id (ie: 17)'
required: true
PR_TITLE:
description: 'pass github.event.pull_request.title'
required: true
PR_BODY:
description: 'pass github.event.pull_request.body'
description: 'The pull request title'
required: true
REPOSITORY_NAME:
description: 'pass github.repository'
required: true
PR_NUMBER:
description: 'pass github.event.pull_request.number'
required: true
DIFF:
description: 'what has changed between PRs'
description: 'The repository name (ie: octocat/hello-world)'
required: true
runs:
using: "composite"
Expand Down Expand Up @@ -57,10 +54,9 @@ runs:
source .env/bin/activate
python review.py
env:
GIT_COMMIT_HASH: ${{ inputs.GIT_COMMIT_HASH }}
GITHUB_TOKEN: ${{ inputs.GITHUB_TOKEN }}
LINK: "https://github.com/${{ inputs.REPOSITORY_NAME }}/pull/${{ inputs.PR_NUMBER }}"
OPENAI_API_KEY: ${{ inputs.OPENAI_API_KEY }}
OPENAI_ORG_KEY: ${{ inputs.OPENAI_ORG_KEY }}
GITHUB_TOKEN: ${{ inputs.GITHUB_TOKEN }}
GIT_COMMIT_HASH: ${{ inputs.GIT_COMMIT_HASH }}
PR_TITLE: ${{ inputs.PR_TITLE }}
PR_BODY: ${{ inputs.PR_BODY }}
98 changes: 50 additions & 48 deletions review.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
import os
import requests
import json
import subprocess #is this still needed?
import subprocess # is this still needed?
import openai


def get_review():
pr_link = os.getenv("LINK")
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.organization = os.getenv("OPENAI_ORG_KEY")
ACCESS_TOKEN = os.getenv("GITHUB_TOKEN")
GIT_COMMIT_HASH = os.getenv("GIT_COMMIT_HASH")
PR_TITLE = os.getenv("PR_TITLE")
PR_BODY = os.getenv("PR_BODY")
PR_DIFF = os.getenv("DIFF")

headers = {
"Accept": "application/vnd.github.v3.patch",
"authorization": f"Bearer {ACCESS_TOKEN}"
}

intro = f"Here is a pull request. Please assume you are a reviewer of this PR. First I will tell you the title and body of the PR.\n"
pr_title = f"The title is {PR_TITLE}.\n"
pr_body = f"The body is {PR_BODY}.\n"
question = "Can you tell me the problems and bugs with the following pull request and provide specific suggestions to improve it? Afterwards your explanation please provide a short summary response in structured Markdown language using headings and lists.\n"
diff = f"Here's the diff of what changed in this PR: {PR_DIFF}"
prompt = intro + pr_title + pr_body + question + diff

print(f"Prompt sent to OpenAI: {prompt}")

model = "text-davinci-003"
response = openai.Completion.create(
engine=model,
prompt=prompt,
temperature=0.5,
max_tokens=324,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
review = response['choices'][0]['text']

data = {"body": review, "commit_id": GIT_COMMIT_HASH, "event": "COMMENT"}
data = json.dumps(data)
print(f"Response from OpenAI: {data}")

OWNER = pr_link.split("/")[-4]
REPO = pr_link.split("/")[-3]
PR_NUMBER = pr_link.split("/")[-1]

# https://api.github.com/repos/OWNER/REPO/pulls/PULL_NUMBER/reviews
response = requests.post(f'https://api.github.com/repos/{OWNER}/{REPO}/pulls/{PR_NUMBER}/reviews', headers=headers, data=data)
print(response.json())
ACCESS_TOKEN = os.getenv("GITHUB_TOKEN")
GIT_COMMIT_HASH = os.getenv("GIT_COMMIT_HASH")
PR_PATCH = os.getenv("GIT_PATCH_OUTPUT")
model = "text-davinci-003"
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.organization = os.getenv("OPENAI_ORG_KEY")
pr_link = os.getenv("LINK")

headers = {
"Accept": "application/vnd.github.v3.patch",
"authorization": f"Bearer {ACCESS_TOKEN}",
}

intro = f"Act as a code reviewer of a Pull Request, providing feedback on the code changes below. You are provided with the Pull Request changes in a patch format.\n"
explanation = f"Each patch entry has the commit message in the Subject line followed by the code changes (diffs) in a unidiff format.\n"
patch_info = f"Patch of the Pull Request to review:\n\n{PR_PATCH}\n"
task_headline = f"As a code reviewer, your task is:\n"
task_list = f"- Review the code changes (diffs) in the patch and provide feedback.\n- If there are any bugs, highlight them.\n - Do not highlight minor issues and nitpicks.\n - Look out for typos in repeating variables.\n - Use markdown formatting.\n - Use bullet points if you have multiple comments.\n"
prompt = intro + explanation + patch_info + task_headline + task_list

print(f"Prompt sent to GPT-3: {prompt}")

response = openai.Completion.create(
engine=model,
prompt=prompt,
temperature=0.55,
max_tokens=312,
top_p=1,
frequency_penalty=0.3,
presence_penalty=0.0,
)
review = response["choices"][0]["text"]

data = {"body": review, "commit_id": GIT_COMMIT_HASH, "event": "COMMENT"}
data = json.dumps(data)
print(f"\n\nResponse from GPT-3: {data}\n\n")

OWNER = pr_link.split("/")[-4]
REPO = pr_link.split("/")[-3]
PR_NUMBER = pr_link.split("/")[-1]

# https://api.github.com/repos/OWNER/REPO/pulls/PULL_NUMBER/reviews
response = requests.post(
f"https://api.github.com/repos/{OWNER}/{REPO}/pulls/{PR_NUMBER}/reviews",
headers=headers,
data=data,
)
print(response.json())


if __name__ == "__main__":
get_review()
get_review()

0 comments on commit a99830d

Please sign in to comment.