Update review.yml

This commit is contained in:
PierreGode 2024-12-10 15:53:19 +01:00 committed by GitHub
parent 7a6ee82a1a
commit eada44f538
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,108 +23,91 @@ jobs:
- name: Run Code Review - name: Run Code Review
env: env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
GITHUB_TOKEN: ${{ secrets.G_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: | run: |
python - <<EOF python - <<EOF
import os import os
import requests import requests
import json import json
# Helper function to extract valid line numbers
def extract_line_number(line_info): def extract_line_number(line_info):
try: try:
return int(line_info.split(" ")[1]) # Extract integer after "Line" return int(line_info.split(" ")[1]) # Extract integer after "Line"
except (IndexError, ValueError): except (IndexError, ValueError):
return None # Return None if conversion fails return None # Return None if conversion fails
# Gather GitHub event details
event_path = os.environ.get('GITHUB_EVENT_PATH') event_path = os.environ.get('GITHUB_EVENT_PATH')
with open(event_path, 'r') as f: with open(event_path, 'r') as f:
event = json.load(f) event = json.load(f)
# Extract PR and repo details
pr_number = event['pull_request']['number'] pr_number = event['pull_request']['number']
repo_full_name = event['repository']['full_name'] repo_full_name = event['repository']['full_name']
token = os.environ.get('GITHUB_TOKEN') token = os.environ.get('GITHUB_TOKEN')
openai_key = os.environ.get('OPENAI_API_KEY') openai_key = os.environ.get('OPENAI_API_KEY')
# Get PR diff headers = {'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3.diff'}
headers = {
'Authorization': f'token {token}',
'Accept': 'application/vnd.github.v3.diff',
}
diff_url = event['pull_request']['url'] + "/files" diff_url = event['pull_request']['url'] + "/files"
pr_files = requests.get(diff_url, headers=headers).json() pr_files = requests.get(diff_url, headers=headers).json()
inline_comments = [] # Collect inline comments to post inline_comments = []
# Loop through the files in the PR
for fdata in pr_files: for fdata in pr_files:
filename = fdata['filename'] filename = fdata['filename']
patch = fdata.get('patch', '') patch = fdata.get('patch', '')
# Debug: Log the patch content to ensure it's being sent correctly # Log patch for debugging
print(f"Reviewing file: {filename}") print(f"Reviewing file: {filename}")
print(f"Patch:\n{patch}") print(f"Patch:\n{patch}")
# Call OpenAI for inline code analysis
issues_prompt = f""" issues_prompt = f"""
You are a code reviewer. Analyze the following code patch for issues such as: Review the following code patch for issues:
- Syntax errors - Syntax errors
- Logical errors - Logical issues
- Best practices - Security vulnerabilities
Provide specific inline comments that include: Provide feedback for each issue, including:
- The exact line number - Line number
- A clear explanation of the issue - Problem description
- A suggested fix - Suggested fix
Analyze only the provided code:
Analyze only the following patch:
{patch} {patch}
""" """
ai_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {openai_key}"} ai_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {openai_key}"}
data_issues = { data_issues = {"model": "gpt-4o-mini", "messages": [{"role": "user", "content": issues_prompt}], "temperature": 0.5}
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": issues_prompt}],
"temperature": 0.5
}
issues_response = requests.post("https://api.openai.com/v1/chat/completions", headers=ai_headers, json=data_issues) issues_response = requests.post("https://api.openai.com/v1/chat/completions", headers=ai_headers, json=data_issues)
issues_response.raise_for_status() issues_response.raise_for_status()
issues = issues_response.json()['choices'][0]['message']['content'].strip() issues = issues_response.json()['choices'][0]['message']['content'].strip()
# Debug: Log the AI's response # Log AI response for debugging
print(f"AI Response:\n{issues}") print("Raw AI response:")
print(issues)
# Parse issues for inline comments
if issues and "no issues found" not in issues.lower(): if issues and "no issues found" not in issues.lower():
for issue in issues.split("\\n- "): for issue in issues.split("\\n- "):
if issue.strip(): if issue.strip():
# Example issue format: "Line X: Description of issue"
if "Line " in issue: if "Line " in issue:
parts = issue.split(":") parts = issue.split(":")
line_info = parts[0].strip() line_info = parts[0].strip()
description = ":".join(parts[1:]).strip() description = ":".join(parts[1:]).strip()
# Extract valid line number
line_number = extract_line_number(line_info) line_number = extract_line_number(line_info)
if line_number is not None: if line_number is not None:
inline_comments.append({ inline_comments.append({
"path": filename, "path": filename,
"line": line_number, "line": line_number,
"side": "RIGHT", # Changes are always on the "RIGHT" side in the diff "side": "RIGHT",
"body": f"**AI Code Review:**\n{description}" "body": f"**AI Code Review:**\n{description}"
}) })
# Post inline comments as a single review
if inline_comments: if inline_comments:
review_data = { review_data = {"body": "AI-generated review comments.", "event": "COMMENT", "comments": inline_comments}
"body": "AI-generated review comments for code issues.",
"event": "COMMENT",
"comments": inline_comments
}
review_response = requests.post(f"https://api.github.com/repos/{repo_full_name}/pulls/{pr_number}/reviews", review_response = requests.post(f"https://api.github.com/repos/{repo_full_name}/pulls/{pr_number}/reviews",
headers={'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3+json'}, headers={'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3+json'},
json=review_data) json=review_data)
review_response.raise_for_status() review_response.raise_for_status()
print("Inline review comments posted successfully.") print("Comments posted successfully.")
else: else:
print("No issues found in the code.") print("No issues found in the code.")
EOF EOF