Update review.yml #63
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
def extract_line_number(line_info): | |
try: | |
return int(line_info.split(" ")[1]) # Extract integer after "Line" | |
except (IndexError, ValueError): | |
return None # Return None if conversion fails | |
event_path = os.environ.get('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'] | |
token = os.environ.get('GITHUB_TOKEN') | |
openai_key = os.environ.get('OPENAI_API_KEY') | |
headers = {'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3.diff'} | |
diff_url = event['pull_request']['url'] + "/files" | |
pr_files = requests.get(diff_url, headers=headers).json() | |
inline_comments = [] | |
for fdata in pr_files: | |
filename = fdata['filename'] | |
patch = fdata.get('patch', '') | |
# Log patch for debugging | |
print(f"Reviewing file: {filename}") | |
print(f"Patch:\n{patch}") | |
issues_prompt = f""" | |
Review the following code patch for issues: | |
- Syntax errors | |
- Logical issues | |
- Security vulnerabilities | |
Provide feedback for each issue, including: | |
- Line number | |
- Problem description | |
- Suggested fix | |
Analyze only the following patch: | |
{patch} | |
""" | |
ai_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {openai_key}"} | |
data_issues = {"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.raise_for_status() | |
issues = issues_response.json()['choices'][0]['message']['content'].strip() | |
# Log AI response for debugging | |
print("Raw AI response:") | |
print(issues) | |
if issues and "no issues found" not in issues.lower(): | |
for issue in issues.split("\\n- "): | |
if issue.strip(): | |
if "Line " in issue: | |
parts = issue.split(":") | |
line_info = parts[0].strip() | |
description = ":".join(parts[1:]).strip() | |
line_number = extract_line_number(line_info) | |
if line_number is not None: | |
inline_comments.append({ | |
"path": filename, | |
"line": line_number, | |
"side": "RIGHT", | |
"body": f"**AI Code Review:**\n{description}" | |
}) | |
if inline_comments: | |
review_data = {"body": "AI-generated review comments.", "event": "COMMENT", "comments": inline_comments} | |
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'}, | |
json=review_data) | |
review_response.raise_for_status() | |
print("Comments posted successfully.") | |
else: | |
print("No issues found in the code.") | |
EOF |