Skip to content

add code review bot #11

add code review bot

add code review bot #11

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
- name: Get the PR diff
uses: GrantBirki/[email protected]
id: git-diff
with:
raw_diff_file_output: diff.txt
file_output_only: "true"
- 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)
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 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
})