Skip to content

Commit 601f5a2

Browse files
Update copilot.py
1 parent 4ac696c commit 601f5a2

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

etc/tool/copilot.py

+14-24
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
sys.path.append(str(Path(__file__).parent.parent.parent))
1010

1111
# GitHub API credentials
12-
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") # Used for commenting and requesting changes
12+
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
1313
BOT_GITHUB_TOKEN = os.getenv("BOT") # Used for approvals
1414
GITHUB_REPOSITORY = os.getenv("GITHUB_REPOSITORY")
1515
PR_NUMBER = os.getenv("PR_NUMBER")
@@ -62,7 +62,7 @@ def analyze_file_contents(file_contents):
6262
if domain in line:
6363
issues.append({
6464
"line": i,
65-
"issue": f"Forbidden domain `{domain}` found.",
65+
"issue": f"Forbidden domain `{domain}` found.",
6666
"fix": f"Replace `{domain}` with an allowed domain like `is-epic.me` or `is-awsm.tech`."
6767
})
6868

@@ -106,33 +106,26 @@ def ai_review_pr(pr_body, changed_files, file_contents):
106106

107107
decision = response.get("content", "").strip() if isinstance(response, dict) else response.strip()
108108

109-
# If AI fails or response is empty, request changes automatically
110109
if not decision:
111-
print("❌ AI response is empty or invalid. Defaulting to 'request changes'.")
112110
return "request changes", ["AI review failed. Please manually check."]
113111

114-
# If AI finds issues, extract structured comments
115112
if "consider" in decision.lower() or "avoid" in decision.lower():
116113
return "request changes", decision.split("\n")
117114

118115
return "approve", []
119116

120117
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}"]
123119

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)
129124

130125
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"])
136129

137130
def approve_pr(pr):
138131
"""Approves the PR using the bot's token."""
@@ -141,7 +134,6 @@ def approve_pr(pr):
141134
bot_pr = bot_repo.get_pull(int(PR_NUMBER))
142135

143136
bot_pr.create_review(event="APPROVE", body="✅ AI Code Reviewer (Bot) has approved this PR.")
144-
print("✅ PR Approved by AI (Using Bot Token)")
145137

146138
def main():
147139
github = Github(GITHUB_TOKEN)
@@ -154,24 +146,22 @@ def main():
154146
for file in changed_files:
155147
file_contents = fetch_file_content(repo, file, pr)
156148

157-
# Check for syntax errors in JSON files
158149
if file.endswith(".json"):
159150
syntax_error = check_json_syntax(file_contents)
160151
if syntax_error:
161152
all_issues.append({"line": "N/A", "issue": f"Invalid JSON syntax: {syntax_error}", "fix": "Fix the JSON structure."})
162153

163-
# Domain validation and other checks
164154
issues = analyze_file_contents(file_contents)
165155
if issues:
166156
all_issues.extend(issues)
167157

168-
# AI Review for extra validation
169158
ai_decision, ai_comments = ai_review_pr(pr.body, changed_files, file_contents)
170159

171-
# Request changes if issues exist
172160
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+
175165
else:
176166
approve_pr(pr)
177167

0 commit comments

Comments
 (0)