@@ -576,7 +576,7 @@ def test_non_pr_message_check_uses_commit_message_scope(self):
576576 def test_message_flag_removed_before_other_checks_in_pr (self ):
577577 captured_args = []
578578
579- def fake_other_checks (args ):
579+ def fake_other_checks (args , rev = None ):
580580 captured_args .extend (args )
581581 return []
582582
@@ -642,6 +642,107 @@ def test_push_without_pr_commits_does_not_warn(self):
642642 _rc , _results , output = self ._run_capturing_stdout ()
643643 self .assertNotIn ("::warning" , output )
644644
645+ @staticmethod
646+ def _fake_git_and_cli (head2_resolves : bool ):
647+ """subprocess.run stand-in: answers rev-parse and the CLI alike."""
648+ commands : list [list [str ]] = []
649+
650+ def run (command , ** _kwargs ):
651+ commands .append (command )
652+ if command [:2 ] == ["git" , "rev-parse" ]:
653+ return MagicMock (
654+ returncode = 0 if head2_resolves else 1 ,
655+ stdout = "abc123\n " if head2_resolves else "" ,
656+ )
657+ check = command [3 ].lstrip ("-" ).replace ("-" , "_" )
658+ return MagicMock (returncode = 0 , stdout = json_output (make_check (check )))
659+
660+ return run , commands
661+
662+ def test_pr_author_checks_read_the_branch_tip (self ):
663+ """On refs/pull/N/merge HEAD's author is GitHub, not the contributor."""
664+ run , commands = self ._fake_git_and_cli (head2_resolves = True )
665+ with (
666+ patch ("main.MESSAGE_ENABLED" , False ),
667+ patch ("main.BRANCH_ENABLED" , True ),
668+ patch ("main.AUTHOR_NAME_ENABLED" , True ),
669+ patch ("main.AUTHOR_EMAIL_ENABLED" , True ),
670+ patch ("main.is_pr_event" , return_value = True ),
671+ patch ("main.subprocess.run" , side_effect = run ),
672+ ):
673+ rc , results , output = self ._run_capturing_stdout ()
674+ self .assertEqual (rc , 0 )
675+ self .assertEqual (
676+ [s .label for s in results ], ["Branch" , "Author name" , "Author email" ]
677+ )
678+ self .assertIn (["git" , "rev-parse" , "--verify" , "--quiet" , "HEAD^2" ], commands )
679+ self .assertIn (
680+ ["commit-check" , "--format" , "json" , "--author-name" , "--rev" , "HEAD^2" ],
681+ commands ,
682+ )
683+ self .assertIn (
684+ ["commit-check" , "--format" , "json" , "--author-email" , "--rev" , "HEAD^2" ],
685+ commands ,
686+ )
687+ # The branch check has no commit to point at.
688+ self .assertIn (["commit-check" , "--format" , "json" , "--branch" ], commands )
689+ self .assertNotIn ("::warning" , output )
690+
691+ def test_pr_author_checks_fall_back_to_head_on_shallow_clone (self ):
692+ run , commands = self ._fake_git_and_cli (head2_resolves = False )
693+ with (
694+ patch ("main.MESSAGE_ENABLED" , False ),
695+ patch ("main.BRANCH_ENABLED" , False ),
696+ patch ("main.AUTHOR_NAME_ENABLED" , True ),
697+ patch ("main.AUTHOR_EMAIL_ENABLED" , False ),
698+ patch ("main.is_pr_event" , return_value = True ),
699+ patch ("main.subprocess.run" , side_effect = run ),
700+ ):
701+ rc , results , output = self ._run_capturing_stdout ()
702+ self .assertEqual (rc , 0 )
703+ self .assertIn (["commit-check" , "--format" , "json" , "--author-name" ], commands )
704+ self .assertFalse ([c for c in commands if "--rev" in c ], commands )
705+ warning = [ln for ln in output .splitlines () if ln .startswith ("::warning" )]
706+ self .assertEqual (len (warning ), 1 , output )
707+ self .assertTrue (warning [0 ].startswith ("::warning title=commit-check::" ))
708+ self .assertIn ("Could not resolve HEAD^2" , warning [0 ])
709+ self .assertIn ("is actions/checkout using fetch-depth: 0?" , warning [0 ])
710+
711+ def test_push_author_checks_never_pass_rev (self ):
712+ run , commands = self ._fake_git_and_cli (head2_resolves = True )
713+ with (
714+ patch ("main.MESSAGE_ENABLED" , False ),
715+ patch ("main.BRANCH_ENABLED" , False ),
716+ patch ("main.AUTHOR_NAME_ENABLED" , True ),
717+ patch ("main.AUTHOR_EMAIL_ENABLED" , True ),
718+ patch ("main.is_pr_event" , return_value = False ),
719+ patch ("main.subprocess.run" , side_effect = run ),
720+ ):
721+ _rc , _results , output = self ._run_capturing_stdout ()
722+ self .assertFalse ([c for c in commands if c [0 ] == "git" ], commands )
723+ self .assertFalse ([c for c in commands if "--rev" in c ], commands )
724+ self .assertNotIn ("::warning" , output )
725+
726+
727+ class TestPrHeadRev (unittest .TestCase ):
728+ def test_resolving_head2_returns_the_revision (self ):
729+ with patch (
730+ "main.subprocess.run" , return_value = MagicMock (returncode = 0 )
731+ ) as mock_run :
732+ self .assertEqual (main .pr_head_rev (), "HEAD^2" )
733+ self .assertEqual (
734+ mock_run .call_args [0 ][0 ],
735+ ["git" , "rev-parse" , "--verify" , "--quiet" , "HEAD^2" ],
736+ )
737+
738+ def test_shallow_clone_returns_none (self ):
739+ with patch ("main.subprocess.run" , return_value = MagicMock (returncode = 1 )):
740+ self .assertIsNone (main .pr_head_rev ())
741+
742+ def test_missing_git_returns_none (self ):
743+ with patch ("main.subprocess.run" , side_effect = OSError ("no git" )):
744+ self .assertIsNone (main .pr_head_rev ())
745+
645746
646747class TestCommitCheckVersionPin (unittest .TestCase ):
647748 """The warn rendering is inert against an engine that never emits it.
0 commit comments