@@ -56,6 +56,20 @@ def comment_pr(repo, run_id):
56
56
if os .path .isdir ("pr" ):
57
57
shutil .rmtree ("pr" )
58
58
59
+ try :
60
+ utils .download_artifact (repo , "comment" , "comment" , run_id )
61
+ with open ("comment/ID" ) as file :
62
+ raw_comment_id = int (file .read ().strip ())
63
+ except Exception as e :
64
+ # If there is no existing comment, the `comment/ID` artifact
65
+ # will not exist. This will cause `utils.download_artifact`
66
+ # to fail, so we catch that and set `raw_comment_id` to `None`.
67
+ print ("Could not retrieve an existing comment ID. \n " , e )
68
+ raw_comment_id = None
69
+ finally :
70
+ if os .path .isdir ("comment" ):
71
+ shutil .rmtree ("comment" )
72
+
59
73
# Try storing diff for previous run:
60
74
prev_run_id = 0
61
75
prev_diff_exists = False
@@ -95,14 +109,37 @@ def comment_pr(repo, run_id):
95
109
96
110
comment = comment_first_line + \
97
111
"A recent commit removed the previously reported differences."
98
- post_comment (comment , repo , pr_number )
112
+
113
+ if raw_comment_id is None :
114
+ post_initial_comment (comment , repo , pr_number )
115
+ else :
116
+ update_existing_comment (comment , repo , pr_number , raw_comment_id )
99
117
100
118
101
- def post_comment (comment , repo , pr_number ):
119
+ def post_initial_comment (comment , repo , pr_number ):
102
120
print (f"Posting comment to PR #{ pr_number } " )
103
121
utils .subprocess_run (["gh" , "pr" , "comment" , str (pr_number ),
104
122
"--repo" , repo , "--body" , comment ])
105
123
124
+ def update_existing_comment (comment , repo , pr_number , raw_comment_id ):
125
+ # Fetch existing comment, and validate:
126
+ # - comment belongs to the PR with number `pr_number`
127
+ # - comment starts with the expected prefix `comment_first_line`
128
+ # - comment author is github-actions[bot]
129
+ comment_author = "github-actions[bot]"
130
+ filter = f"select(.issue_url | endswith(\" { repo } /issues/{ pr_number } \" )) " + \
131
+ f"| select(.body | startswith(\" { comment_first_line } \" )) " + \
132
+ f"| select(.user.login == \" { comment_author } \" ) " + \
133
+ "| .id"
134
+ comment_id = utils .subprocess_check_output (["gh" , "api" , f"repos/{ repo } /issues/comments/{ raw_comment_id } " ,
135
+ "--jq" , filter ]).strip ()
136
+
137
+ if comment_id :
138
+ print (f"Updating comment { comment_id } on PR #{ pr_number } " )
139
+ utils .subprocess_run (["gh" , "api" , f"repos/{ repo } /issues/comments/{ comment_id } " ,
140
+ "-X" , "PATCH" , "-f" , f"body={ comment } " ])
141
+ else :
142
+ print (f"Comment { raw_comment_id } did not pass validations: not editing." )
106
143
107
144
def get_previous_run_id (repo , run_id , pr_number ):
108
145
"""
@@ -118,7 +155,7 @@ def get_previous_run_id(repo, run_id, pr_number):
118
155
pr_repo = this_run ["head_repository" ]
119
156
120
157
# Get all previous runs that match branch, repo and workflow name:
121
- output = utils .subprocess_check_output (["gh" , "api" , "-X" , "GET" , f"repos/{ repo } /actions/runs" , "-f" , "event=pull_request" , "-f" , "status=success" , "-f" , f"branch=' { pr_branch } ' " , "--paginate" ,
158
+ output = utils .subprocess_check_output (["gh" , "api" , "-X" , "GET" , f"repos/{ repo } /actions/runs" , "-f" , "event=pull_request" , "-f" , "status=success" , "-f" , f"branch={ pr_branch } " , "--paginate" ,
122
159
"--jq" , f'[.workflow_runs.[] | select(.head_repository.full_name=="{ pr_repo } " and .name=="{ artifacts_workflow_name } ")] | sort_by(.id) | reverse | [.[].id]' ])
123
160
124
161
ids = []
0 commit comments