Skip to content

Commit ba4b862

Browse files
committed
Make sure we are commenting on the correct PR the given commit is actually corresponding to.
1 parent acefc6a commit ba4b862

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

zorg/buildbot/reporters/utils.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,55 @@ def createStatus(
316316
)
317317
return None
318318

319+
# Users could reference wrong PRs in commit messages.
320+
# So, to make sure we are commenting the correct one we need to check
321+
# if the given commit is actually corresponding to the PR we parsed
322+
# from the commit message.
323+
324+
wrong_issue = True
325+
page = 1
326+
while wrong_issue:
327+
events_response = yield self._http.get(
328+
"/".join(["/repos", repo_user, repo_name, "issues", issue, "events"]) +
329+
f"?per_page=100&page={page}")
330+
if events_response.code not in (200,):
331+
log.msg(
332+
f"LLVMFailGitHubReporter.createStatus: WARNING: Cannot get events for PR#{issue}. Do not comment this PR."
333+
)
334+
return None
335+
336+
events = yield events_response.json()
337+
338+
# Empty events array signals that there is no more.
339+
if not events:
340+
log.msg(
341+
f"LLVMFailGitHubReporter.createStatus: WARNING: Got empty events list for PR#{issue}."
342+
)
343+
break
344+
345+
log.msg(
346+
"LLVMFailGitHubReporter.createStatus: WARNING: Got events list for PR#{issue} (page {page}): {events}."
347+
)
348+
349+
for event in events:
350+
if event["event"] == "merged":
351+
if event["commit_id"] == sha:
352+
wrong_issue = False
353+
break
354+
else:
355+
log.msg(
356+
f"LLVMFailGitHubReporter.createStatus: WARNING: Event 'merged' in PR#{issue} contains commit_id {event['commit_id']}, but revision is {sha}."
357+
)
358+
page += 1
359+
360+
if wrong_issue:
361+
log.msg(
362+
f"LLVMFailGitHubReporter.createStatus: WARNING: Given commit {sha} is not related to PR#{issue}. Do not comment this PR."
363+
)
364+
return None
365+
366+
# This is the right issue to comment.
367+
319368
url = "/".join(["/repos", repo_user, repo_name, "issues", issue, "comments"])
320369
log.msg(f"LLVMFailGitHubReporter.createStatus: INFO: http.post({url})")
321370

0 commit comments

Comments
 (0)