feat: add PR review and edit tools to CLI and MCP server
This commit is contained in:
@@ -21,7 +21,11 @@ from mcp_server import ( # noqa: E402
|
||||
gitea_list_prs,
|
||||
gitea_view_pr,
|
||||
gitea_merge_pr,
|
||||
gitea_review_pr,
|
||||
gitea_delete_branch,
|
||||
gitea_edit_pr,
|
||||
gitea_get_file,
|
||||
gitea_commit_files,
|
||||
)
|
||||
|
||||
FAKE_AUTH = "Basic dGVzdDp0ZXN0"
|
||||
@@ -305,6 +309,47 @@ class TestMergePR(unittest.TestCase):
|
||||
self.assertEqual(payload["force_merge"], True)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Review PR
|
||||
# ---------------------------------------------------------------------------
|
||||
class TestReviewPR(unittest.TestCase):
|
||||
|
||||
@patch("mcp_server.api_request")
|
||||
@patch("mcp_server.get_auth_header", return_value=FAKE_AUTH)
|
||||
def test_review_pr_and_merge(self, _auth, mock_api):
|
||||
# GET PR response (fetch head SHA)
|
||||
mock_api.side_effect = [
|
||||
{"head": {"sha": "sha-val-123"}}, # GET PR pulls/1
|
||||
{}, # POST review
|
||||
{}, # POST merge
|
||||
]
|
||||
result = gitea_review_pr(
|
||||
pr_number=1,
|
||||
event="APPROVE",
|
||||
body="Looks good",
|
||||
merge=True,
|
||||
merge_method="squash"
|
||||
)
|
||||
self.assertTrue(result["success"])
|
||||
self.assertIn("Successfully submitted review", result["message"])
|
||||
self.assertIn("Successfully merged", result["message"])
|
||||
|
||||
# Check call counts and arguments
|
||||
self.assertEqual(mock_api.call_count, 3)
|
||||
|
||||
# Verify GET PR
|
||||
self.assertEqual(mock_api.call_args_list[0][0][0], "GET")
|
||||
|
||||
# Verify POST review
|
||||
self.assertEqual(mock_api.call_args_list[1][0][0], "POST")
|
||||
self.assertEqual(mock_api.call_args_list[1][0][3]["event"], "APPROVE")
|
||||
self.assertEqual(mock_api.call_args_list[1][0][3]["commit_id"], "sha-val-123")
|
||||
|
||||
# Verify POST merge
|
||||
self.assertEqual(mock_api.call_args_list[2][0][0], "POST")
|
||||
self.assertEqual(mock_api.call_args_list[2][0][3]["Do"], "squash")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Delete Branch
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -322,5 +367,104 @@ class TestDeleteBranch(unittest.TestCase):
|
||||
self.assertIn("feat%2Fbranch", url)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Edit PR
|
||||
# ---------------------------------------------------------------------------
|
||||
class TestEditPR(unittest.TestCase):
|
||||
|
||||
@patch("mcp_server.api_request")
|
||||
@patch("mcp_server.get_auth_header", return_value=FAKE_AUTH)
|
||||
def test_edit_pr_success(self, _auth, mock_api):
|
||||
mock_api.return_value = {
|
||||
"number": 1,
|
||||
"title": "New Title",
|
||||
"body": "New description",
|
||||
"state": "open",
|
||||
"html_url": "https://gitea.example.com/pulls/1"
|
||||
}
|
||||
result = gitea_edit_pr(
|
||||
pr_number=1,
|
||||
title="New Title",
|
||||
body="New description",
|
||||
state="open"
|
||||
)
|
||||
self.assertTrue(result["success"])
|
||||
self.assertEqual(result["title"], "New Title")
|
||||
self.assertEqual(result["body"], "New description")
|
||||
|
||||
# Verify PATCH and payload
|
||||
call_args = mock_api.call_args
|
||||
self.assertEqual(call_args[0][0], "PATCH")
|
||||
self.assertEqual(call_args[0][3]["title"], "New Title")
|
||||
self.assertEqual(call_args[0][3]["body"], "New description")
|
||||
self.assertEqual(call_args[0][3]["state"], "open")
|
||||
|
||||
def test_edit_pr_no_fields_raises(self):
|
||||
with self.assertRaises(ValueError):
|
||||
gitea_edit_pr(pr_number=1)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Get File
|
||||
# ---------------------------------------------------------------------------
|
||||
class TestGetFile(unittest.TestCase):
|
||||
|
||||
@patch("mcp_server.api_request")
|
||||
@patch("mcp_server.get_auth_header", return_value=FAKE_AUTH)
|
||||
def test_get_file_success(self, _auth, mock_api):
|
||||
mock_api.return_value = {
|
||||
"name": "README.md",
|
||||
"path": "README.md",
|
||||
"sha": "3a0b123",
|
||||
"size": 100,
|
||||
"encoding": "base64",
|
||||
"content": "SGVsbG8gV29ybGQ="
|
||||
}
|
||||
result = gitea_get_file(filepath="README.md", ref="main")
|
||||
self.assertEqual(result["name"], "README.md")
|
||||
self.assertEqual(result["sha"], "3a0b123")
|
||||
self.assertEqual(result["content"], "SGVsbG8gV29ybGQ=")
|
||||
|
||||
# Verify endpoint and GET method
|
||||
call_args = mock_api.call_args
|
||||
self.assertEqual(call_args[0][0], "GET")
|
||||
self.assertIn("contents/README.md", call_args[0][1])
|
||||
self.assertIn("ref=main", call_args[0][1])
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Commit Files
|
||||
# ---------------------------------------------------------------------------
|
||||
class TestCommitFiles(unittest.TestCase):
|
||||
|
||||
@patch("mcp_server.api_request")
|
||||
@patch("mcp_server.get_auth_header", return_value=FAKE_AUTH)
|
||||
def test_commit_files_success(self, _auth, mock_api):
|
||||
mock_api.return_value = {
|
||||
"commit": {"sha": "commit-sha-123"},
|
||||
"branch": {"name": "test-branch"}
|
||||
}
|
||||
files = [
|
||||
{"operation": "create", "path": "test.txt", "content": "SGVsbG8="}
|
||||
]
|
||||
result = gitea_commit_files(
|
||||
files=files,
|
||||
message="Initial commit",
|
||||
new_branch="test-branch"
|
||||
)
|
||||
self.assertTrue(result["success"])
|
||||
self.assertEqual(result["commit"], "commit-sha-123")
|
||||
self.assertEqual(result["branch"], "test-branch")
|
||||
|
||||
# Verify POST method and payload
|
||||
call_args = mock_api.call_args
|
||||
self.assertEqual(call_args[0][0], "POST")
|
||||
self.assertIn("/contents", call_args[0][1])
|
||||
payload = call_args[0][3]
|
||||
self.assertEqual(payload["message"], "Initial commit")
|
||||
self.assertEqual(payload["new_branch"], "test-branch")
|
||||
self.assertEqual(payload["files"], files)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -10,6 +10,7 @@ sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent.par
|
||||
import list_prs # noqa: E402
|
||||
import view_pr # noqa: E402
|
||||
import delete_branch # noqa: E402
|
||||
import edit_pr # noqa: E402
|
||||
|
||||
|
||||
FAKE_CREDS = "Basic dGVzdHVzZXI6dGVzdHBhc3M="
|
||||
@@ -64,5 +65,36 @@ class TestDeleteBranch(unittest.TestCase):
|
||||
delete_branch.main([])
|
||||
|
||||
|
||||
class TestEditPR(unittest.TestCase):
|
||||
|
||||
@patch("edit_pr.api_request")
|
||||
@patch("edit_pr.get_auth_header", return_value=FAKE_CREDS)
|
||||
def test_edit_pr_success(self, _auth, mock_api):
|
||||
mock_api.return_value = {
|
||||
"number": 1,
|
||||
"title": "New Title",
|
||||
"state": "open",
|
||||
"html_url": "http://url1",
|
||||
"base": {"ref": "main"},
|
||||
"body": "New Description"
|
||||
}
|
||||
rc = edit_pr.main(["1", "--title", "New Title", "--body", "New Description"])
|
||||
self.assertEqual(rc, 0)
|
||||
mock_api.assert_called_once()
|
||||
# Verify call payload
|
||||
payload = mock_api.call_args[0][3]
|
||||
self.assertEqual(payload["title"], "New Title")
|
||||
self.assertEqual(payload["body"], "New Description")
|
||||
|
||||
@patch("edit_pr.get_auth_header", return_value=FAKE_CREDS)
|
||||
def test_missing_fields_fails(self, _auth):
|
||||
rc = edit_pr.main(["1"])
|
||||
self.assertEqual(rc, 1)
|
||||
|
||||
def test_missing_pr_number_exits(self):
|
||||
with self.assertRaises(SystemExit):
|
||||
edit_pr.main([])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
"""Tests for review_pr.py.
|
||||
|
||||
Mocks api_request and credentials.
|
||||
"""
|
||||
import sys
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent.parent))
|
||||
import review_pr # noqa: E402
|
||||
|
||||
|
||||
FAKE_CREDS = "Basic dGVzdHVzZXI6dGVzdHBhc3M="
|
||||
FAKE_PR_DATA = {
|
||||
"number": 81,
|
||||
"state": "open",
|
||||
"head": {
|
||||
"ref": "feature-branch",
|
||||
"sha": "abcdef1234567890"
|
||||
},
|
||||
"base": {
|
||||
"ref": "main"
|
||||
},
|
||||
"html_url": "https://gitea.example.com/pulls/81"
|
||||
}
|
||||
|
||||
|
||||
class TestArgParsing(unittest.TestCase):
|
||||
|
||||
@patch("review_pr.get_auth_header", return_value=FAKE_CREDS)
|
||||
def test_missing_pr_number_exits(self, _auth):
|
||||
with self.assertRaises(SystemExit):
|
||||
review_pr.main([])
|
||||
|
||||
|
||||
class TestAPIPayload(unittest.TestCase):
|
||||
|
||||
@patch("review_pr.api_request")
|
||||
@patch("review_pr.get_auth_header", return_value=FAKE_CREDS)
|
||||
def test_payload_fields_and_workflow(self, _auth, mock_api):
|
||||
# Setup mock api_request to return PR details, then review response
|
||||
mock_api.side_effect = [FAKE_PR_DATA, {}]
|
||||
|
||||
rc = review_pr.main([
|
||||
"--pr-number", "81",
|
||||
"--event", "APPROVE",
|
||||
"--body", "Approved and ready to merge",
|
||||
])
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(mock_api.call_count, 2)
|
||||
|
||||
# Verify first call: GET PR
|
||||
first_call_args = mock_api.call_args_list[0]
|
||||
self.assertEqual(first_call_args[0][0], "GET")
|
||||
self.assertEqual(first_call_args[0][1], "https://gitea.dadeschools.net/api/v1/repos/Contractor/Timesheet/pulls/81")
|
||||
|
||||
# Verify second call: POST review
|
||||
second_call_args = mock_api.call_args_list[1]
|
||||
self.assertEqual(second_call_args[0][0], "POST")
|
||||
self.assertEqual(second_call_args[0][1], "https://gitea.dadeschools.net/api/v1/repos/Contractor/Timesheet/pulls/81/reviews")
|
||||
payload = second_call_args[0][3]
|
||||
self.assertEqual(payload["event"], "APPROVE")
|
||||
self.assertEqual(payload["body"], "Approved and ready to merge")
|
||||
self.assertEqual(payload["commit_id"], "abcdef1234567890")
|
||||
|
||||
@patch("review_pr.api_request")
|
||||
@patch("review_pr.get_auth_header", return_value=FAKE_CREDS)
|
||||
def test_approve_and_merge_workflow(self, _auth, mock_api):
|
||||
# Setup mock api_request to return PR details, review response, and merge response
|
||||
mock_api.side_effect = [FAKE_PR_DATA, {}, {}]
|
||||
|
||||
rc = review_pr.main([
|
||||
"--pr-number", "81",
|
||||
"--event", "APPROVE",
|
||||
"--body", "Approved",
|
||||
"--merge",
|
||||
"--merge-method", "squash"
|
||||
])
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(mock_api.call_count, 3)
|
||||
|
||||
# Verify third call: POST merge
|
||||
third_call_args = mock_api.call_args_list[2]
|
||||
self.assertEqual(third_call_args[0][0], "POST")
|
||||
self.assertEqual(third_call_args[0][1], "https://gitea.dadeschools.net/api/v1/repos/Contractor/Timesheet/pulls/81/merge")
|
||||
payload = third_call_args[0][3]
|
||||
self.assertEqual(payload["Do"], "squash")
|
||||
self.assertEqual(payload["force_merge"], False)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user