-
Notifications
You must be signed in to change notification settings - Fork 4
134 lines (115 loc) · 4.81 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
130
131
132
133
134
name: PR summary by AI
on:
pull_request:
types:
- opened
- synchronize
- reopened
permissions:
contents: read
pull-requests: write
issues: write
jobs:
pr_summary:
name: PR Summary
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Read README.md
id: read_readme
run: |
README_CONTENT=$(cat README.md)
echo "::set-output name=README::$README_CONTENT"
- 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: PR Summary
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
GITHUB_TOKEN: ${{ secrets.G_TOKEN }}
README_CONTENT: ${{ steps.read_readme.outputs.README }}
run: |
python - <<EOF
import os
import requests
import json
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')
readme_content = os.environ.get('README_CONTENT')
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()
diff_text = ""
for fdata in pr_files:
filename = fdata['filename']
patch = fdata.get('patch', 'No changes')
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."
)
ai_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {openai_key}"}
data_summary = {
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": summary_prompt}],
"temperature": 0.7
}
summary_response = requests.post("https://api.openai.com/v1/chat/completions", headers=ai_headers, json=data_summary)
summary_response.raise_for_status()
summary = summary_response.json()['choices'][0]['message']['content'].strip()
comment_url = f"https://api.github.com/repos/{repo_full_name}/issues/{pr_number}/comments"
summary_comment = {
"body": f"**AI Pull Request Summary:**\n{summary}"
}
requests.post(comment_url, headers={'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3+json'}, json=summary_comment)
print("PR Summary posted successfully.")
EOF
code_review:
name: AI Code Review
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Read README.md
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
with:
GITHUB_TOKEN: ${{ secrets.G_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_API_MODEL: "gpt-4o-mini"
exclude: "**/*.json,**/*.md"
# Assuming the action allows passing additional context, include README
additional_context: ${{ steps.read_readme_review.outputs.README }}