-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matthew Fisher <matt.fisher@fermyon.com>
- Loading branch information
1 parent
5cc58d7
commit c838257
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
name: Code Review | ||
|
||
on: | ||
pull_request: | ||
types: [opened, synchronize, reopened] | ||
|
||
jobs: | ||
review: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Get the PR diff | ||
uses: GrantBirki/git-diff-action@v2.6.0 | ||
id: git-diff | ||
with: | ||
raw_diff_file_output: diff.txt | ||
file_output_only: "true" | ||
|
||
- name: Review code | ||
id: code-review | ||
run: | | ||
# Get the diff | ||
escaped_diff=$(cat ${{ steps.git-diff.outputs.raw-diff-path }} | jq -sRr @json | sed -e 's/^"//' -e 's/"$//') | ||
echo $escaped_diff | ||
# Send to OpenAI for review | ||
response=$(curl -X POST https://api.openai.com/v1/chat/completions \ | ||
-H "Content-Type: application/json" \ | ||
-H "Authorization: Bearer ${{ secrets.OPENAI_API_KEY }}" \ | ||
-d '{ | ||
"model": "gpt-4-turbo-preview", | ||
"messages": [ | ||
{ | ||
"role": "system", | ||
"content": "Review this pull request and suggest improvements. Check for any logical and security concerns with the code. If you do not have any recommendations, respond with \"Looks good to me!\"." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "```\n'"$escaped_diff"'\n```\n" | ||
} | ||
] | ||
}') | ||
# Extract the review | ||
echo $response | ||
comments=$(echo $response | jq -r '.choices[0].message.content' | jq -sRr @json) | ||
echo $comments | ||
echo "comments=$(echo $comments)" >> $GITHUB_OUTPUT | ||
- name: Post review comments | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
github.rest.issues.createComment({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: ${{ steps.code-review.outputs.comments }} | ||
}) |