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

View File

@ -129,19 +129,19 @@ class GiteaClient:
if repo['owner']['login'].lower() != owner.lower(): if repo['owner']['login'].lower() != owner.lower():
continue continue
yield repo['name'] yield repo['name']
def create_pull_request( def create_pull_request(
self, self,
owner: str, owner: str,
repo: str, repo: str,
title: str, title: str,
body: str, body: str,
head: str, head: str,
base: str, base: str,
labels: list[str] = None labels: list[str] = None,
) -> dict: ) -> dict:
"""Create a pull request and optionally apply labels. """Create a pull request and optionally apply labels.
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.
@ -150,55 +150,46 @@ class GiteaClient:
head (str): The name of the branch where changes are implemented. head (str): The name of the branch where changes are implemented.
base (str): The name of the branch you want the changes pulled into. 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. labels (list[str], optional): List of label names to apply to the pull request.
Returns: Returns:
dict: The created pull request data. dict: The created pull request 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}/pulls' url = f'{self.gitea_url}/repos/{owner}/{repo}/pulls'
json_data = { json_data = {'title': title, 'body': body, 'head': head, 'base': base}
'title': title,
'body': body,
'head': head,
'base': base
}
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() pull_request = response.json()
# Apply labels if provided # Apply labels if provided
if labels and pull_request.get('number'): if labels and pull_request.get('number'):
self.add_labels_to_pull_request(owner, repo, pull_request['number'], labels) self.add_labels_to_pull_request(owner, repo, pull_request['number'], labels)
return pull_request return pull_request
def add_labels_to_pull_request( def add_labels_to_pull_request(
self, self, owner: str, repo: str, pull_number: int, labels: list[str],
owner: str,
repo: str,
pull_number: int,
labels: list[str]
) -> bool: ) -> bool:
"""Add labels to an existing pull request. """Add labels to an existing pull request.
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.
pull_number (int): The pull request number. pull_number (int): The pull request number.
labels (list[str]): List of label names to apply. labels (list[str]): List of label names to apply.
Returns: Returns:
bool: True if labels were successfully applied. bool: True if labels were successfully applied.
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/{pull_number}/labels' url = f'{self.gitea_url}/repos/{owner}/{repo}/issues/{pull_number}/labels'
json_data = {'labels': labels} json_data = {'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()
return True return True

View File

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