-
Notifications
You must be signed in to change notification settings - Fork 45
129 lines (113 loc) · 4.32 KB
/
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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
name: Code Review Pipeline
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
code_review:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set Up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install Python Dependencies
run: |
python -m pip install --upgrade pip
pip install requests
- name: Run Code Review
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
GITHUB_TOKEN: ${{ secrets.G_TOKEN }}
run: |
python - <<EOF
import os
import requests
import json
# Helper function to extract line numbers
def extract_line_number(issue_text):
try:
if "Line" in issue_text:
line_part = issue_text.split("Line")[1].split(":")[0].strip()
return int(line_part)
except (ValueError, IndexError):
pass
return None
# Load GitHub event data
event_path = os.getenv("GITHUB_EVENT_PATH")
with open(event_path, 'r') as f:
event = json.load(f)
pr_number = event["pull_request"]["number"]
repo_full_name = event["repository"]["full_name"]
# Fetch PR diff
headers = {
"Authorization": f'token {os.getenv("GITHUB_TOKEN")}',
"Accept": "application/vnd.github.v3.diff",
}
diff_url = event["pull_request"]["url"] + "/files"
pr_files = requests.get(diff_url, headers=headers).json()
# Prepare inline comments
inline_comments = []
for file in pr_files:
filename = file["filename"]
patch = file.get("patch", "")
if not patch.strip():
continue
# Send patch to OpenAI for review
prompt = f"""
Analyze the following code patch and find:
- Syntax errors
- Logical issues
- Security vulnerabilities
For each issue, specify:
- Line number
- Problem description
- Suggested fix
Patch:
{patch}
"""
openai_headers = {
"Authorization": f'Bearer {os.getenv("OPENAI_API_KEY")}',
"Content-Type": "application/json",
}
openai_payload = {
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.3,
}
response = requests.post(
"https://api.openai.com/v1/chat/completions",
headers=openai_headers,
json=openai_payload,
)
response.raise_for_status()
ai_output = response.json()["choices"][0]["message"]["content"]
# Process AI output
for issue in ai_output.split("\n"):
if "Line" in issue:
line_number = extract_line_number(issue)
if line_number:
description = issue.split(": ", 1)[-1].strip()
inline_comments.append(
{
"path": filename,
"line": line_number,
"side": "RIGHT",
"body": f"**AI Code Review:**\n{description}",
}
)
# Submit review comments
if inline_comments:
review_url = f"https://api.github.com/repos/{repo_full_name}/pulls/{pr_number}/reviews"
review_data = {
"event": "COMMENT",
"body": "AI-generated inline comments for code review.",
"comments": inline_comments,
}
review_response = requests.post(review_url, headers=headers, json=review_data)
review_response.raise_for_status()
print("Code review comments posted successfully.")
else:
print("No issues found in the code.")
EOF