diff --git a/aider_gitea/__init__.py b/aider_gitea/__init__.py index 7795aac..c78e0fb 100644 --- a/aider_gitea/__init__.py +++ b/aider_gitea/__init__.py @@ -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: diff --git a/aider_gitea/gitea_client.py b/aider_gitea/gitea_client.py index c712a4f..b88e82b 100644 --- a/aider_gitea/gitea_client.py +++ b/aider_gitea/gitea_client.py @@ -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 diff --git a/test/test_gitea_client_pr_labels.py b/test/test_gitea_client_pr_labels.py index 31dfc20..9127278 100644 --- a/test/test_gitea_client_pr_labels.py +++ b/test/test_gitea_client_pr_labels.py @@ -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