Removed lots of redundant code
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:36:02 +02:00
parent 903920bdfd
commit d25dae34f0
2 changed files with 6 additions and 68 deletions

View File

@ -158,38 +158,8 @@ class GiteaClient:
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 = {'title': title, 'body': body, 'head': head, 'base': base} json_data = {'title': title, 'body': body, 'head': head, 'base': base, '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()
pull_request = response.json() return 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],
) -> 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

@ -12,11 +12,12 @@ class TestGiteaClientPRLabels:
# Mock the PR creation response # Mock the PR creation response
pr_response = MagicMock() pr_response = MagicMock()
pr_response.status_code = 201 pr_response.status_code = 201
pr_response.json.return_value = { expected_result = {
'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',
} }
pr_response.json.return_value = expected_result
# Mock the label addition response # Mock the label addition response
label_response = MagicMock() label_response = MagicMock()
@ -37,7 +38,7 @@ class TestGiteaClientPRLabels:
) )
# Verify PR creation call # Verify PR creation call
assert mock_post.call_count == 2 assert mock_post.call_count == 1
pr_call_args = mock_post.call_args_list[0] pr_call_args = mock_post.call_args_list[0]
assert ( assert (
pr_call_args[0][0] pr_call_args[0][0]
@ -45,38 +46,5 @@ class TestGiteaClientPRLabels:
) )
assert pr_call_args[1]['json']['title'] == 'Test PR' 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[1]['json']['labels'] == ['aider']
# Verify the result # Verify the result
assert result['number'] == 123 assert result == expected_result
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'],
)
# 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[1]['json']['labels'] == ['aider', 'bug']
# Verify the result
assert result is True