forked from GREENRAT-K405/playground
-
Notifications
You must be signed in to change notification settings - Fork 0
88 lines (72 loc) · 2.59 KB
/
Copy pathcode-review.yaml
File metadata and controls
88 lines (72 loc) · 2.59 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
name: AI Code Reviewer
on:
issue_comment:
types: [created]
permissions:
contents: read
pull-requests: write
issues: write
jobs:
gemini-code-review:
runs-on: ubuntu-latest
if: |
github.event.issue.pull_request &&
contains(github.event.comment.body, '/gemini-review')
steps:
- name: Checkout Repo
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install Google Gen AI SDK
run: pip install google-generativeai
- name: Run Gemini Code Review
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
PR_NUMBER: ${{ github.event.issue.number }}
REPO: ${{ github.repository }}
run: |
python -c "
import os
import subprocess
import google.generativeai as genai
# 1. Setup Gemini
genai.configure(api_key=os.environ['GEMINI_API_KEY'])
model = genai.GenerativeModel('gemini-1.5-flash')
# 2. Get the PR Diff using GitHub CLI
pr_num = os.environ['PR_NUMBER']
repo = os.environ['REPO']
print(f'Fetching diff for PR #{pr_num} in {repo}...')
# Get the diff text
diff_cmd = ['gh', 'pr', 'diff', pr_num, '--repo', repo]
process = subprocess.run(diff_cmd, capture_output=True, text=True)
diff_text = process.stdout
if not diff_text:
print('No diff found or error fetching diff.')
exit(1)
# Limit diff size to prevent token overflow (simple truncation)
if len(diff_text) > 30000:
diff_text = diff_text[:30000] + '\n...(truncated)...'
# 3. Ask Gemini to review
prompt = f'''
You are an expert Senior Software Engineer. Review the following code diff.
Focus on:
1. Security vulnerabilities.
2. Potential bugs.
3. Performance improvements.
4. Code cleanliness/Best practices.
Be concise and constructive.
Code Diff:
{diff_text}
'''
print('Sending to Gemini...')
response = model.generate_content(prompt)
review_text = response.text
# 4. Post the review as a comment
print('Posting comment...')
comment_cmd = ['gh', 'pr', 'comment', pr_num, '--repo', repo, '--body', review_text]
subprocess.run(comment_cmd, check=True)
print('Review posted!')
"