-
Notifications
You must be signed in to change notification settings - Fork 26
62 lines (53 loc) · 1.99 KB
/
code-review.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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-4o",
"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 }}
})