Update review.yml

This commit is contained in:
PierreGode 2025-02-03 14:49:54 +01:00 committed by GitHub
parent 224986e33e
commit be872e8e94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,4 @@
name: PR Summary and Code Review name: PR summary by AI
on: on:
pull_request: pull_request:
@ -14,47 +14,49 @@ permissions:
jobs: jobs:
pr_summary: pr_summary:
name: PR Summary
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
# Checkout repository
- name: Checkout Code - name: Checkout Code
uses: actions/checkout@v3 uses: actions/checkout@v3
# Set up Python for PR summaries - name: Read README.md
id: read_readme
run: |
README_CONTENT=$(cat README.md)
echo "::set-output name=README::$README_CONTENT"
- name: Set Up Python - name: Set Up Python
uses: actions/setup-python@v4 uses: actions/setup-python@v4
with: with:
python-version: '3.9' python-version: '3.9'
# Install Python dependencies
- name: Install Python Dependencies - name: Install Python Dependencies
run: | run: |
python -m pip install --upgrade pip python -m pip install --upgrade pip
pip install requests pip install requests
# Run AI Analysis (PR Summary Only) - name: PR Summary
- name: Generate PR Summary
env: env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
GITHUB_TOKEN: ${{ secrets.G_TOKEN }} GITHUB_TOKEN: ${{ secrets.G_TOKEN }}
README_CONTENT: ${{ steps.read_readme.outputs.README }}
run: | run: |
python - <<EOF python - <<EOF
import os import os
import requests import requests
import json import json
# 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')
readme_content = os.environ.get('README_CONTENT')
# Get PR diff
headers = { headers = {
'Authorization': f'token {token}', 'Authorization': f'token {token}',
'Accept': 'application/vnd.github.v3.diff', 'Accept': 'application/vnd.github.v3.diff',
@ -65,11 +67,29 @@ jobs:
diff_text = "" diff_text = ""
for fdata in pr_files: for fdata in pr_files:
filename = fdata['filename'] filename = fdata['filename']
patch = fdata.get('patch', '') patch = fdata.get('patch', 'No changes')
diff_text += f"File: {filename}\\nPatch:\\n{patch}\\n\\n" diff_text += f"File: {filename}\nPatch:\n"
for line in patch.split('\n'):
if line.startswith('+'):
diff_text += f"Added: {line[1:]}\n"
elif line.startswith('-'):
diff_text += f"Removed: {line[1:]}\n"
else:
diff_text += f"{line}\n"
summary_prompt = (
f"Based on the following README, provide a comprehensive analysis of the pull request. \n\n"
f"**README Content:**\n{readme_content}\n\n"
f"**Pull Request Diff:**\n{diff_text}\n\n"
f"Please include the following in your summary:\n"
f"- Key files and components modified.\n"
f"- Main purpose of the changes (e.g., bug fixes, feature additions, optimizations).\n"
f"- Specific functionalities introduced, modified, or removed.\n"
f" - Highlight lines added (marked with 'Added:') and lines removed (marked with 'Removed:').\n"
f"- Any potential implications or considerations (e.g., performance impacts, breaking changes, dependencies).\n"
f"Ensure the summary clearly states which version contains corrections or bug fixes."
)
# Generate PR summary using OpenAI
summary_prompt = f"Summarize the following pull request changes in a concise, technical manner:\\n\\n{diff_text}"
ai_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {openai_key}"} ai_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {openai_key}"}
data_summary = { data_summary = {
"model": "gpt-4o-mini", "model": "gpt-4o-mini",
@ -80,29 +100,34 @@ jobs:
summary_response.raise_for_status() summary_response.raise_for_status()
summary = summary_response.json()['choices'][0]['message']['content'].strip() summary = summary_response.json()['choices'][0]['message']['content'].strip()
# Post AI Pull Request Summary
comment_url = f"https://api.github.com/repos/{repo_full_name}/issues/{pr_number}/comments" comment_url = f"https://api.github.com/repos/{repo_full_name}/issues/{pr_number}/comments"
summary_comment = { summary_comment = {
"body": f"**AI Pull Request Summary:**\\n{summary}" "body": f"**AI Pull Request Summary:**\n{summary}"
} }
summary_comment_response = requests.post(comment_url, headers={'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3+json'}, json=summary_comment) requests.post(comment_url, headers={'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3+json'}, json=summary_comment)
summary_comment_response.raise_for_status()
print("PR Summary posted successfully.") print("PR Summary posted successfully.")
EOF EOF
code_review: code_review:
name: AI Code Review
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
# Checkout repository
- name: Checkout Repository - name: Checkout Repository
uses: actions/checkout@v4 uses: actions/checkout@v4
# Run GPT Code Reviewer (handles all code review tasks) - name: Read README.md
- name: Run GPT Code Reviewer id: read_readme_review
run: |
README_CONTENT=$(cat README.md)
echo "::set-output name=README::$README_CONTENT"
- name: AI Code Review
uses: PierreGode/GPTcode-reviewer@main uses: PierreGode/GPTcode-reviewer@main
with: with:
GITHUB_TOKEN: ${{ secrets.G_TOKEN }} GITHUB_TOKEN: ${{ secrets.G_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_API_MODEL: "gpt-4o-mini" OPENAI_API_MODEL: "gpt-4o-mini"
exclude: "**/*.json, **/*.md" exclude: "**/*.json,**/*.md"
# Assuming the action allows passing additional context, include README
additional_context: ${{ steps.read_readme_review.outputs.README }}