feat: add MCP server + shared auth module (#7, #1)

- New: mcp_server.py — FastMCP stdio server exposing 7 tools:
  gitea_create_issue, gitea_create_pr, gitea_close_issue,
  gitea_list_issues, gitea_view_issue, gitea_mark_issue,
  gitea_mirror_refs
- New: auth.py — shared authentication and API helpers
  (get_credentials, get_auth_header, api_request, repo_api_url)
- Refactored: create_pr.py, create_issue.py, manage_labels.py
  to use shared auth module (eliminates credential duplication)
- New: tests/test_mcp_server.py — 17 tests for all MCP tools
- Updated: tests/test_credentials.py — now tests auth.py directly
- Updated: tests/test_create_issue.py — adapted for refactored imports
- New: requirements.txt — frozen venv deps (mcp[cli], pytest)
- Updated: README.md — MCP server as primary interface
- Config: added gitea-tools to mcp_config.json

Closes #1. Resolves #2, #5. Relates to #7.
This commit is contained in:
2026-06-21 20:08:07 -04:00
parent dd6f1308c1
commit b7e195e426
11 changed files with 978 additions and 214 deletions
+34 -12
View File
@@ -1,4 +1,4 @@
"""Tests for the shared get_credentials() function used by create_issue.py and create_pr.py.
"""Tests for the shared get_credentials() function in auth.py.
These test the credential parsing logic in isolation by mocking subprocess.Popen.
"""
@@ -7,7 +7,7 @@ import unittest
from unittest.mock import MagicMock, patch
sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent.parent))
import create_pr # noqa: E402 (get_credentials is identical in create_issue.py and create_pr.py)
import auth # noqa: E402
class TestGetCredentials(unittest.TestCase):
@@ -19,45 +19,45 @@ class TestGetCredentials(unittest.TestCase):
mock_proc.communicate.return_value = (output_text, "")
return mock_proc
@patch("create_pr.subprocess.Popen")
@patch("auth.subprocess.Popen")
def test_parses_standard_output(self, mock_popen_cls):
mock_popen_cls.return_value = self._mock_popen(
"protocol=https\nhost=gitea.example.com\nusername=admin\npassword=s3cret\n"
)
user, password = create_pr.get_credentials("gitea.example.com")
user, password = auth.get_credentials("gitea.example.com")
self.assertEqual(user, "admin")
self.assertEqual(password, "s3cret")
@patch("create_pr.subprocess.Popen")
@patch("auth.subprocess.Popen")
def test_handles_password_with_equals(self, mock_popen_cls):
# Tokens often contain '=' characters
mock_popen_cls.return_value = self._mock_popen(
"username=bot\npassword=abc=def=ghi\n"
)
user, password = create_pr.get_credentials("example.com")
user, password = auth.get_credentials("example.com")
self.assertEqual(user, "bot")
self.assertEqual(password, "abc=def=ghi")
@patch("create_pr.subprocess.Popen")
@patch("auth.subprocess.Popen")
def test_empty_output_returns_empty(self, mock_popen_cls):
mock_popen_cls.return_value = self._mock_popen("")
user, password = create_pr.get_credentials("example.com")
user, password = auth.get_credentials("example.com")
self.assertEqual(user, "")
self.assertEqual(password, "")
@patch("create_pr.subprocess.Popen")
@patch("auth.subprocess.Popen")
def test_missing_password_returns_empty(self, mock_popen_cls):
mock_popen_cls.return_value = self._mock_popen("username=admin\n")
user, password = create_pr.get_credentials("example.com")
user, password = auth.get_credentials("example.com")
self.assertEqual(user, "admin")
self.assertEqual(password, "")
@patch("create_pr.subprocess.Popen")
@patch("auth.subprocess.Popen")
def test_sends_correct_stdin(self, mock_popen_cls):
mock_proc = self._mock_popen("username=u\npassword=p\n")
mock_popen_cls.return_value = mock_proc
create_pr.get_credentials("gitea.prgs.cc")
auth.get_credentials("gitea.prgs.cc")
# Verify the correct input was sent to git credential fill
mock_proc.communicate.assert_called_once_with(
@@ -65,5 +65,27 @@ class TestGetCredentials(unittest.TestCase):
)
class TestGetAuthHeader(unittest.TestCase):
"""Test the get_auth_header function."""
@patch("auth.get_credentials", return_value=("user", "pass"))
def test_returns_basic_header(self, _cred):
header = auth.get_auth_header("example.com")
self.assertIsNotNone(header)
self.assertTrue(header.startswith("Basic "))
@patch("auth.get_credentials", return_value=("", ""))
def test_returns_none_for_missing_creds(self, _cred):
header = auth.get_auth_header("example.com")
self.assertIsNone(header)
class TestRepoApiUrl(unittest.TestCase):
def test_url_format(self):
url = auth.repo_api_url("gitea.prgs.cc", "Org", "Repo")
self.assertEqual(url, "https://gitea.prgs.cc/api/v1/repos/Org/Repo")
if __name__ == "__main__":
unittest.main()