Azure: return Comment object when creating comment

This commit is contained in:
Thomas De Keulenaer
2025-05-09 11:58:24 +02:00
parent 36307e66f1
commit e516d66c1c
2 changed files with 18 additions and 24 deletions

View File

@ -18,14 +18,10 @@ ADO_APP_CLIENT_DEFAULT_ID = "499b84ac-1321-427f-aa17-267ca6975798/.default"
MAX_PR_DESCRIPTION_AZURE_LENGTH = 4000-1 MAX_PR_DESCRIPTION_AZURE_LENGTH = 4000-1
try: try:
# noinspection PyUnresolvedReferences
# noinspection PyUnresolvedReferences # noinspection PyUnresolvedReferences
from azure.devops.connection import Connection from azure.devops.connection import Connection
# noinspection PyUnresolvedReferences # noinspection PyUnresolvedReferences
from azure.devops.v7_1.git.models import (Comment, CommentThread, from azure.devops.released.git import (Comment, CommentThread, GitPullRequest, GitVersionDescriptor, GitClient)
GitPullRequest,
GitPullRequestIterationChanges,
GitVersionDescriptor)
# noinspection PyUnresolvedReferences # noinspection PyUnresolvedReferences
from azure.identity import DefaultAzureCredential from azure.identity import DefaultAzureCredential
from msrest.authentication import BasicAuthentication from msrest.authentication import BasicAuthentication
@ -121,31 +117,29 @@ class AzureDevopsProvider(GitProvider):
get_logger().warning(f"Azure failed to publish code suggestion, error: {e}") get_logger().warning(f"Azure failed to publish code suggestion, error: {e}")
return True return True
def get_pr_description_full(self) -> str: def get_pr_description_full(self) -> str:
return self.pr.description return self.pr.description
def edit_comment(self, comment, body: str): def edit_comment(self, comment: Comment, body: str):
try: try:
self.azure_devops_client.update_comment( self.azure_devops_client.update_comment(
repository_id=self.repo_slug, repository_id=self.repo_slug,
pull_request_id=self.pr_num, pull_request_id=self.pr_num,
thread_id=comment["thread_id"], thread_id=comment.thread_id,
comment_id=comment["comment_id"], comment_id=comment.id,
comment=Comment(content=body), comment=Comment(content=body),
project=self.workspace_slug, project=self.workspace_slug,
) )
except Exception as e: except Exception as e:
get_logger().exception(f"Failed to edit comment, error: {e}") get_logger().exception(f"Failed to edit comment, error: {e}")
def remove_comment(self, comment): def remove_comment(self, comment: Comment):
try: try:
self.azure_devops_client.delete_comment( self.azure_devops_client.delete_comment(
repository_id=self.repo_slug, repository_id=self.repo_slug,
pull_request_id=self.pr_num, pull_request_id=self.pr_num,
thread_id=comment["thread_id"], thread_id=comment.thread_id,
comment_id=comment["comment_id"], comment_id=comment.id,
project=self.workspace_slug, project=self.workspace_slug,
) )
except Exception as e: except Exception as e:
@ -378,7 +372,7 @@ class AzureDevopsProvider(GitProvider):
get_logger().exception(f"Failed to get diff files, error: {e}") get_logger().exception(f"Failed to get diff files, error: {e}")
return [] return []
def publish_comment(self, pr_comment: str, is_temporary: bool = False, thread_context=None): def publish_comment(self, pr_comment: str, is_temporary: bool = False, thread_context=None) -> Comment:
if is_temporary and not get_settings().config.publish_output_progress: if is_temporary and not get_settings().config.publish_output_progress:
get_logger().debug(f"Skipping publish_comment for temporary comment: {pr_comment}") get_logger().debug(f"Skipping publish_comment for temporary comment: {pr_comment}")
return None return None
@ -390,10 +384,11 @@ class AzureDevopsProvider(GitProvider):
repository_id=self.repo_slug, repository_id=self.repo_slug,
pull_request_id=self.pr_num, pull_request_id=self.pr_num,
) )
response = {"thread_id": thread_response.id, "comment_id": thread_response.comments[0].id} created_comment = thread_response.comments[0]
created_comment.thread_id = thread_response.id
if is_temporary: if is_temporary:
self.temp_comments.append(response) self.temp_comments.append(created_comment)
return response return created_comment
def publish_description(self, pr_title: str, pr_body: str): def publish_description(self, pr_title: str, pr_body: str):
if len(pr_body) > MAX_PR_DESCRIPTION_AZURE_LENGTH: if len(pr_body) > MAX_PR_DESCRIPTION_AZURE_LENGTH:
@ -522,7 +517,7 @@ class AzureDevopsProvider(GitProvider):
def get_user_id(self): def get_user_id(self):
return 0 return 0
def get_issue_comments(self): def get_issue_comments(self) -> list[Comment]:
threads = self.azure_devops_client.get_threads(repository_id=self.repo_slug, pull_request_id=self.pr_num, project=self.workspace_slug) threads = self.azure_devops_client.get_threads(repository_id=self.repo_slug, pull_request_id=self.pr_num, project=self.workspace_slug)
threads.reverse() threads.reverse()
comment_list = [] comment_list = []
@ -562,7 +557,7 @@ class AzureDevopsProvider(GitProvider):
return workspace_slug, repo_slug, pr_number return workspace_slug, repo_slug, pr_number
@staticmethod @staticmethod
def _get_azure_devops_client(): def _get_azure_devops_client() -> GitClient:
org = get_settings().azure_devops.get("org", None) org = get_settings().azure_devops.get("org", None)
pat = get_settings().azure_devops.get("pat", None) pat = get_settings().azure_devops.get("pat", None)

View File

@ -228,7 +228,7 @@ class GitProvider(ABC):
update_header: bool = True, update_header: bool = True,
name='review', name='review',
final_update_message=True): final_update_message=True):
self.publish_comment(pr_comment) return self.publish_comment(pr_comment)
def publish_persistent_comment_full(self, pr_comment: str, def publish_persistent_comment_full(self, pr_comment: str,
initial_header: str, initial_header: str,
@ -250,14 +250,13 @@ class GitProvider(ABC):
# response = self.mr.notes.update(comment.id, {'body': pr_comment_updated}) # response = self.mr.notes.update(comment.id, {'body': pr_comment_updated})
self.edit_comment(comment, pr_comment_updated) self.edit_comment(comment, pr_comment_updated)
if final_update_message: if final_update_message:
self.publish_comment( return self.publish_comment(
f"**[Persistent {name}]({comment_url})** updated to latest commit {latest_commit_url}") f"**[Persistent {name}]({comment_url})** updated to latest commit {latest_commit_url}")
return return comment
except Exception as e: except Exception as e:
get_logger().exception(f"Failed to update persistent review, error: {e}") get_logger().exception(f"Failed to update persistent review, error: {e}")
pass pass
self.publish_comment(pr_comment) return self.publish_comment(pr_comment)
@abstractmethod @abstractmethod
def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str, original_suggestion=None): def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str, original_suggestion=None):