mirror of
https://github.com/qodo-ai/pr-agent.git
synced 2025-07-05 05:10:38 +08:00
feat: Refactor comment verification in github_provider.py
This commit is contained in:
@ -1,3 +1,4 @@
|
|||||||
|
import time
|
||||||
import hashlib
|
import hashlib
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Optional, Tuple
|
from typing import Optional, Tuple
|
||||||
@ -223,6 +224,7 @@ class GithubProvider(GitProvider):
|
|||||||
|
|
||||||
def publish_inline_comments(self, comments: list[dict]):
|
def publish_inline_comments(self, comments: list[dict]):
|
||||||
try:
|
try:
|
||||||
|
# publish all comments in a single message
|
||||||
self.pr.create_review(commit=self.last_commit_id, comments=comments)
|
self.pr.create_review(commit=self.last_commit_id, comments=comments)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if get_settings().config.verbosity_level >= 2:
|
if get_settings().config.verbosity_level >= 2:
|
||||||
@ -260,27 +262,35 @@ class GithubProvider(GitProvider):
|
|||||||
elif get_settings().config.verbosity_level >= 2:
|
elif get_settings().config.verbosity_level >= 2:
|
||||||
get_logger().error("Dropped all comments - no verified comments left to publish")
|
get_logger().error("Dropped all comments - no verified comments left to publish")
|
||||||
|
|
||||||
|
def _verif_comment(self, comment: dict):
|
||||||
|
is_verified = False
|
||||||
|
e = None
|
||||||
|
try:
|
||||||
|
headers, data = self.pr._requester.requestJsonAndCheck(
|
||||||
|
"POST", f"{self.pr.url}/reviews", input=dict(commit_id=self.last_commit_id.sha, comments=[comment])
|
||||||
|
)
|
||||||
|
pending_review_id = data["id"]
|
||||||
|
is_verified = True
|
||||||
|
except Exception as e:
|
||||||
|
is_verified = False
|
||||||
|
pending_review_id = None
|
||||||
|
if pending_review_id is not None:
|
||||||
|
try:
|
||||||
|
self.pr._requester.requestJsonAndCheck("DELETE", f"{self.pr.url}/reviews/{pending_review_id}")
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
return is_verified, e
|
||||||
def _verify_inline_comments(self, comments: list[dict]) -> tuple[list[dict], list[tuple[dict, Exception]]]:
|
def _verify_inline_comments(self, comments: list[dict]) -> tuple[list[dict], list[tuple[dict, Exception]]]:
|
||||||
"""Very each comment against the GitHub API and return 2 lists: 1 of verified and 1 of invalid comments"""
|
"""Very each comment against the GitHub API and return 2 lists: 1 of verified and 1 of invalid comments"""
|
||||||
import time
|
|
||||||
verified_comments = []
|
verified_comments = []
|
||||||
invalid_comments = []
|
invalid_comments = []
|
||||||
for comment in comments:
|
for comment in comments:
|
||||||
time.sleep(1) # for avoiding secondary rate limit
|
time.sleep(1) # for avoiding secondary rate limit
|
||||||
try:
|
is_verified, e = self._verif_comment(comment)
|
||||||
headers, data = self.pr._requester.requestJsonAndCheck(
|
if is_verified:
|
||||||
"POST", f"{self.pr.url}/reviews", input=dict(commit_id=self.last_commit_id.sha, comments=[comment])
|
|
||||||
)
|
|
||||||
pending_review_id = data["id"]
|
|
||||||
verified_comments.append(comment)
|
verified_comments.append(comment)
|
||||||
except Exception as e:
|
else:
|
||||||
invalid_comments.append((comment, e))
|
invalid_comments.append((comment, e))
|
||||||
pending_review_id = None
|
|
||||||
if pending_review_id is not None:
|
|
||||||
try:
|
|
||||||
self.pr._requester.requestJsonAndCheck("DELETE", f"{self.pr.url}/reviews/{pending_review_id}")
|
|
||||||
except Exception as e:
|
|
||||||
pass
|
|
||||||
return verified_comments, invalid_comments
|
return verified_comments, invalid_comments
|
||||||
|
|
||||||
def _try_fix_invalid_inline_comments(self, invalid_comments: list[dict]) -> list[dict]:
|
def _try_fix_invalid_inline_comments(self, invalid_comments: list[dict]) -> list[dict]:
|
||||||
|
Reference in New Issue
Block a user