9
9
sys .path .append (str (Path (__file__ ).parent .parent .parent ))
10
10
11
11
# GitHub API credentials
12
- GITHUB_TOKEN = os .getenv ("GITHUB_TOKEN" ) # Used for commenting and requesting changes
12
+ GITHUB_TOKEN = os .getenv ("GITHUB_TOKEN" )
13
13
BOT_GITHUB_TOKEN = os .getenv ("BOT" ) # Used for approvals
14
14
GITHUB_REPOSITORY = os .getenv ("GITHUB_REPOSITORY" )
15
15
PR_NUMBER = os .getenv ("PR_NUMBER" )
@@ -62,7 +62,7 @@ def analyze_file_contents(file_contents):
62
62
if domain in line :
63
63
issues .append ({
64
64
"line" : i ,
65
- "issue" : f"Forbidden domain `{ domain } ` found." ,
65
+ "issue" : f"❌ Forbidden domain `{ domain } ` found." ,
66
66
"fix" : f"Replace `{ domain } ` with an allowed domain like `is-epic.me` or `is-awsm.tech`."
67
67
})
68
68
@@ -106,33 +106,26 @@ def ai_review_pr(pr_body, changed_files, file_contents):
106
106
107
107
decision = response .get ("content" , "" ).strip () if isinstance (response , dict ) else response .strip ()
108
108
109
- # If AI fails or response is empty, request changes automatically
110
109
if not decision :
111
- print ("❌ AI response is empty or invalid. Defaulting to 'request changes'." )
112
110
return "request changes" , ["AI review failed. Please manually check." ]
113
111
114
- # If AI finds issues, extract structured comments
115
112
if "consider" in decision .lower () or "avoid" in decision .lower ():
116
113
return "request changes" , decision .split ("\n " )
117
114
118
115
return "approve" , []
119
116
120
117
except Exception as e :
121
- print (f"❌ AI review failed: { e } " )
122
- return "request changes" , ["AI review failed. Please manually check." ]
118
+ return "request changes" , [f"AI review failed: { e } " ]
123
119
124
- def post_comment (pr , message ):
125
- """Posts a comment on the PR."""
126
- existing_comments = [comment .body for comment in pr .get_issue_comments ()]
127
- if message not in existing_comments and message .strip ():
128
- pr .create_issue_comment (message )
120
+ def post_line_comment (pr , filename , line , issue , fix ):
121
+ """Posts a comment on a specific line in a PR."""
122
+ body = f"**Issue:** { issue } \n **Suggested Fix:** { fix } "
123
+ pr .create_review_comment (body , pr .head .sha , filename , line )
129
124
130
125
def request_changes (pr , issues , filename ):
131
- """Requests changes on the PR and comments on how to fix them."""
132
- formatted_issues = "\n \n " .join ([f"- **Line { issue ['line' ]} :** { issue ['issue' ]} \n - **Suggested Fix:** { issue ['fix' ]} " for issue in issues ])
133
-
134
- pr .create_review (event = "REQUEST_CHANGES" , body = f"⚠️ AI Review found issues in `{ filename } `. See comments for fixes." )
135
- post_comment (pr , f"⚠️ **AI Review suggests changes for `{ filename } `:**\n \n { formatted_issues } " )
126
+ """Requests changes on the PR and comments per line."""
127
+ for issue in issues :
128
+ post_line_comment (pr , filename , issue ["line" ], issue ["issue" ], issue ["fix" ])
136
129
137
130
def approve_pr (pr ):
138
131
"""Approves the PR using the bot's token."""
@@ -141,7 +134,6 @@ def approve_pr(pr):
141
134
bot_pr = bot_repo .get_pull (int (PR_NUMBER ))
142
135
143
136
bot_pr .create_review (event = "APPROVE" , body = "✅ AI Code Reviewer (Bot) has approved this PR." )
144
- print ("✅ PR Approved by AI (Using Bot Token)" )
145
137
146
138
def main ():
147
139
github = Github (GITHUB_TOKEN )
@@ -154,24 +146,22 @@ def main():
154
146
for file in changed_files :
155
147
file_contents = fetch_file_content (repo , file , pr )
156
148
157
- # Check for syntax errors in JSON files
158
149
if file .endswith (".json" ):
159
150
syntax_error = check_json_syntax (file_contents )
160
151
if syntax_error :
161
152
all_issues .append ({"line" : "N/A" , "issue" : f"Invalid JSON syntax: { syntax_error } " , "fix" : "Fix the JSON structure." })
162
153
163
- # Domain validation and other checks
164
154
issues = analyze_file_contents (file_contents )
165
155
if issues :
166
156
all_issues .extend (issues )
167
157
168
- # AI Review for extra validation
169
158
ai_decision , ai_comments = ai_review_pr (pr .body , changed_files , file_contents )
170
159
171
- # Request changes if issues exist
172
160
if all_issues or ai_decision == "request changes" :
173
- request_changes (pr , all_issues , "Multiple Files" )
174
- post_comment (pr , "\n " .join (ai_comments ))
161
+ for file in changed_files :
162
+ request_changes (pr , all_issues , file )
163
+ pr .create_review (event = "REQUEST_CHANGES" , body = "⚠️ AI Review found issues. See comments for fixes." )
164
+
175
165
else :
176
166
approve_pr (pr )
177
167
0 commit comments