Compare commits
1 Commits
15bd83fc68
...
9b7fb10288
Author | SHA1 | Date | |
---|---|---|---|
9b7fb10288 |
|
@ -155,16 +155,15 @@ def create_aider_command(issue: str) -> list[str]:
|
|||
AIDER_LINT,
|
||||
'--auto-test',
|
||||
'--no-auto-lint',
|
||||
'--api-key',
|
||||
secrets.llm_api_key(),
|
||||
'--read',
|
||||
'CONVENTIONS.md',
|
||||
'--message',
|
||||
LLM_MESSAGE_FORMAT.format(issue=issue),
|
||||
'--yes',
|
||||
'--yes-always',
|
||||
]
|
||||
|
||||
for key in secrets.llm_api_keys():
|
||||
l += ['--api-key', key]
|
||||
|
||||
if True:
|
||||
l.append('--cache-prompts')
|
||||
|
||||
|
@ -229,26 +228,9 @@ def push_changes(
|
|||
|
||||
# First push the branch without creating a PR
|
||||
cmd = ['git', 'push', 'origin', branch_name, '--force']
|
||||
push_success = run_cmd(cmd, cwd, check=False)
|
||||
|
||||
if not push_success:
|
||||
error_message = f'Failed to push branch `{branch_name}`. The changes could not be uploaded to the repository.'
|
||||
logger.error(error_message)
|
||||
try:
|
||||
gitea_client.create_issue_comment(
|
||||
owner=owner,
|
||||
repo=repo,
|
||||
issue_number=issue_number,
|
||||
body=f'❌ **Automated Solution Failed**\n\n{error_message}\n\nPlease check repository permissions and try again.',
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception(
|
||||
f'Failed to comment on issue #{issue_number} after push failure: {e}',
|
||||
)
|
||||
return False
|
||||
run_cmd(cmd, cwd)
|
||||
|
||||
# Then create the PR with the aider label
|
||||
try:
|
||||
gitea_client.create_pull_request(
|
||||
owner=owner,
|
||||
repo=repo,
|
||||
|
@ -259,22 +241,6 @@ def push_changes(
|
|||
labels=['aider'],
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
error_message = f'Failed to create pull request for branch `{branch_name}`. The changes were pushed but the PR could not be created.'
|
||||
logger.exception(f'{error_message}: {e}')
|
||||
try:
|
||||
gitea_client.create_issue_comment(
|
||||
owner=owner,
|
||||
repo=repo,
|
||||
issue_number=issue_number,
|
||||
body=f'⚠️ **Partial Automation Success**\n\n{error_message}\n\n'
|
||||
f"The changes are available in the branch `{branch_name}`, but you'll need to create the PR manually.",
|
||||
)
|
||||
except Exception as comment_error:
|
||||
logger.exception(
|
||||
f'Failed to comment on issue #{issue_number} after PR creation failure: {comment_error}',
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
def has_commits_on_branch(cwd: Path, base_branch: str, current_branch: str) -> bool:
|
||||
|
@ -317,9 +283,6 @@ def run_cmd(cmd: list[str], cwd: Path | None = None, check=True) -> bool:
|
|||
return result.returncode == 0
|
||||
|
||||
|
||||
SKIP_AIDER = False
|
||||
|
||||
|
||||
def solve_issue_in_repository(
|
||||
args,
|
||||
tmpdirname: Path,
|
||||
|
@ -329,8 +292,6 @@ def solve_issue_in_repository(
|
|||
issue_number: str,
|
||||
gitea_client=None,
|
||||
) -> bool:
|
||||
logger.info('### %s #####', issue_title)
|
||||
|
||||
repo_url = f'{args.gitea_url}:{args.owner}/{args.repo}.git'.replace(
|
||||
'https://',
|
||||
'git@',
|
||||
|
@ -359,15 +320,11 @@ def solve_issue_in_repository(
|
|||
|
||||
# Run aider
|
||||
issue_content = f'# {issue_title}\n{issue_description}'
|
||||
if not SKIP_AIDER:
|
||||
succeeded = run_cmd(
|
||||
create_aider_command(issue_content),
|
||||
tmpdirname,
|
||||
check=False,
|
||||
)
|
||||
else:
|
||||
logger.warning('Skipping aider command (for testing)')
|
||||
succeeded = True
|
||||
if not succeeded:
|
||||
logger.error('Aider invocation failed for issue #%s', issue_number)
|
||||
return False
|
||||
|
@ -387,7 +344,7 @@ def solve_issue_in_repository(
|
|||
)
|
||||
files_changed = result.stdout.strip()
|
||||
|
||||
if not files_changed and not SKIP_AIDER:
|
||||
if not files_changed:
|
||||
logger.info(
|
||||
'Aider did not make any changes beyond the initial ruff pass for issue #%s',
|
||||
issue_number,
|
||||
|
|
|
@ -163,32 +163,9 @@ class GiteaClient:
|
|||
'body': body,
|
||||
'head': head,
|
||||
'base': base,
|
||||
'labels': labels,
|
||||
}
|
||||
|
||||
response = self.session.post(url, json=json_data)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
def create_issue_comment(
|
||||
self, owner: str, repo: str, issue_number: str, body: str,
|
||||
) -> dict:
|
||||
"""Create a comment on an issue.
|
||||
|
||||
Args:
|
||||
owner (str): Owner of the repository.
|
||||
repo (str): Name of the repository.
|
||||
issue_number (str): The issue number to comment on.
|
||||
body (str): The content of the comment.
|
||||
|
||||
Returns:
|
||||
dict: The created comment data.
|
||||
|
||||
Raises:
|
||||
requests.HTTPError: If the API request fails.
|
||||
"""
|
||||
url = f'{self.gitea_url}/repos/{owner}/{repo}/issues/{issue_number}/comments'
|
||||
json_data = {'body': body}
|
||||
|
||||
response = self.session.post(url, json=json_data)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
|
|
@ -3,9 +3,9 @@ import secret_loader
|
|||
SECRETS = secret_loader.SecretLoader()
|
||||
|
||||
|
||||
def llm_api_keys() -> list[str]:
|
||||
return SECRETS.load_or_fail('LLM_API_KEY').strip().split('\n')
|
||||
def llm_api_key():
|
||||
return SECRETS.load_or_fail('LLM_API_KEY')
|
||||
|
||||
|
||||
def gitea_token() -> str:
|
||||
def gitea_token():
|
||||
return SECRETS.load_or_fail('GITEA_TOKEN')
|
||||
|
|
|
@ -1,88 +0,0 @@
|
|||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from aider_gitea import push_changes
|
||||
|
||||
|
||||
class TestIssueCommentOnFailure:
|
||||
def setup_method(self):
|
||||
self.cwd = Path('/tmp/test-repo')
|
||||
self.branch_name = 'issue-123-test-branch'
|
||||
self.issue_number = '123'
|
||||
self.issue_title = 'Test Issue'
|
||||
self.base_branch = 'main'
|
||||
self.gitea_client = MagicMock()
|
||||
self.owner = 'test-owner'
|
||||
self.repo = 'test-repo'
|
||||
|
||||
@patch('aider_gitea.has_commits_on_branch', return_value=True)
|
||||
@patch('aider_gitea.get_commit_messages', return_value=['Test commit'])
|
||||
@patch('aider_gitea.run_cmd')
|
||||
def test_comment_on_push_failure(
|
||||
self, mock_run_cmd, mock_get_commit_messages, mock_has_commits,
|
||||
):
|
||||
# Setup run_cmd to fail on git push
|
||||
mock_run_cmd.return_value = False
|
||||
|
||||
# Call push_changes
|
||||
result = push_changes(
|
||||
self.cwd,
|
||||
self.branch_name,
|
||||
self.issue_number,
|
||||
self.issue_title,
|
||||
self.base_branch,
|
||||
self.gitea_client,
|
||||
self.owner,
|
||||
self.repo,
|
||||
)
|
||||
|
||||
# Verify result is False
|
||||
assert result is False
|
||||
|
||||
# Verify create_issue_comment was called with appropriate message
|
||||
self.gitea_client.create_issue_comment.assert_called_once()
|
||||
args, kwargs = self.gitea_client.create_issue_comment.call_args
|
||||
assert kwargs['owner'] == self.owner
|
||||
assert kwargs['repo'] == self.repo
|
||||
assert kwargs['issue_number'] == self.issue_number
|
||||
assert 'Failed to push branch' in kwargs['body']
|
||||
assert '❌ **Automated Solution Failed**' in kwargs['body']
|
||||
|
||||
@patch('aider_gitea.has_commits_on_branch', return_value=True)
|
||||
@patch('aider_gitea.get_commit_messages', return_value=['Test commit'])
|
||||
@patch('aider_gitea.run_cmd')
|
||||
def test_comment_on_pr_creation_failure(
|
||||
self, mock_run_cmd, mock_get_commit_messages, mock_has_commits,
|
||||
):
|
||||
# Setup run_cmd to succeed on git push
|
||||
mock_run_cmd.return_value = True
|
||||
|
||||
# Setup create_pull_request to fail
|
||||
self.gitea_client.create_pull_request.side_effect = Exception(
|
||||
'PR creation failed',
|
||||
)
|
||||
|
||||
# Call push_changes
|
||||
result = push_changes(
|
||||
self.cwd,
|
||||
self.branch_name,
|
||||
self.issue_number,
|
||||
self.issue_title,
|
||||
self.base_branch,
|
||||
self.gitea_client,
|
||||
self.owner,
|
||||
self.repo,
|
||||
)
|
||||
|
||||
# Verify result is False
|
||||
assert result is False
|
||||
|
||||
# Verify create_issue_comment was called with appropriate message
|
||||
self.gitea_client.create_issue_comment.assert_called_once()
|
||||
args, kwargs = self.gitea_client.create_issue_comment.call_args
|
||||
assert kwargs['owner'] == self.owner
|
||||
assert kwargs['repo'] == self.repo
|
||||
assert kwargs['issue_number'] == self.issue_number
|
||||
assert 'Failed to create pull request' in kwargs['body']
|
||||
assert '⚠️ **Partial Automation Success**' in kwargs['body']
|
||||
assert self.branch_name in kwargs['body']
|
|
@ -19,7 +19,7 @@ class TestSolveIssueInRepository:
|
|||
self.issue_description = 'This is a test issue'
|
||||
self.issue_number = '123'
|
||||
|
||||
@patch('aider_gitea.secrets.llm_api_keys', return_value='fake-api-key')
|
||||
@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')
|
||||
|
@ -59,7 +59,7 @@ class TestSolveIssueInRepository:
|
|||
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_keys', return_value='fake-api-key')
|
||||
@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')
|
||||
|
|
Loading…
Reference in New Issue
Block a user