Compare commits

...

1 Commits

2 changed files with 65 additions and 1 deletions

View File

@@ -235,6 +235,7 @@ def send_pull_request(
target_branch: str | None = None,
reviewer: str | None = None,
pr_title: str | None = None,
repository: str | None = None,
) -> str:
"""Send a pull request to a GitHub or Gitlab repository.
@@ -250,6 +251,7 @@ def send_pull_request(
target_branch: The target branch to create the pull request against (defaults to repository default branch)
reviewer: The GitHub or Gitlab username of the reviewer to assign
pr_title: Custom title for the pull request (optional)
repository: Repository in format "owner/repo" for GitHub Actions token validation
"""
if pr_type not in ['branch', 'draft', 'ready']:
raise ValueError(f'Invalid pr_type: {pr_type}')
@@ -633,6 +635,12 @@ def main() -> None:
help='Custom title for the pull request',
default=None,
)
parser.add_argument(
'--repository',
type=str,
help='Repository in format "owner/repo" for GitHub Actions token validation',
default=os.getenv('GITHUB_REPOSITORY'),
)
my_args = parser.parse_args()
token = my_args.token or os.getenv('GITHUB_TOKEN') or os.getenv('GITLAB_TOKEN')
@@ -642,7 +650,7 @@ def main() -> None:
)
username = my_args.username if my_args.username else os.getenv('GIT_USERNAME')
platform = identify_token(token)
platform = identify_token(token, my_args.repository)
if platform == Platform.INVALID:
raise ValueError('Token is invalid.')

View File

@@ -504,6 +504,62 @@ def test_send_pull_request_with_reviewer(
assert result == 'https://github.com/test-owner/test-repo/pull/1'
@patch('subprocess.run')
@patch('httpx.post')
@patch('httpx.get')
def test_send_pull_request_with_github_actions_token(
mock_get, mock_post, mock_run, mock_issue, mock_output_dir
):
"""Test that GitHub Actions token works correctly."""
repo_path = os.path.join(mock_output_dir, 'repo')
token = 'github-actions-token'
repository = 'test-owner/test-repo'
# Mock API responses
mock_get.side_effect = [
MagicMock(status_code=404), # Branch doesn't exist
MagicMock(status_code=200), # Repository exists (GitHub Actions token check)
MagicMock(json=lambda: {'default_branch': 'main'}), # Get default branch
]
mock_post.return_value.json.return_value = {
'html_url': 'https://github.com/test-owner/test-repo/pull/1'
}
# Mock subprocess.run calls
mock_run.side_effect = [
MagicMock(returncode=0), # git checkout -b
MagicMock(returncode=0), # git push
]
# Call the function with GitHub Actions token
result = send_pull_request(
issue=mock_issue,
token=token,
username='test-user',
platform=Platform.GITHUB,
patch_dir=repo_path,
pr_type='ready',
repository=repository,
)
# Assert API calls
assert mock_get.call_count == 3
assert mock_post.call_count == 1
# Check GitHub Actions token validation call
token_validation_call = mock_get.call_args_list[1]
assert token_validation_call[0][0] == 'https://api.github.com/repos/test-owner/test-repo'
assert token_validation_call[1]['headers']['Authorization'] == f'Bearer {token}'
# Check PR creation
pr_create_call = mock_post.call_args[1]
assert pr_create_call['json']['title'] == 'Fix issue #42: Test Issue'
# Check the result URL
assert result == 'https://github.com/test-owner/test-repo/pull/1'
@patch('subprocess.run')
@patch('httpx.post')
@patch('httpx.get')