Skip to content

Commit a59d442

Browse files
author
siquanlv
committed
ai review test
1 parent 2cc89e9 commit a59d442

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

.github/scripts/ai_code_review.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@
22
import requests
33
import openai
44

5-
def load_diff(file_path='pr.diff'):
5+
6+
def load_diff(file_path="pr.diff"):
67
"""Load the diff content from file."""
78
if not os.path.exists(file_path):
89
print("❌ Diff file not found.")
910
return ""
10-
with open(file_path, 'r', encoding='utf-8') as f:
11+
with open(file_path, "r", encoding="utf-8") as f:
1112
return f.read()
1213

14+
1315
def call_gpt(diff_text):
1416
"""Call AI to review the diff."""
15-
api_key = os.getenv("OPENAI_API_KEY")
17+
api_key = os.environ.get("OPENAI_API_KEY")
1618
if not api_key:
1719
raise ValueError("❌ OPENAI_API_KEY is not set.")
1820

19-
client = openai.OpenAI(api_key=api_key, base_url='https://llm-proxy.us-east-2.int.infra.intelligence.webex.com/azure/v1?api-version=2024-10-21',default_headers={"api-key": api_key})
21+
client = openai.OpenAI(
22+
api_key=api_key,
23+
base_url="https://llm-proxy.us-east-2.int.infra.intelligence.webex.com/azure/v1?api-version=2024-10-21",
24+
default_headers={"api-key": api_key},
25+
)
2026

2127
prompt = f"""
2228
You are a senior Java code reviewer.
@@ -52,12 +58,13 @@ def call_gpt(diff_text):
5258
model="gpt-4o",
5359
messages=[
5460
{"role": "system", "content": "You are a Java code review expert."},
55-
{"role": "user", "content": prompt}
61+
{"role": "user", "content": prompt},
5662
],
57-
temperature=0.2
63+
temperature=0.2,
5864
)
5965
return response.choices[0].message.content
6066

67+
6168
def post_pr_comment(review_text):
6269
"""Post review result as a comment on the PR."""
6370
repo = os.getenv("GITHUB_REPOSITORY")
@@ -67,7 +74,7 @@ def post_pr_comment(review_text):
6774
url = f"https://api.github.com/repos/{repo}/issues/{pr_number}/comments"
6875
headers = {
6976
"Authorization": f"token {token}",
70-
"Accept": "application/vnd.github.v3+json"
77+
"Accept": "application/vnd.github.v3+json",
7178
}
7279
payload = {"body": review_text}
7380

@@ -79,6 +86,7 @@ def post_pr_comment(review_text):
7986
else:
8087
print("✅ Review comment posted successfully.")
8188

89+
8290
def main():
8391
diff = load_diff()
8492
if not diff.strip():
@@ -87,6 +95,6 @@ def main():
8795
review = call_gpt(diff)
8896
post_pr_comment(review)
8997

98+
9099
if __name__ == "__main__":
91100
main()
92-

0 commit comments

Comments
 (0)