|
2 | 2 | import requests
|
3 | 3 | import openai
|
4 | 4 |
|
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() |
14 | 26 |
|
15 | 27 | def call_gpt(diff_text):
|
16 | 28 | """Call AI to review the diff."""
|
@@ -86,14 +98,30 @@ def post_pr_comment(review_text):
|
86 | 98 | else:
|
87 | 99 | print("✅ Review comment posted successfully.")
|
88 | 100 |
|
| 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() |
89 | 107 |
|
90 | 108 | 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) |
95 | 123 | review = call_gpt(diff)
|
96 |
| - post_pr_comment(review) |
| 124 | + post_comment(comments_url, f"🤖 **AI review feedback:**\n\n{review}", github_token) |
97 | 125 |
|
98 | 126 |
|
99 | 127 | if __name__ == "__main__":
|
|
0 commit comments