Compare commits

...

2 Commits

Author SHA1 Message Date
Rohit Malhotra
164fab0a8d Add tests for Git LFS and clone depth support 2025-02-20 12:45:25 -05:00
Rohit Malhotra
bddf6674c3 Add Git LFS and clone depth support 2025-02-20 12:45:24 -05:00
2 changed files with 108 additions and 8 deletions

View File

@@ -429,14 +429,23 @@ async def resolve_issue(
# checkout the repo
repo_dir = os.path.join(output_dir, 'repo')
if not os.path.exists(repo_dir):
checkout_output = subprocess.check_output(
[
'git',
'clone',
issue_handler.get_clone_url(),
f'{output_dir}/repo',
]
).decode('utf-8')
# Configure Git LFS to skip downloading large files if requested
if os.getenv('GIT_LFS_SKIP_SMUDGE') == '1':
subprocess.check_output(['git', 'config', '--global', 'filter.lfs.smudge', 'git-lfs smudge --skip'])
subprocess.check_output(['git', 'config', '--global', 'filter.lfs.process', 'git-lfs filter-process --skip'])
# Build git clone command
clone_cmd = ['git', 'clone']
# Add --depth if requested
if depth := os.getenv('GIT_CLONE_DEPTH'):
clone_cmd.extend(['--depth', depth])
# Add repository URL and destination
clone_cmd.extend([issue_handler.get_clone_url(), f'{output_dir}/repo'])
# Execute git clone
checkout_output = subprocess.check_output(clone_cmd).decode('utf-8')
if 'fatal' in checkout_output:
raise RuntimeError(f'Failed to clone repository: {checkout_output}')

View File

@@ -0,0 +1,91 @@
import os
import subprocess
import tempfile
from unittest import mock
import pytest
from openhands.resolver.resolve_issue import resolve_issue
from openhands.resolver.utils import Platform
@pytest.mark.asyncio
async def test_git_lfs_skip_smudge():
# Create a temporary directory for the test
with tempfile.TemporaryDirectory() as temp_dir:
# Mock environment variables
with mock.patch.dict(os.environ, {'GIT_LFS_SKIP_SMUDGE': '1'}):
# Mock subprocess.check_output to verify git config is called
with mock.patch('subprocess.check_output') as mock_check_output:
# Mock issue handler
mock_handler = mock.MagicMock()
mock_handler.get_clone_url.return_value = 'https://github.com/test/repo.git'
mock_handler.get_converted_issues.return_value = [mock.MagicMock()]
# Mock issue_handler_factory to return our mock handler
with mock.patch('openhands.resolver.resolve_issue.issue_handler_factory', return_value=mock_handler):
# Call resolve_issue with test parameters
await resolve_issue(
owner='test',
repo='repo',
token='token',
username='username',
platform=Platform.GITHUB,
max_iterations=1,
output_dir=temp_dir,
llm_config=mock.MagicMock(),
runtime_container_image=None,
prompt_template='',
issue_type='issue',
repo_instruction=None,
issue_number=1,
comment_id=None,
)
# Verify git config was called with correct parameters
mock_check_output.assert_any_call(['git', 'config', '--global', 'filter.lfs.smudge', 'git-lfs smudge --skip'])
mock_check_output.assert_any_call(['git', 'config', '--global', 'filter.lfs.process', 'git-lfs filter-process --skip'])
@pytest.mark.asyncio
async def test_git_clone_depth():
# Create a temporary directory for the test
with tempfile.TemporaryDirectory() as temp_dir:
# Mock environment variables
with mock.patch.dict(os.environ, {'GIT_CLONE_DEPTH': '1'}):
# Mock subprocess.check_output to verify git clone is called with --depth
with mock.patch('subprocess.check_output') as mock_check_output:
# Mock issue handler
mock_handler = mock.MagicMock()
mock_handler.get_clone_url.return_value = 'https://github.com/test/repo.git'
mock_handler.get_converted_issues.return_value = [mock.MagicMock()]
# Mock issue_handler_factory to return our mock handler
with mock.patch('openhands.resolver.resolve_issue.issue_handler_factory', return_value=mock_handler):
# Call resolve_issue with test parameters
await resolve_issue(
owner='test',
repo='repo',
token='token',
username='username',
platform=Platform.GITHUB,
max_iterations=1,
output_dir=temp_dir,
llm_config=mock.MagicMock(),
runtime_container_image=None,
prompt_template='',
issue_type='issue',
repo_instruction=None,
issue_number=1,
comment_id=None,
)
# Verify git clone was called with --depth
mock_check_output.assert_any_call([
'git',
'clone',
'--depth',
'1',
'https://github.com/test/repo.git',
f'{temp_dir}/repo',
])