diff --git a/aider_gitea/gitea_client.py b/aider_gitea/gitea_client.py index d392cab..4ea3ccc 100644 --- a/aider_gitea/gitea_client.py +++ b/aider_gitea/gitea_client.py @@ -168,3 +168,22 @@ class GiteaClient: response = self.session.post(url, json=json_data) response.raise_for_status() return response.json() + + def get_pull_request_comments(self, owner: str, repo: str, pull_number: int) -> list[dict]: + """Get comments for a specific pull request. + + Args: + owner (str): Owner of the repository. + repo (str): Name of the repository. + pull_number (int): The pull request number. + + Returns: + list[dict]: A list of comment dictionaries for the pull request. + + Raises: + requests.HTTPError: If the API request fails. + """ + url = f'{self.gitea_url}/repos/{owner}/{repo}/pulls/{pull_number}/comments' + response = self.session.get(url) + response.raise_for_status() + return response.json() diff --git a/test/test_gitea_client_pr_comments.py b/test/test_gitea_client_pr_comments.py new file mode 100644 index 0000000..0bae54b --- /dev/null +++ b/test/test_gitea_client_pr_comments.py @@ -0,0 +1,68 @@ +import pytest +from unittest.mock import patch, MagicMock + +from aider_gitea.gitea_client import GiteaClient + + +class TestGiteaClientPRComments: + def setup_method(self): + """Set up test fixtures.""" + self.client = GiteaClient('https://gitea.example.com', 'fake-token') + self.owner = 'test-owner' + self.repo = 'test-repo' + self.pull_number = 123 + + @patch('requests.Session.get') + def test_get_pull_request_comments(self, mock_get): + """Test retrieving comments for a pull request.""" + # Mock response data + mock_response = MagicMock() + mock_response.json.return_value = [ + { + 'id': 1, + 'body': 'This is a test comment', + 'user': {'login': 'test-user'}, + 'created_at': '2023-01-01T00:00:00Z', + }, + { + 'id': 2, + 'body': 'Another test comment', + 'user': {'login': 'another-user'}, + 'created_at': '2023-01-02T00:00:00Z', + }, + ] + mock_get.return_value = mock_response + + # Call the method + comments = self.client.get_pull_request_comments(self.owner, self.repo, self.pull_number) + + # Verify the request was made correctly + mock_get.assert_called_once_with( + f'{self.client.gitea_url}/repos/{self.owner}/{self.repo}/pulls/{self.pull_number}/comments' + ) + + # Verify the response was processed correctly + assert len(comments) == 2 + assert comments[0]['id'] == 1 + assert comments[0]['body'] == 'This is a test comment' + assert comments[1]['id'] == 2 + assert comments[1]['body'] == 'Another test comment' + + @patch('requests.Session.get') + def test_get_pull_request_comments_empty(self, mock_get): + """Test retrieving comments for a pull request with no comments.""" + # Mock response data + mock_response = MagicMock() + mock_response.json.return_value = [] + mock_get.return_value = mock_response + + # Call the method + comments = self.client.get_pull_request_comments(self.owner, self.repo, self.pull_number) + + # Verify the request was made correctly + mock_get.assert_called_once_with( + f'{self.client.gitea_url}/repos/{self.owner}/{self.repo}/pulls/{self.pull_number}/comments' + ) + + # Verify the response was processed correctly + assert len(comments) == 0