Skip to content

add code review bot

add code review bot #5

Workflow file for this run

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
- uses: GrantBirki/git-diff-action@v2.6.0
id: git-diff
- name: Review code
id: code-review
run: |
# Get the diff
diff=$(cat ${{ steps.git-diff.outputs.raw-diff-path }})
escaped_diff=$(echo "$diff" | jq -sRr @json)
# 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 code 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": "$escaped_diff"
}
]
}')
# Extract the review
echo $response
comments=$(echo $response | jq -r '.choices[0].message.content')
echo "comments=$comments" >> $GITHUB_OUTPUT
- name: Post review comments
uses: actions/github-script@v7
with:
script: |
const comments = ${{ steps.code-review.outputs.comments }}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comments
})