Skip to content

Commit 572e890

Browse files
authored
Merge pull request #20 from Siquan-lab/master
ai review test
2 parents 8ff5c72 + 4834467 commit 572e890

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

.github/scripts/ai_code_review.py

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@
22
import requests
33
import openai
44

5-
6-
def load_diff(file_path="pr.diff"):
7-
"""Load the diff content from file."""
8-
if not os.path.exists(file_path):
9-
print("❌ Diff file not found.")
10-
return ""
11-
with open(file_path, "r", encoding="utf-8") as f:
12-
return f.read()
13-
5+
def load_event():
6+
event_path = os.environ["GITHUB_EVENT_PATH"]
7+
with open(event_path, "r") as f:
8+
return json.load(f)
9+
10+
def get_pr_diff(diff_url, github_token):
11+
response = requests.get(diff_url, headers={
12+
"Authorization": f"token {github_token}",
13+
"Accept": "application/vnd.github.v3.diff"
14+
})
15+
response.raise_for_status()
16+
return response.text
17+
18+
19+
# def load_diff(file_path="pr.diff"):
20+
# """Load the diff content from file."""
21+
# if not os.path.exists(file_path):
22+
# print("❌ Diff file not found.")
23+
# return ""
24+
# with open(file_path, "r", encoding="utf-8") as f:
25+
# return f.read()
1426

1527
def call_gpt(diff_text):
1628
"""Call AI to review the diff."""
@@ -86,14 +98,30 @@ def post_pr_comment(review_text):
8698
else:
8799
print("✅ Review comment posted successfully.")
88100

101+
def post_comment(comment_url, body, github_token):
102+
response = requests.post(comment_url, headers={
103+
"Authorization": f"token {github_token}",
104+
"Accept": "application/vnd.github.v3+json"
105+
}, json={"body": body})
106+
response.raise_for_status()
89107

90108
def main():
91-
diff = load_diff()
92-
if not diff.strip():
93-
print("ℹ️ No diff content found. Skipping review.")
94-
return
109+
# diff = load_diff()
110+
# if not diff.strip():
111+
# print("ℹ️ No diff content found. Skipping review.")
112+
# return
113+
# review = call_gpt(diff)
114+
# post_pr_comment(review)
115+
116+
event = load_event()
117+
github_token = os.environ["GITHUB_TOKEN"]
118+
119+
diff_url = event["pull_request"]["diff_url"]
120+
comments_url = event["pull_request"]["comments_url"]
121+
122+
diff = get_pr_diff(diff_url, github_token)
95123
review = call_gpt(diff)
96-
post_pr_comment(review)
124+
post_comment(comments_url, f"🤖 **AI review feedback:**\n\n{review}", github_token)
97125

98126

99127
if __name__ == "__main__":

.github/workflows/ai_code_review.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
name: AI Code Review on PR
22

33
on:
4-
pull_request:
4+
pull_request_target:
55
types: [opened, synchronize, reopened]
66

7+
permissions:
8+
contents: read
9+
pull-requests: write
10+
711
jobs:
812
AI-Review:
913
runs-on: ubuntu-latest
10-
permissions:
11-
contents: read
12-
pull-requests: write
13-
id-token: write
14-
secrets: inherit
1514

1615
steps:
17-
- name: ✅ Checkout PR code
18-
uses: actions/checkout@v3
16+
# - name: ✅ Checkout PR code
17+
# uses: actions/checkout@v3
1918

2019
- name: 🔍 Debug workspace structure
2120
run: ls -R .github
2221

2322
- name: 🐍 Set up Python
24-
uses: actions/setup-python@v4
23+
uses: actions/setup-python@v5
2524
with:
2625
python-version: '3.10'
2726

0 commit comments

Comments
 (0)