@@ -691,6 +691,182 @@ def test_fork_pr_writes_job_summary_hint(self):
691691 self .assertIn ("read-only" , content )
692692 self .assertIn ("fork-pr-comments" , content )
693693
694+ # --- PyGithub interaction tests ---
695+
696+ def _mock_github_chain (self ):
697+ """Set up mocked PyGithub objects for comment interaction tests."""
698+ mock_github = MagicMock ()
699+ mock_repo = MagicMock ()
700+ mock_issue = MagicMock ()
701+ mock_github .get_repo .return_value = mock_repo
702+ mock_repo .get_issue .return_value = mock_issue
703+ return mock_github , mock_repo , mock_issue
704+
705+ def _patch_github_env (self , token = "fake-token" , repo = "test/repo" ):
706+ return patch .dict (
707+ os .environ ,
708+ {"GITHUB_TOKEN" : token , "GITHUB_REPOSITORY" : repo },
709+ )
710+
711+ def test_new_comment_created_when_failure (self ):
712+ """No existing comment: creates a new comment, returns 1 when result has content."""
713+ mock_github , _ , mock_issue = self ._mock_github_chain ()
714+ mock_issue .get_comments .return_value = []
715+ with (
716+ patch ("main.PR_COMMENTS_ENABLED" , True ),
717+ patch ("main.is_fork_pr" , return_value = False ),
718+ patch ("github.Github" , return_value = mock_github ),
719+ patch ("main.get_pr_number" , return_value = 42 ),
720+ patch ("main.read_result_file" , return_value = "bad commit message" ),
721+ self ._patch_github_env (),
722+ patch ("builtins.print" ),
723+ ):
724+ rc = main .add_pr_comments ()
725+ self .assertEqual (rc , 1 )
726+ mock_issue .create_comment .assert_called_once ()
727+ body = mock_issue .create_comment .call_args [1 ]["body" ]
728+ self .assertIn (main .FAILURE_TITLE , body )
729+ self .assertIn ("bad commit message" , body )
730+
731+ def test_new_comment_created_when_success (self ):
732+ """No existing comment: creates a new success comment, returns 0."""
733+ mock_github , _ , mock_issue = self ._mock_github_chain ()
734+ mock_issue .get_comments .return_value = []
735+ with (
736+ patch ("main.PR_COMMENTS_ENABLED" , True ),
737+ patch ("main.is_fork_pr" , return_value = False ),
738+ patch ("github.Github" , return_value = mock_github ),
739+ patch ("main.get_pr_number" , return_value = 42 ),
740+ patch ("main.read_result_file" , return_value = None ),
741+ self ._patch_github_env (),
742+ patch ("builtins.print" ),
743+ ):
744+ rc = main .add_pr_comments ()
745+ self .assertEqual (rc , 0 )
746+ mock_issue .create_comment .assert_called_once ()
747+ body = mock_issue .create_comment .call_args [1 ]["body" ]
748+ self .assertEqual (body , main .SUCCESS_TITLE )
749+
750+ def test_comment_up_to_date_skips (self ):
751+ """Existing comment with same body: no edit/create, returns 0."""
752+ mock_github , _ , mock_issue = self ._mock_github_chain ()
753+ existing = MagicMock ()
754+ existing .body = main .SUCCESS_TITLE
755+ mock_issue .get_comments .return_value = [existing ]
756+ with (
757+ patch ("main.PR_COMMENTS_ENABLED" , True ),
758+ patch ("main.is_fork_pr" , return_value = False ),
759+ patch ("github.Github" , return_value = mock_github ),
760+ patch ("main.get_pr_number" , return_value = 42 ),
761+ patch ("main.read_result_file" , return_value = None ),
762+ self ._patch_github_env (),
763+ patch ("builtins.print" ),
764+ ):
765+ rc = main .add_pr_comments ()
766+ self .assertEqual (rc , 0 )
767+ existing .edit .assert_not_called ()
768+ existing .delete .assert_not_called ()
769+ mock_issue .create_comment .assert_not_called ()
770+
771+ def test_comment_updated_old_deleted (self ):
772+ """Multiple existing comments with stale body: update last, delete rest."""
773+ mock_github , _ , mock_issue = self ._mock_github_chain ()
774+ old_comment = MagicMock ()
775+ old_comment .body = main .FAILURE_TITLE + "\n ```\n old message\n ```"
776+ last_comment = MagicMock ()
777+ last_comment .body = main .FAILURE_TITLE + "\n ```\n older message\n ```"
778+ mock_issue .get_comments .return_value = [old_comment , last_comment ]
779+ with (
780+ patch ("main.PR_COMMENTS_ENABLED" , True ),
781+ patch ("main.is_fork_pr" , return_value = False ),
782+ patch ("github.Github" , return_value = mock_github ),
783+ patch ("main.get_pr_number" , return_value = 42 ),
784+ patch ("main.read_result_file" , return_value = "new error" ),
785+ self ._patch_github_env (),
786+ patch ("builtins.print" ),
787+ ):
788+ rc = main .add_pr_comments ()
789+ self .assertEqual (rc , 1 )
790+ last_comment .edit .assert_called_once ()
791+ old_comment .delete .assert_called_once ()
792+ mock_issue .create_comment .assert_not_called ()
793+ edited_body = last_comment .edit .call_args [0 ][0 ]
794+ self .assertIn ("new error" , edited_body )
795+
796+ def test_missing_token_returns_zero (self ):
797+ """GITHUB_TOKEN not set: catches ValueError, returns 0."""
798+ with (
799+ patch ("main.PR_COMMENTS_ENABLED" , True ),
800+ patch ("main.is_fork_pr" , return_value = False ),
801+ patch ("main.get_pr_number" , return_value = 42 ),
802+ patch .dict (os .environ , {"GITHUB_REPOSITORY" : "test/repo" }),
803+ patch ("os.getenv" , return_value = None ),
804+ patch ("builtins.print" ),
805+ ):
806+ rc = main .add_pr_comments ()
807+ self .assertEqual (rc , 0 )
808+
809+ def test_403_forbidden_returns_warning (self ):
810+ """GithubException(403): prints warning, returns 0."""
811+ from github import GithubException
812+
813+ mock_github , mock_repo , _ = self ._mock_github_chain ()
814+ mock_repo .get_issue .side_effect = GithubException (
815+ 403 , {"message" : "Resource not accessible by integration" }
816+ )
817+ with (
818+ patch ("main.PR_COMMENTS_ENABLED" , True ),
819+ patch ("main.is_fork_pr" , return_value = False ),
820+ patch ("github.Github" , return_value = mock_github ),
821+ patch ("main.get_pr_number" , return_value = 42 ),
822+ self ._patch_github_env (),
823+ patch ("builtins.print" ) as mock_print ,
824+ ):
825+ rc = main .add_pr_comments ()
826+ self .assertEqual (rc , 0 )
827+ warning_text = mock_print .call_args_list [- 1 ][0 ][0 ]
828+ self .assertIn ("warning" , warning_text )
829+ self .assertIn ("403" , warning_text )
830+
831+ def test_github_exception_returns_zero (self ):
832+ """GithubException(non-403): prints error, returns 0."""
833+ from github import GithubException
834+
835+ mock_github , mock_repo , _ = self ._mock_github_chain ()
836+ mock_repo .get_issue .side_effect = GithubException (
837+ 500 , {"message" : "Internal Server Error" }
838+ )
839+ with (
840+ patch ("main.PR_COMMENTS_ENABLED" , True ),
841+ patch ("main.is_fork_pr" , return_value = False ),
842+ patch ("github.Github" , return_value = mock_github ),
843+ patch ("main.get_pr_number" , return_value = 42 ),
844+ self ._patch_github_env (),
845+ patch ("builtins.print" ) as mock_print ,
846+ ):
847+ rc = main .add_pr_comments ()
848+ self .assertEqual (rc , 0 )
849+ error_text = mock_print .call_args_list [- 1 ][0 ][0 ]
850+ self .assertIn ("Error posting PR comment" , error_text )
851+ self .assertIn ("500" , error_text )
852+
853+ def test_generic_exception_returns_zero (self ):
854+ """Generic Exception: prints error, returns 0."""
855+ mock_github , mock_repo , _ = self ._mock_github_chain ()
856+ mock_repo .get_issue .side_effect = ValueError ("unexpected error" )
857+ with (
858+ patch ("main.PR_COMMENTS_ENABLED" , True ),
859+ patch ("main.is_fork_pr" , return_value = False ),
860+ patch ("github.Github" , return_value = mock_github ),
861+ patch ("main.get_pr_number" , return_value = 42 ),
862+ self ._patch_github_env (),
863+ patch ("builtins.print" ) as mock_print ,
864+ ):
865+ rc = main .add_pr_comments ()
866+ self .assertEqual (rc , 0 )
867+ error_text = mock_print .call_args_list [- 1 ][0 ][0 ]
868+ self .assertIn ("Error posting PR comment" , error_text )
869+
694870
695871class TestIsForkPrWithReadonlyToken (unittest .TestCase ):
696872 def test_fork_pr_with_pull_request_event (self ):
0 commit comments