Ruff after aider
This commit is contained in:
parent
e32f53d485
commit
15bd83fc68
|
@ -230,19 +230,21 @@ def push_changes(
|
||||||
# First push the branch without creating a PR
|
# First push the branch without creating a PR
|
||||||
cmd = ['git', 'push', 'origin', branch_name, '--force']
|
cmd = ['git', 'push', 'origin', branch_name, '--force']
|
||||||
push_success = run_cmd(cmd, cwd, check=False)
|
push_success = run_cmd(cmd, cwd, check=False)
|
||||||
|
|
||||||
if not push_success:
|
if not push_success:
|
||||||
error_message = f"Failed to push branch `{branch_name}`. The changes could not be uploaded to the repository."
|
error_message = f'Failed to push branch `{branch_name}`. The changes could not be uploaded to the repository.'
|
||||||
logger.error(error_message)
|
logger.error(error_message)
|
||||||
try:
|
try:
|
||||||
gitea_client.create_issue_comment(
|
gitea_client.create_issue_comment(
|
||||||
owner=owner,
|
owner=owner,
|
||||||
repo=repo,
|
repo=repo,
|
||||||
issue_number=issue_number,
|
issue_number=issue_number,
|
||||||
body=f"❌ **Automated Solution Failed**\n\n{error_message}\n\nPlease check repository permissions and try again."
|
body=f'❌ **Automated Solution Failed**\n\n{error_message}\n\nPlease check repository permissions and try again.',
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(f"Failed to comment on issue #{issue_number} after push failure: {e}")
|
logger.exception(
|
||||||
|
f'Failed to comment on issue #{issue_number} after push failure: {e}',
|
||||||
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Then create the PR with the aider label
|
# Then create the PR with the aider label
|
||||||
|
@ -258,18 +260,20 @@ def push_changes(
|
||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
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."
|
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}")
|
logger.exception(f'{error_message}: {e}')
|
||||||
try:
|
try:
|
||||||
gitea_client.create_issue_comment(
|
gitea_client.create_issue_comment(
|
||||||
owner=owner,
|
owner=owner,
|
||||||
repo=repo,
|
repo=repo,
|
||||||
issue_number=issue_number,
|
issue_number=issue_number,
|
||||||
body=f"⚠️ **Partial Automation Success**\n\n{error_message}\n\n"
|
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."
|
f"The changes are available in the branch `{branch_name}`, but you'll need to create the PR manually.",
|
||||||
)
|
)
|
||||||
except Exception as comment_error:
|
except Exception as comment_error:
|
||||||
logger.exception(f"Failed to comment on issue #{issue_number} after PR creation failure: {comment_error}")
|
logger.exception(
|
||||||
|
f'Failed to comment on issue #{issue_number} after PR creation failure: {comment_error}',
|
||||||
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -168,25 +168,27 @@ class GiteaClient:
|
||||||
response = self.session.post(url, json=json_data)
|
response = self.session.post(url, json=json_data)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def create_issue_comment(self, owner: str, repo: str, issue_number: str, body: str) -> dict:
|
def create_issue_comment(
|
||||||
|
self, owner: str, repo: str, issue_number: str, body: str,
|
||||||
|
) -> dict:
|
||||||
"""Create a comment on an issue.
|
"""Create a comment on an issue.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
owner (str): Owner of the repository.
|
owner (str): Owner of the repository.
|
||||||
repo (str): Name of the repository.
|
repo (str): Name of the repository.
|
||||||
issue_number (str): The issue number to comment on.
|
issue_number (str): The issue number to comment on.
|
||||||
body (str): The content of the comment.
|
body (str): The content of the comment.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: The created comment data.
|
dict: The created comment data.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
requests.HTTPError: If the API request fails.
|
requests.HTTPError: If the API request fails.
|
||||||
"""
|
"""
|
||||||
url = f'{self.gitea_url}/repos/{owner}/{repo}/issues/{issue_number}/comments'
|
url = f'{self.gitea_url}/repos/{owner}/{repo}/issues/{issue_number}/comments'
|
||||||
json_data = {'body': body}
|
json_data = {'body': body}
|
||||||
|
|
||||||
response = self.session.post(url, json=json_data)
|
response = self.session.post(url, json=json_data)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import pytest
|
|
||||||
from unittest.mock import MagicMock, patch
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
from aider_gitea import push_changes
|
from aider_gitea import push_changes
|
||||||
|
|
||||||
|
@ -15,14 +14,16 @@ class TestIssueCommentOnFailure:
|
||||||
self.gitea_client = MagicMock()
|
self.gitea_client = MagicMock()
|
||||||
self.owner = 'test-owner'
|
self.owner = 'test-owner'
|
||||||
self.repo = 'test-repo'
|
self.repo = 'test-repo'
|
||||||
|
|
||||||
@patch('aider_gitea.has_commits_on_branch', return_value=True)
|
@patch('aider_gitea.has_commits_on_branch', return_value=True)
|
||||||
@patch('aider_gitea.get_commit_messages', return_value=['Test commit'])
|
@patch('aider_gitea.get_commit_messages', return_value=['Test commit'])
|
||||||
@patch('aider_gitea.run_cmd')
|
@patch('aider_gitea.run_cmd')
|
||||||
def test_comment_on_push_failure(self, mock_run_cmd, mock_get_commit_messages, mock_has_commits):
|
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
|
# Setup run_cmd to fail on git push
|
||||||
mock_run_cmd.return_value = False
|
mock_run_cmd.return_value = False
|
||||||
|
|
||||||
# Call push_changes
|
# Call push_changes
|
||||||
result = push_changes(
|
result = push_changes(
|
||||||
self.cwd,
|
self.cwd,
|
||||||
|
@ -32,31 +33,35 @@ class TestIssueCommentOnFailure:
|
||||||
self.base_branch,
|
self.base_branch,
|
||||||
self.gitea_client,
|
self.gitea_client,
|
||||||
self.owner,
|
self.owner,
|
||||||
self.repo
|
self.repo,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Verify result is False
|
# Verify result is False
|
||||||
assert result is False
|
assert result is False
|
||||||
|
|
||||||
# Verify create_issue_comment was called with appropriate message
|
# Verify create_issue_comment was called with appropriate message
|
||||||
self.gitea_client.create_issue_comment.assert_called_once()
|
self.gitea_client.create_issue_comment.assert_called_once()
|
||||||
args, kwargs = self.gitea_client.create_issue_comment.call_args
|
args, kwargs = self.gitea_client.create_issue_comment.call_args
|
||||||
assert kwargs['owner'] == self.owner
|
assert kwargs['owner'] == self.owner
|
||||||
assert kwargs['repo'] == self.repo
|
assert kwargs['repo'] == self.repo
|
||||||
assert kwargs['issue_number'] == self.issue_number
|
assert kwargs['issue_number'] == self.issue_number
|
||||||
assert "Failed to push branch" in kwargs['body']
|
assert 'Failed to push branch' in kwargs['body']
|
||||||
assert "❌ **Automated Solution Failed**" in kwargs['body']
|
assert '❌ **Automated Solution Failed**' in kwargs['body']
|
||||||
|
|
||||||
@patch('aider_gitea.has_commits_on_branch', return_value=True)
|
@patch('aider_gitea.has_commits_on_branch', return_value=True)
|
||||||
@patch('aider_gitea.get_commit_messages', return_value=['Test commit'])
|
@patch('aider_gitea.get_commit_messages', return_value=['Test commit'])
|
||||||
@patch('aider_gitea.run_cmd')
|
@patch('aider_gitea.run_cmd')
|
||||||
def test_comment_on_pr_creation_failure(self, mock_run_cmd, mock_get_commit_messages, mock_has_commits):
|
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
|
# Setup run_cmd to succeed on git push
|
||||||
mock_run_cmd.return_value = True
|
mock_run_cmd.return_value = True
|
||||||
|
|
||||||
# Setup create_pull_request to fail
|
# Setup create_pull_request to fail
|
||||||
self.gitea_client.create_pull_request.side_effect = Exception("PR creation failed")
|
self.gitea_client.create_pull_request.side_effect = Exception(
|
||||||
|
'PR creation failed',
|
||||||
|
)
|
||||||
|
|
||||||
# Call push_changes
|
# Call push_changes
|
||||||
result = push_changes(
|
result = push_changes(
|
||||||
self.cwd,
|
self.cwd,
|
||||||
|
@ -66,18 +71,18 @@ class TestIssueCommentOnFailure:
|
||||||
self.base_branch,
|
self.base_branch,
|
||||||
self.gitea_client,
|
self.gitea_client,
|
||||||
self.owner,
|
self.owner,
|
||||||
self.repo
|
self.repo,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Verify result is False
|
# Verify result is False
|
||||||
assert result is False
|
assert result is False
|
||||||
|
|
||||||
# Verify create_issue_comment was called with appropriate message
|
# Verify create_issue_comment was called with appropriate message
|
||||||
self.gitea_client.create_issue_comment.assert_called_once()
|
self.gitea_client.create_issue_comment.assert_called_once()
|
||||||
args, kwargs = self.gitea_client.create_issue_comment.call_args
|
args, kwargs = self.gitea_client.create_issue_comment.call_args
|
||||||
assert kwargs['owner'] == self.owner
|
assert kwargs['owner'] == self.owner
|
||||||
assert kwargs['repo'] == self.repo
|
assert kwargs['repo'] == self.repo
|
||||||
assert kwargs['issue_number'] == self.issue_number
|
assert kwargs['issue_number'] == self.issue_number
|
||||||
assert "Failed to create pull request" in kwargs['body']
|
assert 'Failed to create pull request' in kwargs['body']
|
||||||
assert "⚠️ **Partial Automation Success**" in kwargs['body']
|
assert '⚠️ **Partial Automation Success**' in kwargs['body']
|
||||||
assert self.branch_name in kwargs['body']
|
assert self.branch_name in kwargs['body']
|
||||||
|
|
Loading…
Reference in New Issue
Block a user