@@ -500,6 +500,111 @@ def test_send_pull_request_with_reviewer(
500
500
assert result == 'https://github.com/test-owner/test-repo/pull/1'
501
501
502
502
503
+ @patch ('subprocess.run' )
504
+ @patch ('requests.post' )
505
+ @patch ('requests.get' )
506
+ def test_send_pull_request_target_branch_with_fork (
507
+ mock_get , mock_post , mock_run , mock_github_issue , mock_output_dir
508
+ ):
509
+ """Test that target_branch works correctly when using a fork."""
510
+ repo_path = os .path .join (mock_output_dir , 'repo' )
511
+ fork_owner = 'fork-owner'
512
+ target_branch = 'custom-target'
513
+
514
+ # Mock API responses
515
+ mock_get .side_effect = [
516
+ MagicMock (status_code = 404 ), # Branch doesn't exist
517
+ MagicMock (status_code = 200 ), # Target branch exists
518
+ ]
519
+
520
+ mock_post .return_value .json .return_value = {
521
+ 'html_url' : 'https://github.com/test-owner/test-repo/pull/1'
522
+ }
523
+
524
+ # Mock subprocess.run calls
525
+ mock_run .side_effect = [
526
+ MagicMock (returncode = 0 ), # git checkout -b
527
+ MagicMock (returncode = 0 ), # git push
528
+ ]
529
+
530
+ # Call the function with fork_owner and target_branch
531
+ result = send_pull_request (
532
+ github_issue = mock_github_issue ,
533
+ github_token = 'test-token' ,
534
+ github_username = 'test-user' ,
535
+ patch_dir = repo_path ,
536
+ pr_type = 'ready' ,
537
+ fork_owner = fork_owner ,
538
+ target_branch = target_branch ,
539
+ )
540
+
541
+ # Assert API calls
542
+ assert mock_get .call_count == 2
543
+
544
+ # Verify target branch was checked in original repo, not fork
545
+ target_branch_check = mock_get .call_args_list [1 ]
546
+ assert target_branch_check [0 ][0 ] == f'https://api.github.com/repos/test-owner/test-repo/branches/{ target_branch } '
547
+
548
+ # Check PR creation
549
+ mock_post .assert_called_once ()
550
+ post_data = mock_post .call_args [1 ]['json' ]
551
+ assert post_data ['base' ] == target_branch # PR should target the specified branch
552
+ assert post_data ['head' ] == 'openhands-fix-issue-42' # Branch name should be standard
553
+
554
+ # Check that push was to fork
555
+ push_call = mock_run .call_args_list [1 ]
556
+ assert f'https://test-user:test-token@github.com/{ fork_owner } /test-repo.git' in str (push_call )
557
+
558
+
559
+ @patch ('subprocess.run' )
560
+ @patch ('requests.post' )
561
+ @patch ('requests.get' )
562
+ def test_send_pull_request_target_branch_with_additional_message (
563
+ mock_get , mock_post , mock_run , mock_github_issue , mock_output_dir
564
+ ):
565
+ """Test that target_branch works correctly with additional PR message."""
566
+ repo_path = os .path .join (mock_output_dir , 'repo' )
567
+ target_branch = 'feature-branch'
568
+ additional_message = 'Additional PR context'
569
+
570
+ # Mock API responses
571
+ mock_get .side_effect = [
572
+ MagicMock (status_code = 404 ), # Branch doesn't exist
573
+ MagicMock (status_code = 200 ), # Target branch exists
574
+ ]
575
+
576
+ mock_post .return_value .json .return_value = {
577
+ 'html_url' : 'https://github.com/test-owner/test-repo/pull/1'
578
+ }
579
+
580
+ # Mock subprocess.run calls
581
+ mock_run .side_effect = [
582
+ MagicMock (returncode = 0 ), # git checkout -b
583
+ MagicMock (returncode = 0 ), # git push
584
+ ]
585
+
586
+ # Call the function with target_branch and additional_message
587
+ result = send_pull_request (
588
+ github_issue = mock_github_issue ,
589
+ github_token = 'test-token' ,
590
+ github_username = 'test-user' ,
591
+ patch_dir = repo_path ,
592
+ pr_type = 'ready' ,
593
+ target_branch = target_branch ,
594
+ additional_message = additional_message ,
595
+ )
596
+
597
+ # Assert API calls
598
+ assert mock_get .call_count == 2
599
+
600
+ # Check PR creation
601
+ mock_post .assert_called_once ()
602
+ post_data = mock_post .call_args [1 ]['json' ]
603
+ assert post_data ['base' ] == target_branch
604
+ assert additional_message in post_data ['body' ]
605
+ assert 'This pull request fixes #42' in post_data ['body' ]
606
+
607
+
503
608
@patch ('requests.get' )
504
609
def test_send_pull_request_invalid_target_branch (
505
610
mock_get , mock_github_issue , mock_output_dir
0 commit comments