1
1
import os
2
- import requests
3
2
import openai
3
+ from github import Github
4
4
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
5
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
6
+ # def load_event():
7
+ # event_path = os.environ["GITHUB_EVENT_PATH"]
8
+ # with open(event_path, "r") as f:
9
+ # return json.load(f)
10
+ #
11
+ # def get_pr_diff(diff_url, github_token):
12
+ # response = requests.get(diff_url, headers={
13
+ # "Authorization": f"token {github_token}",
14
+ # "Accept": "application/vnd.github.v3.diff"
15
+ # })
16
+ # response.raise_for_status()
17
+ # return response.text
17
18
18
19
19
20
# def load_diff(file_path="pr.diff"):
@@ -24,20 +25,41 @@ def get_pr_diff(diff_url, github_token):
24
25
# with open(file_path, "r", encoding="utf-8") as f:
25
26
# return f.read()
26
27
27
- def call_gpt (diff_text ):
28
+ OPENAI_API_KEY = os .environ ['OPENAI_API_KEY' ]
29
+ OPENAI_API_ENDPOINT = os .environ .get ('OPENAI_API_ENDPOINT' )
30
+ GITHUB_TOKEN = os .environ ['GITHUB_TOKEN' ]
31
+ PR_NUMBER = int (os .environ ['PR_NUMBER' ])
32
+ REPO_NAME = os .environ ['REPO' ]
33
+
34
+ def call_gpt ():
28
35
"""Call AI to review the diff."""
29
- api_key = os . environ . get ( " OPENAI_API_KEY" )
36
+ api_key = OPENAI_API_KEY
30
37
if not api_key :
31
38
raise ValueError ("❌ OPENAI_API_KEY is not set." )
32
39
33
- api_endpoint = os . environ . get ( " OPENAI_API_ENDPOINT" )
40
+ api_endpoint = OPENAI_API_ENDPOINT
34
41
35
42
client = openai .OpenAI (
36
43
api_key = api_key ,
37
44
base_url = api_endpoint ,
38
45
default_headers = {"api-key" : api_key },
39
46
)
40
47
48
+ gh = Github (GITHUB_TOKEN )
49
+ repo = gh .get_repo (REPO_NAME )
50
+ pr = repo .get_pull (PR_NUMBER )
51
+
52
+ diff = pr .patch_url
53
+ files = pr .get_files ()
54
+ changed_files = []
55
+ for f in files :
56
+ if f .status != "removed" :
57
+ file_content = f"File: { f .filename } \n Patch:\n { f .patch } \n "
58
+ changed_files .append (file_content )
59
+
60
+ diff_text = "\n \n " .join (changed_files )
61
+
62
+
41
63
prompt = f"""
42
64
You are a senior Java code reviewer.
43
65
@@ -76,36 +98,33 @@ def call_gpt(diff_text):
76
98
],
77
99
temperature = 0.2 ,
78
100
)
79
- return response .choices [0 ].message .content
80
-
81
-
82
- def post_pr_comment (review_text ):
83
- """Post review result as a comment on the PR."""
84
- repo = os .getenv ("GITHUB_REPOSITORY" )
85
- pr_number = os .getenv ("PR_NUMBER" )
86
- token = os .getenv ("GITHUB_TOKEN" )
87
-
88
- url = f"https://api.github.com/repos/{ repo } /issues/{ pr_number } /comments"
89
- headers = {
90
- "Authorization" : f"token { token } " ,
91
- "Accept" : "application/vnd.github.v3+json" ,
92
- }
93
- payload = {"body" : review_text }
94
-
95
- print ("📝 Posting review comment to PR..." )
96
- response = requests .post (url , headers = headers , json = payload )
97
- if response .status_code != 201 :
98
- print (f"❌ Failed to post comment: { response .status_code } " )
99
- print (response .text )
100
- else :
101
- print ("✅ Review comment posted successfully." )
102
-
103
- def post_comment (comment_url , body , github_token ):
104
- response = requests .post (comment_url , headers = {
105
- "Authorization" : f"token { github_token } " ,
106
- "Accept" : "application/vnd.github.v3+json"
107
- }, json = {"body" : body })
108
- response .raise_for_status ()
101
+ review_comment = response .choices [0 ].message .content
102
+ pr .create_issue_comment (f"🤖 **AI Code Review:**\n \n { review_comment } " )
103
+ print ("✅ Review comment posted." )
104
+
105
+
106
+
107
+ # def post_pr_comment(review_text):
108
+ # """Post review result as a comment on the PR."""
109
+ # repo = os.getenv("GITHUB_REPOSITORY")
110
+ # pr_number = os.getenv("PR_NUMBER")
111
+ # token = os.getenv("GITHUB_TOKEN")
112
+ #
113
+ # url = f"https://api.github.com/repos/{repo}/issues/{pr_number}/comments"
114
+ # headers = {
115
+ # "Authorization": f"token {token}",
116
+ # "Accept": "application/vnd.github.v3+json",
117
+ # }
118
+ # payload = {"body": review_text}
119
+ #
120
+ # print("📝 Posting review comment to PR...")
121
+ # response = requests.post(url, headers=headers, json=payload)
122
+ # if response.status_code != 201:
123
+ # print(f"❌ Failed to post comment: {response.status_code}")
124
+ # print(response.text)
125
+ # else:
126
+ # print("✅ Review comment posted successfully.")
127
+
109
128
110
129
def main ():
111
130
# diff = load_diff()
0 commit comments