diff --git a/aider_gitea/__init__.py b/aider_gitea/__init__.py index 92d34a3..5a155b4 100644 --- a/aider_gitea/__init__.py +++ b/aider_gitea/__init__.py @@ -240,6 +240,51 @@ def push_changes( return True +def get_current_commit_hash(cwd: Path) -> str: + """Get the hash of the current commit. + + Args: + cwd: The current working directory (repository path). + + Returns: + The hash of the current commit as a string. + """ + try: + result = subprocess.run( + ['git', 'rev-parse', 'HEAD'], + check=True, + cwd=cwd, + capture_output=True, + text=True, + ) + return result.stdout.strip() + except subprocess.CalledProcessError: + logger.exception('Failed to get current commit hash') + return '' + +def has_changes_since_commit(cwd: Path, commit_hash: str) -> bool: + """Check if there are any changes since the specified commit. + + Args: + cwd: The current working directory (repository path). + commit_hash: The hash of the commit to compare against. + + Returns: + True if there are changes since the commit, False otherwise. + """ + try: + result = subprocess.run( + ['git', 'diff', '--name-only', f'{commit_hash}..HEAD'], + check=True, + cwd=cwd, + capture_output=True, + text=True, + ) + return bool(result.stdout.strip()) + except subprocess.CalledProcessError: + logger.exception('Failed to check for changes since commit %s', commit_hash) + return False + def has_commits_on_branch(cwd: Path, base_branch: str, current_branch: str) -> bool: """Check if there are any commits on the current branch that aren't in the base branch. @@ -299,6 +344,14 @@ def solve_issue_in_repository( run_cmd(['bash', '-c', AIDER_TEST], tmpdirname) run_cmd(['git', 'checkout', args.base_branch], tmpdirname) run_cmd(['git', 'checkout', '-b', branch_name], tmpdirname) + + # Run initial ruff pass and commit + run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], tmpdirname, check=False) + run_cmd(['git', 'add', '.'], tmpdirname) + run_cmd(['git', 'commit', '-m', 'Initial ruff pass'], tmpdirname, check=False) + + # Get the commit hash after ruff but before aider + pre_aider_commit = get_current_commit_hash(tmpdirname) # Run aider issue_content = f'# {issue_title}\n{issue_description}' @@ -315,6 +368,11 @@ def solve_issue_in_repository( run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], tmpdirname, check=False) run_cmd(['git', 'add', '.'], tmpdirname) run_cmd(['git', 'commit', '-m', 'Ruff'], tmpdirname, check=False) + + # Check if aider made any changes beyond the initial ruff pass + if not has_changes_since_commit(tmpdirname, pre_aider_commit): + logger.info('Aider did not make any changes beyond the initial ruff pass for issue #%s', issue_number) + return False # Push changes return push_changes( diff --git a/test/test_init.py b/test/test_init.py index bbd0912..3442e87 100644 --- a/test/test_init.py +++ b/test/test_init.py @@ -1,3 +1,78 @@ +import os +import tempfile +from pathlib import Path +from unittest.mock import patch, MagicMock + +import pytest + def test_init(): import aider_gitea # noqa: F401 import aider_gitea.secrets # noqa: F401 + +def test_get_current_commit_hash(): + from aider_gitea import get_current_commit_hash + + with tempfile.TemporaryDirectory() as tmpdirname: + with patch('subprocess.run') as mock_run: + mock_result = MagicMock() + mock_result.stdout = "abcdef1234567890" + mock_run.return_value = mock_result + + result = get_current_commit_hash(Path(tmpdirname)) + + assert result == "abcdef1234567890" + mock_run.assert_called_once() + +def test_has_changes_since_commit(): + from aider_gitea import has_changes_since_commit + + with tempfile.TemporaryDirectory() as tmpdirname: + # Test with changes + with patch('subprocess.run') as mock_run: + mock_result = MagicMock() + mock_result.stdout = "file1.py\nfile2.py" + mock_run.return_value = mock_result + + result = has_changes_since_commit(Path(tmpdirname), "abcdef") + + assert result is True + mock_run.assert_called_once() + + # Test without changes + with patch('subprocess.run') as mock_run: + mock_result = MagicMock() + mock_result.stdout = "" + mock_run.return_value = mock_result + + result = has_changes_since_commit(Path(tmpdirname), "abcdef") + + assert result is False + mock_run.assert_called_once() + +def test_solve_issue_with_no_aider_changes(): + from aider_gitea import solve_issue_in_repository + + with tempfile.TemporaryDirectory() as tmpdirname: + args = MagicMock() + args.gitea_url = "https://gitea.example.com" + args.owner = "test-owner" + args.repo = "test-repo" + args.base_branch = "main" + + with patch('aider_gitea.run_cmd', return_value=True) as mock_run_cmd, \ + patch('aider_gitea.get_current_commit_hash', return_value="abcdef") as mock_get_hash, \ + patch('aider_gitea.has_changes_since_commit', return_value=False) as mock_has_changes: + + result = solve_issue_in_repository( + args, + Path(tmpdirname), + "issue-123-test", + "Test Issue", + "Test Description", + "123", + None + ) + + assert result is False + mock_get_hash.assert_called_once() + mock_has_changes.assert_called_once_with(Path(tmpdirname), "abcdef")