Skip to content

Commit f5376d9

Browse files
committed
update exiting comments or delete it to add new one
1 parent b74de65 commit f5376d9

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

main.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def add_pr_comments() -> int:
106106
# Initialize GitHub client
107107
g = Github(token)
108108
repo = g.get_repo(repo_name)
109-
pr = repo.get_issue(int(pr_number))
109+
pull_request = repo.get_issue(int(pr_number))
110110

111111
# Prepare comment content
112112
result_text = read_result_file()
@@ -116,7 +116,38 @@ def add_pr_comments() -> int:
116116
else f"{FAILURE_TITLE}```\n{result_text}\n```"
117117
)
118118

119-
pr.create_comment(body=pr_comments)
119+
# Fetch all existing comments on the PR
120+
comments = pull_request.get_issue_comments()
121+
122+
# Track if we found a matching comment
123+
matching_comments = []
124+
last_comment = None
125+
126+
for comment in comments:
127+
if comment.body.startswith(SUCCESS_TITLE) or comment.body.startswith(
128+
FAILURE_TITLE
129+
):
130+
matching_comments.append(comment)
131+
if matching_comments:
132+
last_comment = matching_comments[-1]
133+
134+
if last_comment.body == pr_comments:
135+
print(f"PR comment already up-to-date for PR #{pr_number}.")
136+
return 0
137+
else:
138+
# If the last comment doesn't match, update it
139+
print(f"Updating the last comment on PR #{pr_number}.")
140+
last_comment.edit(pr_comments)
141+
142+
# Delete all older matching comments
143+
for comment in matching_comments[:-1]:
144+
print(f"Deleting an old comment on PR #{pr_number}.")
145+
comment.delete()
146+
else:
147+
# No matching comments, create a new one
148+
print(f"Creating a new comment on PR #{pr_number}.")
149+
pull_request.create_issue_comment(body=pr_comments)
150+
120151
return 0 if result_text is None else 1
121152
except Exception as e:
122153
print(f"Error posting PR comment: {e}", file=sys.stderr)

0 commit comments

Comments
 (0)