@@ -226,8 +226,10 @@ def execute_revert(
226226 )
227227 return False
228228
229- if not dry_run :
230- self ._comment_pr_notify_revert (commit_sha , sources , ctx )
229+ if ctx .revert_action == RevertAction .RUN_REVERT :
230+ self ._comment_pr_do_revert (commit_sha , sources , ctx )
231+ elif ctx .revert_action == RevertAction .RUN_NOTIFY :
232+ self ._comment_issue_notify (commit_sha , sources , ctx )
231233
232234 self ._logger .insert_event (
233235 repo = ctx .repo_full_name ,
@@ -323,6 +325,32 @@ def execute_restart(
323325 )
324326 return True
325327
328+ def _commit_message_check_pr_is_revert (
329+ self , commit_message : str , ctx : RunContext
330+ ) -> Optional [int ]:
331+ # Look for "Reverted #XXXXX" - indicates a revert action
332+ revert_matches = re .findall (
333+ f"Reverted https://github.com/{ ctx .repo_full_name } /pull/(\\ d+)" ,
334+ commit_message ,
335+ )
336+ if revert_matches :
337+ pr_number = int (revert_matches [- 1 ])
338+ return pr_number
339+ return None
340+
341+ def _commit_message_check_pr_is_merge (
342+ self , commit_message : str , ctx : RunContext
343+ ) -> Optional [int ]:
344+ # Look for "Pull Request resolved: #XXXXX" - indicates a merge action
345+ merge_matches = re .findall (
346+ f"Pull Request resolved: https://github.com/{ ctx .repo_full_name } /pull/(\\ d+)" ,
347+ commit_message ,
348+ )
349+ if merge_matches :
350+ pr_number = int (merge_matches [- 1 ])
351+ return pr_number
352+ return None
353+
326354 def _find_pr_by_sha (
327355 self , commit_sha : str , ctx : RunContext
328356 ) -> Optional [Tuple [CommitPRSourceAction , github .PullRequest .PullRequest ]]:
@@ -348,13 +376,8 @@ def _find_pr_by_sha(
348376 # This is the most reliable way to determine the pytorchbot action
349377 # Use findall to get all matches and pick the last one (pytorchbot appends at the end)
350378
351- # Look for "Reverted #XXXXX" - indicates a revert action
352- revert_matches = re .findall (
353- r"Reverted https://github.com/pytorch/pytorch/pull/(\d+)" ,
354- commit_message ,
355- )
356- if revert_matches :
357- pr_number = int (revert_matches [- 1 ]) # Use the last match
379+ pr_number = self ._commit_message_check_pr_is_revert (commit_message , ctx )
380+ if pr_number is not None :
358381 try :
359382 pr = repo .get_pull (pr_number )
360383 logging .info (
@@ -370,13 +393,8 @@ def _find_pr_by_sha(
370393 str (e ),
371394 )
372395
373- # Look for "Pull Request resolved: #XXXXX" - indicates a merge action
374- pr_resolved_matches = re .findall (
375- r"Pull Request resolved: https://github.com/pytorch/pytorch/pull/(\d+)" ,
376- commit_message ,
377- )
378- if pr_resolved_matches :
379- pr_number = int (pr_resolved_matches [- 1 ]) # Use the last match
396+ pr_number = self ._commit_message_check_pr_is_merge (commit_message , ctx )
397+ if pr_number is not None :
380398 try :
381399 pr = repo .get_pull (pr_number )
382400 logging .info (
@@ -434,13 +452,65 @@ def _find_pr_by_sha(
434452 )
435453 return None
436454
437- def _comment_pr_notify_revert (
455+ def _comment_issue_notify (
456+ self , commit_sha : str , sources : List [SignalMetadata ], ctx : RunContext
457+ ) -> bool :
458+ """Comment on the issue to notify interested stakeholders about the detected autorevert"""
459+
460+ if ctx .revert_action != RevertAction .RUN_NOTIFY :
461+ return False
462+
463+ logging .debug (
464+ "[v2][action] notify for sha %s: finding the issue and notifying stakeholders on issue %s" ,
465+ commit_sha [:8 ],
466+ ctx .notify_issue_number ,
467+ )
468+
469+ # find the PR from commit_sha on main
470+ pr_result = self ._find_pr_by_sha (commit_sha , ctx )
471+ if not pr_result :
472+ logging .error (
473+ "[v2][action] revert for sha %s: no PR found!" , commit_sha [:8 ]
474+ )
475+ return False
476+
477+ try :
478+ issue = (
479+ GHClientFactory ()
480+ .client .get_repo (ctx .repo_full_name )
481+ .get_issue (number = ctx .notify_issue_number )
482+ )
483+ action_type , pr = pr_result
484+ issue .create_comment (
485+ f"Autorevert detected a possible offender: { commit_sha [:8 ]} from PR #{ pr .number } .\n "
486+ + (
487+ "The commit is a revert"
488+ if action_type == CommitPRSourceAction .REVERT
489+ else "The commit is a PR merge"
490+ )
491+ + "\n "
492+ + "This commit is breaking the following workflows:\n "
493+ + "- {}" .format ("\n - " .join (source .workflow_name for source in sources ))
494+ + "\n "
495+ )
496+ except Exception :
497+ logging .exception (
498+ "[v2][action] revert for sha %s: failed to comment on issue #%d" ,
499+ commit_sha [:8 ],
500+ ctx .notify_issue_number ,
501+ )
502+ return False
503+
504+ def _comment_pr_do_revert (
438505 self , commit_sha : str , sources : List [SignalMetadata ], ctx : RunContext
439506 ) -> bool :
440- """Comment on the pull request to notify the author about that their PR is breaking signals."""
507+ """Comment on the pull request to pytorchbot to revert it."""
508+
509+ if ctx .revert_action != RevertAction .RUN_REVERT :
510+ return False
441511
442512 logging .debug (
443- "[v2][action] revert for sha %s: finding the PR andnotifying author" ,
513+ "[v2][action] revert for sha %s: finding the PR and notifying author" ,
444514 commit_sha [:8 ],
445515 )
446516
@@ -461,35 +531,33 @@ def _comment_pr_notify_revert(
461531 )
462532 return False
463533
464- # Comment on the PR to notify the author about the revert
465- comment_body = (
466- "This PR is breaking the following workflows:\n "
467- + "- {}" .format ("\n - " .join (source .workflow_name for source in sources ))
468- + "\n \n Please investigate and fix the issues."
469- )
470-
471- pr .create_issue_comment (comment_body )
472- logging .warning (
473- "[v2][action] revert for sha %s: notified author in PR #%d" ,
474- commit_sha [:8 ],
475- pr .number ,
476- )
534+ # TODO Add autorevert cause for pytorchbot OR decide if we need to use
535+ # other causes like weird
477536
478- if ctx .revert_action == RevertAction .RUN_REVERT :
479- # TODO Add autorevert cause for pytorchbot OR decide if we need to use
480- # other causes like weird
481-
482- # TODO check if the tag `autorevert:disable` is present and don't do the revert
483- # comment, instead limiting to poke the author
484- comment_body = (
485- "XXXX revert -m \" Reverted automatically by pytorch's autorevert, "
486- + 'to avoid this behaviour add the tag autorevert:disable" -c autorevert'
537+ # TODO check if the tag `autorevert:disable` is present and don't do the revert
538+ # comment, instead limiting to poke the author
539+ try :
540+ pr .create_issue_comment (
541+ "@pytorchbot revert -m \" Reverted automatically by pytorch's autorevert, "
542+ + 'to avoid this behaviour add the tag autorevert:disable" -c autorevert\n '
543+ + "\n "
544+ + "This PR is breaking the following workflows:\n "
545+ + "- {}" .format ("\n - " .join (source .workflow_name for source in sources ))
546+ + "\n \n Please investigate and fix the issues."
487547 )
488- pr . create_issue_comment ( comment_body )
489- logging .warning (
490- "[v2][action] revert for sha %s: requested pytorchbot revert in PR #%d" ,
548+ except Exception as e :
549+ logging .error ( # noqa: G200
550+ "[v2][action] revert for sha %s: failed to comment on PR #%d: %s " ,
491551 commit_sha [:8 ],
492552 pr .number ,
553+ str (e ),
493554 )
555+ return False
556+
557+ logging .warning (
558+ "[v2][action] revert for sha %s: requested pytorchbot revert in PR #%d" ,
559+ commit_sha [:8 ],
560+ pr .number ,
561+ )
494562
495563 return True
0 commit comments