Ruff
All checks were successful
Run Python tests (through Pytest) / Test (push) Successful in 24s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 22s

This commit is contained in:
Jon Michael Aanes 2025-04-15 00:20:00 +02:00
parent 2bb7a378ab
commit 07fca9b3c2
3 changed files with 58 additions and 61 deletions

View File

@ -228,7 +228,7 @@ def push_changes(
# First push the branch without creating a PR
cmd = ['git', 'push', 'origin', branch_name]
run_cmd(cmd, cwd)
# Then create the PR with the aider label
gitea_client.create_pull_request(
owner=owner,
@ -237,7 +237,7 @@ def push_changes(
body=description,
head=branch_name,
base=base_branch,
labels=['aider']
labels=['aider'],
)
return True
else:

View File

@ -129,19 +129,19 @@ class GiteaClient:
if repo['owner']['login'].lower() != owner.lower():
continue
yield repo['name']
def create_pull_request(
self,
owner: str,
repo: str,
title: str,
body: str,
head: str,
base: str,
labels: list[str] = None
self,
owner: str,
repo: str,
title: str,
body: str,
head: str,
base: str,
labels: list[str] = None,
) -> dict:
"""Create a pull request and optionally apply labels.
Args:
owner (str): Owner of the repository.
repo (str): Name of the repository.
@ -150,55 +150,46 @@ class GiteaClient:
head (str): The name of the branch where changes are implemented.
base (str): The name of the branch you want the changes pulled into.
labels (list[str], optional): List of label names to apply to the pull request.
Returns:
dict: The created pull request data.
Raises:
requests.HTTPError: If the API request fails.
"""
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}
response = self.session.post(url, json=json_data)
response.raise_for_status()
pull_request = 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]
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

View File

@ -1,12 +1,12 @@
import pytest
from unittest.mock import MagicMock, patch
from aider_gitea.gitea_client import GiteaClient
class TestGiteaClientPRLabels:
def setup_method(self):
self.client = GiteaClient("https://gitea.example.com", "fake_token")
self.client = GiteaClient('https://gitea.example.com', 'fake_token')
@patch('requests.Session.post')
def test_create_pull_request_with_labels(self, mock_post):
# Mock the PR creation response
@ -15,62 +15,68 @@ class TestGiteaClientPRLabels:
pr_response.json.return_value = {
'number': 123,
'title': 'Test PR',
'html_url': 'https://gitea.example.com/owner/repo/pulls/123'
'html_url': 'https://gitea.example.com/owner/repo/pulls/123',
}
# Mock the label addition response
label_response = MagicMock()
label_response.status_code = 200
# Set up the mock to return different responses for different calls
mock_post.side_effect = [pr_response, label_response]
# Call the method with labels
result = self.client.create_pull_request(
owner="owner",
repo="repo",
title="Test PR",
body="Test body",
head="feature-branch",
base="main",
labels=["aider"]
owner='owner',
repo='repo',
title='Test PR',
body='Test body',
head='feature-branch',
base='main',
labels=['aider'],
)
# Verify PR creation call
assert mock_post.call_count == 2
pr_call_args = mock_post.call_args_list[0]
assert pr_call_args[0][0] == 'https://gitea.example.com/api/v1/repos/owner/repo/pulls'
assert (
pr_call_args[0][0]
== 'https://gitea.example.com/api/v1/repos/owner/repo/pulls'
)
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[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
assert result['number'] == 123
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"]
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[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