2
2
import requests
3
3
import openai
4
4
5
- def load_diff (file_path = 'pr.diff' ):
5
+
6
+ def load_diff (file_path = "pr.diff" ):
6
7
"""Load the diff content from file."""
7
8
if not os .path .exists (file_path ):
8
9
print ("❌ Diff file not found." )
9
10
return ""
10
- with open (file_path , 'r' , encoding = ' utf-8' ) as f :
11
+ with open (file_path , "r" , encoding = " utf-8" ) as f :
11
12
return f .read ()
12
13
14
+
13
15
def call_gpt (diff_text ):
14
16
"""Call AI to review the diff."""
15
- api_key = os .getenv ("OPENAI_API_KEY" )
17
+ api_key = os .environ . get ("OPENAI_API_KEY" )
16
18
if not api_key :
17
19
raise ValueError ("❌ OPENAI_API_KEY is not set." )
18
20
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
+ )
20
26
21
27
prompt = f"""
22
28
You are a senior Java code reviewer.
@@ -52,12 +58,13 @@ def call_gpt(diff_text):
52
58
model = "gpt-4o" ,
53
59
messages = [
54
60
{"role" : "system" , "content" : "You are a Java code review expert." },
55
- {"role" : "user" , "content" : prompt }
61
+ {"role" : "user" , "content" : prompt },
56
62
],
57
- temperature = 0.2
63
+ temperature = 0.2 ,
58
64
)
59
65
return response .choices [0 ].message .content
60
66
67
+
61
68
def post_pr_comment (review_text ):
62
69
"""Post review result as a comment on the PR."""
63
70
repo = os .getenv ("GITHUB_REPOSITORY" )
@@ -67,7 +74,7 @@ def post_pr_comment(review_text):
67
74
url = f"https://api.github.com/repos/{ repo } /issues/{ pr_number } /comments"
68
75
headers = {
69
76
"Authorization" : f"token { token } " ,
70
- "Accept" : "application/vnd.github.v3+json"
77
+ "Accept" : "application/vnd.github.v3+json" ,
71
78
}
72
79
payload = {"body" : review_text }
73
80
@@ -79,6 +86,7 @@ def post_pr_comment(review_text):
79
86
else :
80
87
print ("✅ Review comment posted successfully." )
81
88
89
+
82
90
def main ():
83
91
diff = load_diff ()
84
92
if not diff .strip ():
@@ -87,6 +95,6 @@ def main():
87
95
review = call_gpt (diff )
88
96
post_pr_comment (review )
89
97
98
+
90
99
if __name__ == "__main__" :
91
100
main ()
92
-
0 commit comments