112 lines
3.5 KiB
Python
112 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Test P4: scrittura Gitea (create repo, create/update file)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
|
if ROOT not in sys.path:
|
|
sys.path.insert(0, ROOT)
|
|
|
|
|
|
def _load_dotenv() -> None:
|
|
path = os.path.join(ROOT, ".env")
|
|
if not os.path.isfile(path):
|
|
return
|
|
with open(path, encoding="utf-8") as fh:
|
|
for line in fh:
|
|
line = line.strip()
|
|
if not line or line.startswith("#") or "=" not in line:
|
|
continue
|
|
k, _, v = line.partition("=")
|
|
os.environ[k.strip()] = v.strip().strip("'").strip('"')
|
|
|
|
|
|
def main() -> int:
|
|
_load_dotenv()
|
|
os.environ.setdefault("MCP_DB", "/data/loogle_mcp.db")
|
|
|
|
from app.db import init_db
|
|
from app.knowledge import gitea
|
|
from app.mcp import tools
|
|
|
|
init_db()
|
|
gitea._tokens_cache = None
|
|
|
|
if not os.environ.get("GITEA_API_TOKEN_DANIELE"):
|
|
print("ERR: token daniele assente")
|
|
return 1
|
|
|
|
claims = {"sub": "daniele", "scope": "gitea:read gitea:write knowledge:read admin"}
|
|
|
|
test_repo = "daniele/mcp-p4-test"
|
|
test_path = "docs/p4-verify.md"
|
|
test_content = "# P4 verify\n\nFile creato da test_gitea_p4.py\n"
|
|
|
|
# cleanup file if exists from prior run
|
|
try:
|
|
gitea.get_file(test_repo, test_path, username="daniele")
|
|
gitea.create_or_update_file(
|
|
test_repo,
|
|
test_path,
|
|
test_content + "\n(updated)\n",
|
|
"test p4 update",
|
|
username="daniele",
|
|
)
|
|
action = "update"
|
|
except FileNotFoundError:
|
|
# ensure repo exists - use temp repo name under daniele
|
|
repos = {r["full_name"] for r in gitea.list_repos(username="daniele")["repos"]}
|
|
if test_repo not in repos:
|
|
created = tools.call_tool(
|
|
"create_gitea_repo",
|
|
{
|
|
"name": "mcp-p4-test",
|
|
"private": True,
|
|
"description": "Repo temporaneo test P4",
|
|
},
|
|
claims,
|
|
)
|
|
print("create_repo", created["content"][0]["text"][:200])
|
|
written = tools.call_tool(
|
|
"create_or_update_file",
|
|
{
|
|
"repo": test_repo,
|
|
"path": test_path,
|
|
"content": test_content,
|
|
"message": "test p4 create",
|
|
"reindex": False,
|
|
},
|
|
claims,
|
|
)
|
|
payload = json.loads(written["content"][0]["text"])
|
|
action = payload.get("action")
|
|
print("write", action, payload.get("path"))
|
|
|
|
read = gitea.get_file(test_repo, test_path, username="daniele")
|
|
assert "P4 verify" in read.get("content", ""), read
|
|
print("read_ok", read["path"])
|
|
|
|
for user, repo in (("davide", "davide/progetti"), ("luca", "luca/progetti")):
|
|
token_key = f"GITEA_API_TOKEN_{user.upper()}"
|
|
if not os.environ.get(token_key):
|
|
print(f"SKIP {user}: no token")
|
|
continue
|
|
gitea._tokens_cache = None
|
|
listed = gitea.list_repos(username=user)
|
|
names = {r["full_name"] for r in listed["repos"]}
|
|
if repo not in names:
|
|
raise RuntimeError(f"repo mancante per {user}: {repo}")
|
|
print(f"{user}_repo_ok", repo)
|
|
|
|
print("OK P4: create_or_update_file + repos davide/luca/progetti")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|