diff --git a/pr_agent/git_providers/bitbucket_provider.py b/pr_agent/git_providers/bitbucket_provider.py index 05a3f57a..8c9016c9 100644 --- a/pr_agent/git_providers/bitbucket_provider.py +++ b/pr_agent/git_providers/bitbucket_provider.py @@ -154,12 +154,17 @@ class BitbucketProvider(GitProvider): return diff_files - def publish_persistent_review(self, pr_comment: str): + def publish_persistent_comment(self, pr_comment: str, + initial_text="## PR Analysis", + updated_text="## PR Analysis (updated)"): try: for comment in self.pr.comments(): body = comment.raw - if '## PR Analysis' in body: - pr_comment_updated = pr_comment.replace('## PR Analysis\n', '## PR Analysis (updated)\n') + if initial_text in body: + if updated_text: + pr_comment_updated = pr_comment.replace(initial_text, updated_text) + else: + pr_comment_updated = pr_comment d = {"content": {"raw": pr_comment_updated}} response = comment._update_data(comment.put(None, data=d)) return diff --git a/pr_agent/git_providers/git_provider.py b/pr_agent/git_providers/git_provider.py index 742db30f..1f9b3e41 100644 --- a/pr_agent/git_providers/git_provider.py +++ b/pr_agent/git_providers/git_provider.py @@ -44,7 +44,7 @@ class GitProvider(ABC): def publish_comment(self, pr_comment: str, is_temporary: bool = False): pass - def publish_persistent_review(self, pr_comment: str): + def publish_persistent_comment(self, pr_comment: str): self.publish_comment(pr_comment) @abstractmethod diff --git a/pr_agent/git_providers/github_provider.py b/pr_agent/git_providers/github_provider.py index 67564b02..f0871cb1 100644 --- a/pr_agent/git_providers/github_provider.py +++ b/pr_agent/git_providers/github_provider.py @@ -154,11 +154,17 @@ class GithubProvider(GitProvider): def publish_description(self, pr_title: str, pr_body: str): self.pr.edit(title=pr_title, body=pr_body) - def publish_persistent_review(self, pr_comment: str): + def publish_persistent_comment(self, pr_comment: str, + initial_text="## PR Analysis", + updated_text="## PR Analysis (updated)"): prev_comments = list(self.pr.get_issue_comments()) for comment in prev_comments: - if comment.body.startswith('## PR Analysis'): - pr_comment_updated = pr_comment.replace('## PR Analysis\n', '## PR Analysis (updated)\n') + body = comment.body + if body.startswith(initial_text): + if updated_text: + pr_comment_updated = pr_comment.replace(initial_text, updated_text) + else: + pr_comment_updated = pr_comment response = comment.edit(pr_comment_updated) return self.publish_comment(pr_comment) diff --git a/pr_agent/git_providers/gitlab_provider.py b/pr_agent/git_providers/gitlab_provider.py index a0e117ed..e687622e 100644 --- a/pr_agent/git_providers/gitlab_provider.py +++ b/pr_agent/git_providers/gitlab_provider.py @@ -136,11 +136,16 @@ class GitLabProvider(GitProvider): except Exception as e: get_logger().exception(f"Could not update merge request {self.id_mr} description: {e}") - def publish_persistent_review(self, pr_comment: str): + def publish_persistent_comment(self, pr_comment: str, + initial_text="## PR Analysis", + updated_text="## PR Analysis (updated)"): try: for comment in self.mr.notes.list(get_all=True)[::-1]: - if comment.body.startswith('## PR Analysis'): - pr_comment_updated = pr_comment.replace('## PR Analysis\n', '## PR Analysis (updated)\n') + if comment.body.startswith(initial_text): + if updated_text: + pr_comment_updated = pr_comment.replace(initial_text, updated_text) + else: + pr_comment_updated = pr_comment response = self.mr.notes.update(comment.id, {'body': pr_comment_updated}) return except Exception as e: diff --git a/pr_agent/tools/pr_reviewer.py b/pr_agent/tools/pr_reviewer.py index 51e608a0..2106de7b 100644 --- a/pr_agent/tools/pr_reviewer.py +++ b/pr_agent/tools/pr_reviewer.py @@ -120,7 +120,7 @@ class PRReviewer: # publish the review if get_settings().pr_reviewer.persistent_comment and not self.incremental.is_incremental: - self.git_provider.publish_persistent_review(pr_comment) + self.git_provider.publish_persistent_comment(pr_comment) else: self.git_provider.publish_comment(pr_comment)