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 code review bot #35

Merged
merged 1 commit into from
Apr 3, 2024
Merged
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
62 changes: 62 additions & 0 deletions .github/workflows/code-review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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/"$//')

# 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
comments=$(echo $response | jq -r '.choices[0].message.content' | jq -sRr @json)

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 }}
})