Skip to content

Commit ca7b45b

Browse files
authored
Update review.yml
1 parent 7a6ee82 commit ca7b45b

File tree

1 file changed

+72
-73
lines changed

1 file changed

+72
-73
lines changed

.github/workflows/review.yml

+72-73
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
name: Code Review Pipeline
2+
23
on:
34
pull_request:
45
types: [opened, synchronize, reopened]
@@ -30,101 +31,99 @@ jobs:
3031
import requests
3132
import json
3233
33-
# Helper function to extract valid line numbers
34-
def extract_line_number(line_info):
34+
# Helper function to extract line numbers
35+
def extract_line_number(issue_text):
3536
try:
36-
return int(line_info.split(" ")[1]) # Extract integer after "Line"
37-
except (IndexError, ValueError):
38-
return None # Return None if conversion fails
39-
40-
# Gather GitHub event details
41-
event_path = os.environ.get('GITHUB_EVENT_PATH')
37+
if "Line" in issue_text:
38+
line_part = issue_text.split("Line")[1].split(":")[0].strip()
39+
return int(line_part)
40+
except (ValueError, IndexError):
41+
pass
42+
return None
43+
44+
# Load GitHub event data
45+
event_path = os.getenv("GITHUB_EVENT_PATH")
4246
with open(event_path, 'r') as f:
4347
event = json.load(f)
4448
45-
# Extract PR and repo details
46-
pr_number = event['pull_request']['number']
47-
repo_full_name = event['repository']['full_name']
48-
token = os.environ.get('GITHUB_TOKEN')
49-
openai_key = os.environ.get('OPENAI_API_KEY')
49+
pr_number = event["pull_request"]["number"]
50+
repo_full_name = event["repository"]["full_name"]
5051
51-
# Get PR diff
52+
# Fetch PR diff
5253
headers = {
53-
'Authorization': f'token {token}',
54-
'Accept': 'application/vnd.github.v3.diff',
54+
"Authorization": f'token {os.getenv("GITHUB_TOKEN")}',
55+
"Accept": "application/vnd.github.v3.diff",
5556
}
56-
diff_url = event['pull_request']['url'] + "/files"
57+
diff_url = event["pull_request"]["url"] + "/files"
5758
pr_files = requests.get(diff_url, headers=headers).json()
5859
59-
inline_comments = [] # Collect inline comments to post
60+
# Prepare inline comments
61+
inline_comments = []
6062
61-
# Loop through the files in the PR
62-
for fdata in pr_files:
63-
filename = fdata['filename']
64-
patch = fdata.get('patch', '')
63+
for file in pr_files:
64+
filename = file["filename"]
65+
patch = file.get("patch", "")
6566
66-
# Debug: Log the patch content to ensure it's being sent correctly
67-
print(f"Reviewing file: {filename}")
68-
print(f"Patch:\n{patch}")
67+
if not patch.strip():
68+
continue
6969
70-
# Call OpenAI for inline code analysis
71-
issues_prompt = f"""
72-
You are a code reviewer. Analyze the following code patch for issues such as:
70+
# Send patch to OpenAI for review
71+
prompt = f"""
72+
Analyze the following code patch and find:
7373
- Syntax errors
74-
- Logical errors
75-
- Best practices
76-
Provide specific inline comments that include:
77-
- The exact line number
78-
- A clear explanation of the issue
79-
- A suggested fix
80-
Analyze only the provided code:
74+
- Logical issues
75+
- Security vulnerabilities
76+
For each issue, specify:
77+
- Line number
78+
- Problem description
79+
- Suggested fix
80+
81+
Patch:
8182
{patch}
8283
"""
83-
ai_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {openai_key}"}
84-
data_issues = {
84+
openai_headers = {
85+
"Authorization": f'Bearer {os.getenv("OPENAI_API_KEY")}',
86+
"Content-Type": "application/json",
87+
}
88+
openai_payload = {
8589
"model": "gpt-4o-mini",
86-
"messages": [{"role": "user", "content": issues_prompt}],
87-
"temperature": 0.5
90+
"messages": [{"role": "user", "content": prompt}],
91+
"temperature": 0.3,
8892
}
89-
issues_response = requests.post("https://api.openai.com/v1/chat/completions", headers=ai_headers, json=data_issues)
90-
issues_response.raise_for_status()
91-
issues = issues_response.json()['choices'][0]['message']['content'].strip()
92-
93-
# Debug: Log the AI's response
94-
print(f"AI Response:\n{issues}")
95-
96-
# Parse issues for inline comments
97-
if issues and "no issues found" not in issues.lower():
98-
for issue in issues.split("\\n- "):
99-
if issue.strip():
100-
# Example issue format: "Line X: Description of issue"
101-
if "Line " in issue:
102-
parts = issue.split(":")
103-
line_info = parts[0].strip()
104-
description = ":".join(parts[1:]).strip()
105-
106-
# Extract valid line number
107-
line_number = extract_line_number(line_info)
108-
if line_number is not None:
109-
inline_comments.append({
110-
"path": filename,
111-
"line": line_number,
112-
"side": "RIGHT", # Changes are always on the "RIGHT" side in the diff
113-
"body": f"**AI Code Review:**\n{description}"
114-
})
115-
116-
# Post inline comments as a single review
93+
response = requests.post(
94+
"https://api.openai.com/v1/chat/completions",
95+
headers=openai_headers,
96+
json=openai_payload,
97+
)
98+
response.raise_for_status()
99+
ai_output = response.json()["choices"][0]["message"]["content"]
100+
101+
# Process AI output
102+
for issue in ai_output.split("\n"):
103+
if "Line" in issue:
104+
line_number = extract_line_number(issue)
105+
if line_number:
106+
description = issue.split(": ", 1)[-1].strip()
107+
inline_comments.append(
108+
{
109+
"path": filename,
110+
"line": line_number,
111+
"side": "RIGHT",
112+
"body": f"**AI Code Review:**\n{description}",
113+
}
114+
)
115+
116+
# Submit review comments
117117
if inline_comments:
118+
review_url = f"https://api.github.com/repos/{repo_full_name}/pulls/{pr_number}/reviews"
118119
review_data = {
119-
"body": "AI-generated review comments for code issues.",
120120
"event": "COMMENT",
121-
"comments": inline_comments
121+
"body": "AI-generated inline comments for code review.",
122+
"comments": inline_comments,
122123
}
123-
review_response = requests.post(f"https://api.github.com/repos/{repo_full_name}/pulls/{pr_number}/reviews",
124-
headers={'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3+json'},
125-
json=review_data)
124+
review_response = requests.post(review_url, headers=headers, json=review_data)
126125
review_response.raise_for_status()
127-
print("Inline review comments posted successfully.")
126+
print("Code review comments posted successfully.")
128127
else:
129128
print("No issues found in the code.")
130129
EOF

0 commit comments

Comments
 (0)