11import os
2- import requests
32import openai
3+ from github import Github
44
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 )
95
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
1718
1819
1920# def load_diff(file_path="pr.diff"):
@@ -24,20 +25,41 @@ def get_pr_diff(diff_url, github_token):
2425# with open(file_path, "r", encoding="utf-8") as f:
2526# return f.read()
2627
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 ():
2835 """Call AI to review the diff."""
29- api_key = os . environ . get ( " OPENAI_API_KEY" )
36+ api_key = OPENAI_API_KEY
3037 if not api_key :
3138 raise ValueError ("❌ OPENAI_API_KEY is not set." )
3239
33- api_endpoint = os . environ . get ( " OPENAI_API_ENDPOINT" )
40+ api_endpoint = OPENAI_API_ENDPOINT
3441
3542 client = openai .OpenAI (
3643 api_key = api_key ,
3744 base_url = api_endpoint ,
3845 default_headers = {"api-key" : api_key },
3946 )
4047
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+
4163 prompt = f"""
4264You are a senior Java code reviewer.
4365
@@ -76,36 +98,33 @@ def call_gpt(diff_text):
7698 ],
7799 temperature = 0.2 ,
78100 )
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+
109128
110129def main ():
111130# diff = load_diff()
0 commit comments