Compare commits
5 Commits
ab4aef11e6
...
8d67641381
Author | SHA1 | Date | |
---|---|---|---|
8d67641381 | |||
6c4e17999b | |||
8f8b91bf5e | |||
5867cf299f | |||
d25dae34f0 |
|
@ -174,7 +174,7 @@ def create_aider_command(issue: str) -> list[str]:
|
||||||
return l
|
return l
|
||||||
|
|
||||||
|
|
||||||
def get_commit_messages(cwd: Path, base_branch: str, current_branch: str) -> str:
|
def get_commit_messages(cwd: Path, base_branch: str, current_branch: str) -> list[str]:
|
||||||
"""Get commit messages between base branch and current branch.
|
"""Get commit messages between base branch and current branch.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -193,7 +193,7 @@ def get_commit_messages(cwd: Path, base_branch: str, current_branch: str) -> str
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
)
|
)
|
||||||
return result.stdout.strip()
|
return reversed(result.stdout.strip().split('\n'))
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
logger.exception(f'Failed to get commit messages on branch {current_branch}')
|
logger.exception(f'Failed to get commit messages on branch {current_branch}')
|
||||||
return ''
|
return ''
|
||||||
|
@ -220,11 +220,11 @@ def push_changes(
|
||||||
|
|
||||||
if commit_messages:
|
if commit_messages:
|
||||||
description += '## Commit Messages\n\n'
|
description += '## Commit Messages\n\n'
|
||||||
for message in commit_messages.split('\n'):
|
for message in commit_messages:
|
||||||
description += f'- {message}\n'
|
description += f'- {message}\n'
|
||||||
|
|
||||||
# First push the branch without creating a PR
|
# First push the branch without creating a PR
|
||||||
cmd = ['git', 'push', 'origin', branch_name]
|
cmd = ['git', 'push', 'origin', branch_name, '--force']
|
||||||
run_cmd(cmd, cwd)
|
run_cmd(cmd, cwd)
|
||||||
|
|
||||||
# Then create the PR with the aider label
|
# Then create the PR with the aider label
|
||||||
|
@ -300,6 +300,21 @@ def solve_issue_in_repository(
|
||||||
run_cmd(['git', 'checkout', args.base_branch], tmpdirname)
|
run_cmd(['git', 'checkout', args.base_branch], tmpdirname)
|
||||||
run_cmd(['git', 'checkout', '-b', branch_name], tmpdirname)
|
run_cmd(['git', 'checkout', '-b', branch_name], tmpdirname)
|
||||||
|
|
||||||
|
# Run initial ruff pass before aider
|
||||||
|
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)
|
||||||
|
|
||||||
|
# Save the commit hash after ruff but before aider
|
||||||
|
result = subprocess.run(
|
||||||
|
['git', 'rev-parse', 'HEAD'],
|
||||||
|
check=True,
|
||||||
|
cwd=tmpdirname,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
pre_aider_commit = result.stdout.strip()
|
||||||
|
|
||||||
# Run aider
|
# Run aider
|
||||||
issue_content = f'# {issue_title}\n{issue_description}'
|
issue_content = f'# {issue_title}\n{issue_description}'
|
||||||
succeeded = run_cmd(
|
succeeded = run_cmd(
|
||||||
|
@ -311,10 +326,27 @@ def solve_issue_in_repository(
|
||||||
logger.error('Aider invocation failed for issue #%s', issue_number)
|
logger.error('Aider invocation failed for issue #%s', issue_number)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Auto-fix standard code quality stuff
|
# Auto-fix standard code quality stuff after aider
|
||||||
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], tmpdirname, check=False)
|
run_cmd(['bash', '-c', RUFF_FORMAT_AND_AUTO_FIX], tmpdirname, check=False)
|
||||||
run_cmd(['git', 'add', '.'], tmpdirname)
|
run_cmd(['git', 'add', '.'], tmpdirname)
|
||||||
run_cmd(['git', 'commit', '-m', 'Ruff'], tmpdirname, check=False)
|
run_cmd(['git', 'commit', '-m', 'Ruff after aider'], tmpdirname, check=False)
|
||||||
|
|
||||||
|
# Check if aider made any changes beyond the initial ruff pass
|
||||||
|
result = subprocess.run(
|
||||||
|
['git', 'diff', pre_aider_commit, 'HEAD', '--name-only'],
|
||||||
|
check=True,
|
||||||
|
cwd=tmpdirname,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
files_changed = result.stdout.strip()
|
||||||
|
|
||||||
|
if not files_changed:
|
||||||
|
logger.info(
|
||||||
|
'Aider did not make any changes beyond the initial ruff pass for issue #%s',
|
||||||
|
issue_number,
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
# Push changes
|
# Push changes
|
||||||
return push_changes(
|
return push_changes(
|
||||||
|
|
|
@ -158,38 +158,14 @@ class GiteaClient:
|
||||||
requests.HTTPError: If the API request fails.
|
requests.HTTPError: If the API request fails.
|
||||||
"""
|
"""
|
||||||
url = f'{self.gitea_url}/repos/{owner}/{repo}/pulls'
|
url = f'{self.gitea_url}/repos/{owner}/{repo}/pulls'
|
||||||
json_data = {'title': title, 'body': body, 'head': head, 'base': base}
|
json_data = {
|
||||||
|
'title': title,
|
||||||
|
'body': body,
|
||||||
|
'head': head,
|
||||||
|
'base': base,
|
||||||
|
'labels': labels,
|
||||||
|
}
|
||||||
|
|
||||||
response = self.session.post(url, json=json_data)
|
response = self.session.post(url, json=json_data)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
pull_request = response.json()
|
return response.json()
|
||||||
|
|
||||||
# Apply labels if provided
|
|
||||||
if labels and pull_request.get('number'):
|
|
||||||
self.add_labels_to_pull_request(owner, repo, pull_request['number'], labels)
|
|
||||||
|
|
||||||
return pull_request
|
|
||||||
|
|
||||||
def add_labels_to_pull_request(
|
|
||||||
self, owner: str, repo: str, pull_number: int, labels: list[str],
|
|
||||||
) -> bool:
|
|
||||||
"""Add labels to an existing pull request.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
owner (str): Owner of the repository.
|
|
||||||
repo (str): Name of the repository.
|
|
||||||
pull_number (int): The pull request number.
|
|
||||||
labels (list[str]): List of label names to apply.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
bool: True if labels were successfully applied.
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
requests.HTTPError: If the API request fails.
|
|
||||||
"""
|
|
||||||
url = f'{self.gitea_url}/repos/{owner}/{repo}/issues/{pull_number}/labels'
|
|
||||||
json_data = {'labels': labels}
|
|
||||||
|
|
||||||
response = self.session.post(url, json=json_data)
|
|
||||||
response.raise_for_status()
|
|
||||||
return True
|
|
||||||
|
|
|
@ -12,11 +12,12 @@ class TestGiteaClientPRLabels:
|
||||||
# Mock the PR creation response
|
# Mock the PR creation response
|
||||||
pr_response = MagicMock()
|
pr_response = MagicMock()
|
||||||
pr_response.status_code = 201
|
pr_response.status_code = 201
|
||||||
pr_response.json.return_value = {
|
expected_result = {
|
||||||
'number': 123,
|
'number': 123,
|
||||||
'title': 'Test PR',
|
'title': 'Test PR',
|
||||||
'html_url': 'https://gitea.example.com/owner/repo/pulls/123',
|
'html_url': 'https://gitea.example.com/owner/repo/pulls/123',
|
||||||
}
|
}
|
||||||
|
pr_response.json.return_value = expected_result
|
||||||
|
|
||||||
# Mock the label addition response
|
# Mock the label addition response
|
||||||
label_response = MagicMock()
|
label_response = MagicMock()
|
||||||
|
@ -37,7 +38,7 @@ class TestGiteaClientPRLabels:
|
||||||
)
|
)
|
||||||
|
|
||||||
# Verify PR creation call
|
# Verify PR creation call
|
||||||
assert mock_post.call_count == 2
|
assert mock_post.call_count == 1
|
||||||
pr_call_args = mock_post.call_args_list[0]
|
pr_call_args = mock_post.call_args_list[0]
|
||||||
assert (
|
assert (
|
||||||
pr_call_args[0][0]
|
pr_call_args[0][0]
|
||||||
|
@ -45,38 +46,5 @@ class TestGiteaClientPRLabels:
|
||||||
)
|
)
|
||||||
assert pr_call_args[1]['json']['title'] == 'Test PR'
|
assert pr_call_args[1]['json']['title'] == 'Test PR'
|
||||||
|
|
||||||
# Verify label addition call
|
|
||||||
label_call_args = mock_post.call_args_list[1]
|
|
||||||
assert (
|
|
||||||
label_call_args[0][0]
|
|
||||||
== 'https://gitea.example.com/api/v1/repos/owner/repo/issues/123/labels'
|
|
||||||
)
|
|
||||||
assert label_call_args[1]['json']['labels'] == ['aider']
|
|
||||||
|
|
||||||
# Verify the result
|
# Verify the result
|
||||||
assert result['number'] == 123
|
assert result == expected_result
|
||||||
assert result['title'] == 'Test PR'
|
|
||||||
|
|
||||||
@patch('requests.Session.post')
|
|
||||||
def test_add_labels_to_pull_request(self, mock_post):
|
|
||||||
# Mock the response
|
|
||||||
mock_response = MagicMock()
|
|
||||||
mock_response.status_code = 200
|
|
||||||
mock_post.return_value = mock_response
|
|
||||||
|
|
||||||
# Call the method
|
|
||||||
result = self.client.add_labels_to_pull_request(
|
|
||||||
owner='owner', repo='repo', pull_number=123, labels=['aider', 'bug'],
|
|
||||||
)
|
|
||||||
|
|
||||||
# Verify the call
|
|
||||||
mock_post.assert_called_once()
|
|
||||||
call_args = mock_post.call_args
|
|
||||||
assert (
|
|
||||||
call_args[0][0]
|
|
||||||
== 'https://gitea.example.com/api/v1/repos/owner/repo/issues/123/labels'
|
|
||||||
)
|
|
||||||
assert call_args[1]['json']['labels'] == ['aider', 'bug']
|
|
||||||
|
|
||||||
# Verify the result
|
|
||||||
assert result is True
|
|
||||||
|
|
86
test/test_solve_issue_in_repository.py
Normal file
86
test/test_solve_issue_in_repository.py
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
from pathlib import Path
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
from aider_gitea import solve_issue_in_repository
|
||||||
|
|
||||||
|
|
||||||
|
class TestSolveIssueInRepository:
|
||||||
|
def setup_method(self):
|
||||||
|
self.args = MagicMock()
|
||||||
|
self.args.gitea_url = 'https://gitea.example.com'
|
||||||
|
self.args.owner = 'test-owner'
|
||||||
|
self.args.repo = 'test-repo'
|
||||||
|
self.args.base_branch = 'main'
|
||||||
|
|
||||||
|
self.gitea_client = MagicMock()
|
||||||
|
self.tmpdirname = Path('/tmp/test-repo')
|
||||||
|
self.branch_name = 'issue-123-test-branch'
|
||||||
|
self.issue_title = 'Test Issue'
|
||||||
|
self.issue_description = 'This is a test issue'
|
||||||
|
self.issue_number = '123'
|
||||||
|
|
||||||
|
@patch('aider_gitea.secrets.llm_api_key', return_value='fake-api-key')
|
||||||
|
@patch('aider_gitea.run_cmd')
|
||||||
|
@patch('aider_gitea.push_changes')
|
||||||
|
@patch('subprocess.run')
|
||||||
|
def test_solve_issue_with_aider_changes(
|
||||||
|
self, mock_subprocess_run, mock_push_changes, mock_run_cmd, mock_llm_api_key,
|
||||||
|
):
|
||||||
|
# Setup mocks
|
||||||
|
mock_run_cmd.return_value = True
|
||||||
|
mock_push_changes.return_value = True
|
||||||
|
|
||||||
|
# Mock subprocess.run to return different commit hashes and file changes
|
||||||
|
mock_subprocess_run.side_effect = [
|
||||||
|
MagicMock(stdout='abc123\n', returncode=0), # First git rev-parse
|
||||||
|
MagicMock(
|
||||||
|
stdout='file1.py\nfile2.py\n', returncode=0,
|
||||||
|
), # git diff with changes
|
||||||
|
]
|
||||||
|
|
||||||
|
# Call the function
|
||||||
|
result = solve_issue_in_repository(
|
||||||
|
self.args,
|
||||||
|
self.tmpdirname,
|
||||||
|
self.branch_name,
|
||||||
|
self.issue_title,
|
||||||
|
self.issue_description,
|
||||||
|
self.issue_number,
|
||||||
|
self.gitea_client,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Verify results
|
||||||
|
assert result is True
|
||||||
|
assert mock_run_cmd.call_count >= 8 # Verify all expected commands were run
|
||||||
|
mock_push_changes.assert_called_once()
|
||||||
|
|
||||||
|
@patch('aider_gitea.secrets.llm_api_key', return_value='fake-api-key')
|
||||||
|
@patch('aider_gitea.run_cmd')
|
||||||
|
@patch('aider_gitea.push_changes')
|
||||||
|
@patch('subprocess.run')
|
||||||
|
def test_solve_issue_without_aider_changes(
|
||||||
|
self, mock_subprocess_run, mock_push_changes, mock_run_cmd, mock_llm_api_key,
|
||||||
|
):
|
||||||
|
# Setup mocks
|
||||||
|
mock_run_cmd.return_value = True
|
||||||
|
|
||||||
|
# Mock subprocess.run to return same commit hash and no file changes
|
||||||
|
mock_subprocess_run.side_effect = [
|
||||||
|
MagicMock(stdout='abc123\n', returncode=0), # First git rev-parse
|
||||||
|
MagicMock(stdout='', returncode=0), # git diff with no changes
|
||||||
|
]
|
||||||
|
|
||||||
|
# Call the function
|
||||||
|
result = solve_issue_in_repository(
|
||||||
|
self.args,
|
||||||
|
self.tmpdirname,
|
||||||
|
self.branch_name,
|
||||||
|
self.issue_title,
|
||||||
|
self.issue_description,
|
||||||
|
self.issue_number,
|
||||||
|
self.gitea_client,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Verify results
|
||||||
|
assert result is False
|
||||||
|
assert mock_push_changes.call_count == 0 # push_changes should not be called
|
Loading…
Reference in New Issue
Block a user