feat: Refactor comment verification in github_provider.py

This commit is contained in:
mrT23
2024-01-14 10:49:05 +02:00
parent b9951fce62
commit d6f4c1638d

View File

@ -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 _verify_inline_comments(self, comments: list[dict]) -> tuple[list[dict], list[tuple[dict, Exception]]]: def _verif_comment(self, comment: dict):
"""Very each comment against the GitHub API and return 2 lists: 1 of verified and 1 of invalid comments""" is_verified = False
import time e = None
verified_comments = []
invalid_comments = []
for comment in comments:
time.sleep(1) # for avoiding secondary rate limit
try: try:
headers, data = self.pr._requester.requestJsonAndCheck( headers, data = self.pr._requester.requestJsonAndCheck(
"POST", f"{self.pr.url}/reviews", input=dict(commit_id=self.last_commit_id.sha, comments=[comment]) "POST", f"{self.pr.url}/reviews", input=dict(commit_id=self.last_commit_id.sha, comments=[comment])
) )
pending_review_id = data["id"] pending_review_id = data["id"]
verified_comments.append(comment) is_verified = True
except Exception as e: except Exception as e:
invalid_comments.append((comment, e)) is_verified = False
pending_review_id = None pending_review_id = None
if pending_review_id is not None: if pending_review_id is not None:
try: try:
self.pr._requester.requestJsonAndCheck("DELETE", f"{self.pr.url}/reviews/{pending_review_id}") self.pr._requester.requestJsonAndCheck("DELETE", f"{self.pr.url}/reviews/{pending_review_id}")
except Exception as e: except Exception as e:
pass pass
return is_verified, e
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"""
verified_comments = []
invalid_comments = []
for comment in comments:
time.sleep(1) # for avoiding secondary rate limit
is_verified, e = self._verif_comment(comment)
if is_verified:
verified_comments.append(comment)
else:
invalid_comments.append((comment, e))
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]: