Skip to content

Commit 35f4c57

Browse files
committed
EX-322: update logic for comment deleted/if no issuses find
1 parent 2cb0b4b commit 35f4c57

1 file changed

Lines changed: 47 additions & 14 deletions

File tree

src/code_review_agent/bitbucket_client.py

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def cleanup_and_post_all_comments(all_issues: list[CodeIssue], files_with_issues
6262
url = f"{base_url}/comments"
6363

6464
while url:
65-
response = requests.get(url, auth=auth)
65+
response = requests.get(url, auth=auth, headers=headers)
6666
response.raise_for_status()
6767
data = response.json()
6868
all_old_comments.extend(data.get('values', []))
@@ -72,24 +72,57 @@ def cleanup_and_post_all_comments(all_issues: list[CodeIssue], files_with_issues
7272

7373
bot_comments = [
7474
comment for comment in all_old_comments
75-
if (comment.get('user', {}).get('account_id') == bot_account_id) or \
76-
(comment.get('user', {}).get('uuid') == bot_account_id)
75+
if (comment.get('user', {}).get('account_id') == bot_account_id)
7776
]
7877

78+
parent_comment_ids = {
79+
comment['parent']['id']
80+
for comment in all_old_comments
81+
if 'parent' in comment and comment.get('parent')
82+
}
83+
84+
comments_to_delete = [
85+
comment for comment in bot_comments
86+
if comment['id'] not in parent_comment_ids
87+
]
88+
89+
7990
logger.info(f" - Found {len(bot_comments)} old comment(s) from this agent to delete.")
80-
if bot_comments:
91+
logger.info(f" - Found {len(comments_to_delete)} of them are unanswered and will be deleted.")
92+
93+
94+
if comments_to_delete:
8195
delete_url_template = f"{base_url}/comments"
8296
deletions_successful = 0
83-
for comment in bot_comments:
84-
delete_url = f"{delete_url_template}/{comment['id']}"
85-
try:
86-
response = requests.delete(delete_url, auth=auth)
87-
response.raise_for_status()
88-
deletions_successful += 1
89-
except requests.exceptions.RequestException as e:
90-
logger.error(f" - FAILED to delete comment ID {comment['id']}. Status: {e.response.status_code if e.response else 'N/A'}. Response: {e.response.text if e.response else e}")
91-
92-
logger.info(f" - Successfully deleted {deletions_successful} out of {len(bot_comments)} comment(s).")
97+
for comment in comments_to_delete:
98+
delete_url = f"{delete_url_template}/{comment['id']}"
99+
try:
100+
response = requests.delete(delete_url, auth=auth)
101+
response.raise_for_status()
102+
deletions_successful += 1
103+
except requests.exceptions.RequestException as e:
104+
logger.error(f" - FAILED to delete comment ID {comment['id']}. Details: {e}")
105+
106+
logger.info(f" - Successfully deleted {deletions_successful} out of {len(comments_to_delete)} comment(s).")
107+
108+
109+
approve_url = f"{base_url}/approve"
110+
111+
if not all_issues:
112+
logger.info("✅ No issues found. Posting approval comment and approving PR.")
113+
114+
approve_comment_payload = {"content": {"raw": "Excellent work! The AI agent didn't find any issues. Keep up the great contributions! 🎉"}}
115+
requests.post(f"{base_url}/comments", headers=headers, auth=auth, json=approve_comment_payload)
116+
117+
response = requests.post(approve_url, auth=auth)
118+
response.raise_for_status()
119+
logger.info("✅ Successfully approved the Pull Request.")
120+
121+
else:
122+
logger.info(f" - Found {len(all_issues)} issue(s). Posting comments.")
123+
124+
requests.delete(approve_url, auth=auth)
125+
logger.info(" - Ensured PR is not approved by this agent (removed approval if existed).")
93126

94127
_publish_without_cleanup(all_issues, files_with_issues, base_url, auth, headers)
95128

0 commit comments

Comments
 (0)