Compare commits
3 Commits
7937ddfdb9
...
7bc60d42af
Author | SHA1 | Date | |
---|---|---|---|
7bc60d42af | |||
31f9964d1f | |||
91d342868f |
|
@ -228,9 +228,26 @@ def push_changes(
|
|||
|
||||
# First push the branch without creating a PR
|
||||
cmd = ['git', 'push', 'origin', branch_name, '--force']
|
||||
run_cmd(cmd, cwd)
|
||||
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
|
||||
|
||||
# Then create the PR with the aider label
|
||||
try:
|
||||
gitea_client.create_pull_request(
|
||||
owner=owner,
|
||||
repo=repo,
|
||||
|
@ -241,6 +258,23 @@ def push_changes(
|
|||
labels=['aider'],
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
error_message = (
|
||||
f"Failed to create pull request for branch '{branch_name}': {str(e)}"
|
||||
)
|
||||
logger.exception(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\nThe changes were pushed to branch `{branch_name}` but creating a pull request failed.',
|
||||
)
|
||||
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:
|
||||
|
|
|
@ -169,3 +169,27 @@ class GiteaClient:
|
|||
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()
|
||||
|
|
97
test/test_issue_comment_on_failure.py
Normal file
97
test/test_issue_comment_on_failure.py
Normal file
|
@ -0,0 +1,97 @@
|
|||
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.run_cmd', return_value=False)
|
||||
@patch('aider_gitea.has_commits_on_branch', return_value=True)
|
||||
@patch('aider_gitea.get_commit_messages', return_value=['Test commit'])
|
||||
def test_comment_on_push_failure(
|
||||
self, mock_get_commit_messages, mock_has_commits, mock_run_cmd,
|
||||
):
|
||||
"""Test that a comment is added to the issue when git push fails."""
|
||||
result = push_changes(
|
||||
self.cwd,
|
||||
self.branch_name,
|
||||
self.issue_number,
|
||||
self.issue_title,
|
||||
self.base_branch,
|
||||
self.gitea_client,
|
||||
self.owner,
|
||||
self.repo,
|
||||
)
|
||||
|
||||
assert result is False
|
||||
self.gitea_client.create_issue_comment.assert_called_once()
|
||||
comment_body = self.gitea_client.create_issue_comment.call_args[1]['body']
|
||||
assert 'Failed to push branch' in comment_body
|
||||
assert '❌ **Automated Solution Failed**' in comment_body
|
||||
|
||||
@patch('aider_gitea.run_cmd', return_value=True)
|
||||
@patch('aider_gitea.has_commits_on_branch', return_value=True)
|
||||
@patch('aider_gitea.get_commit_messages', return_value=['Test commit'])
|
||||
def test_comment_on_pr_creation_failure(
|
||||
self, mock_get_commit_messages, mock_has_commits, mock_run_cmd,
|
||||
):
|
||||
"""Test that a comment is added to the issue when PR creation fails."""
|
||||
self.gitea_client.create_pull_request.side_effect = Exception(
|
||||
'PR creation failed',
|
||||
)
|
||||
|
||||
result = push_changes(
|
||||
self.cwd,
|
||||
self.branch_name,
|
||||
self.issue_number,
|
||||
self.issue_title,
|
||||
self.base_branch,
|
||||
self.gitea_client,
|
||||
self.owner,
|
||||
self.repo,
|
||||
)
|
||||
|
||||
assert result is False
|
||||
self.gitea_client.create_issue_comment.assert_called_once()
|
||||
comment_body = self.gitea_client.create_issue_comment.call_args[1]['body']
|
||||
assert 'Failed to create pull request' in comment_body
|
||||
assert '❌ **Automated Solution Failed**' in comment_body
|
||||
assert self.branch_name in comment_body
|
||||
|
||||
@patch('aider_gitea.run_cmd', return_value=True)
|
||||
@patch('aider_gitea.has_commits_on_branch', return_value=True)
|
||||
@patch('aider_gitea.get_commit_messages', return_value=['Test commit'])
|
||||
def test_comment_failure_handled(
|
||||
self, mock_get_commit_messages, mock_has_commits, mock_run_cmd,
|
||||
):
|
||||
"""Test that exceptions during commenting are properly handled."""
|
||||
self.gitea_client.create_pull_request.side_effect = Exception(
|
||||
'PR creation failed',
|
||||
)
|
||||
self.gitea_client.create_issue_comment.side_effect = Exception(
|
||||
'Comment creation failed',
|
||||
)
|
||||
|
||||
# This should not raise an exception
|
||||
result = push_changes(
|
||||
self.cwd,
|
||||
self.branch_name,
|
||||
self.issue_number,
|
||||
self.issue_title,
|
||||
self.base_branch,
|
||||
self.gitea_client,
|
||||
self.owner,
|
||||
self.repo,
|
||||
)
|
||||
|
||||
assert result is False
|
|
@ -24,7 +24,11 @@ class TestSolveIssueInRepository:
|
|||
@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,
|
||||
self,
|
||||
mock_subprocess_run,
|
||||
mock_push_changes,
|
||||
mock_run_cmd,
|
||||
mock_llm_api_key,
|
||||
):
|
||||
# Setup mocks
|
||||
mock_run_cmd.return_value = True
|
||||
|
@ -34,7 +38,8 @@ class TestSolveIssueInRepository:
|
|||
mock_subprocess_run.side_effect = [
|
||||
MagicMock(stdout='abc123\n', returncode=0), # First git rev-parse
|
||||
MagicMock(
|
||||
stdout='file1.py\nfile2.py\n', returncode=0,
|
||||
stdout='file1.py\nfile2.py\n',
|
||||
returncode=0,
|
||||
), # git diff with changes
|
||||
]
|
||||
|
||||
|
@ -59,7 +64,11 @@ class TestSolveIssueInRepository:
|
|||
@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,
|
||||
self,
|
||||
mock_subprocess_run,
|
||||
mock_push_changes,
|
||||
mock_run_cmd,
|
||||
mock_llm_api_key,
|
||||
):
|
||||
# Setup mocks
|
||||
mock_run_cmd.return_value = True
|
||||
|
|
Loading…
Reference in New Issue
Block a user