GRM-54: Integrate tea Gitea CLI for API interactions (#68)

This commit is contained in:
2026-06-22 07:04:58 +00:00
parent b8ab4f854b
commit 28e61aa166
17 changed files with 1319 additions and 265 deletions
+63 -1
View File
@@ -1,7 +1,8 @@
#!/usr/bin/env python3
"""Project setup: install Python deps, Ansible collections, and pre-commit hooks.
Replaces the previous ``scripts/setup.sh`` with a tested Python equivalent.
Also configures the ``tea`` Gitea CLI login profile from ``.env`` so that
CI scripts and dev tools can use ``tea`` for Gitea API operations.
Usage::
@@ -10,10 +11,15 @@ Usage::
from __future__ import annotations
import os
import shutil
import subprocess # nosec B404
from pathlib import Path
import click
from dotenv import load_dotenv # pyright: ignore[reportMissingImports,reportUnknownVariableType]
load_dotenv(override=True)
def _run(cmd: list[str], bin_dir: str) -> None:
@@ -45,6 +51,59 @@ def _install_pre_commit_hooks(bin_dir: str) -> None:
_run([pre_commit, "install", "--hook-type", hook_type], bin_dir)
def _configure_tea_login() -> None:
"""Configure tea CLI login from .env if REPO_TOKEN is set.
Idempotent: if a login with the same name already exists, it is not re-added.
Skips silently if tea is not installed or REPO_TOKEN is not set.
"""
tea_bin = shutil.which("tea")
if tea_bin is None:
click.echo("tea: not installed — skipping login configuration.")
return
token = os.environ.get("REPO_TOKEN", "")
if not token:
click.echo("tea: REPO_TOKEN not set — skipping login configuration.")
return
# Derive the Gitea URL from the API URL (strip /api/v1 suffix)
api_url = os.environ.get("GRM_GITEA_API_URL", "https://git.oblachno.oblachno.fyi/api/v1")
gitea_url = api_url.replace("/api/v1", "")
login_name = "grm"
# Check if login already exists
result = subprocess.run( # nosec B603
[tea_bin, "login", "list", "--output", "simple"],
capture_output=True,
text=True,
check=False,
)
if result.returncode == 0 and login_name in result.stdout:
click.echo(f"tea: login '{login_name}' already configured.")
return
# Add login profile
click.echo(f"tea: configuring login '{login_name}' for {gitea_url}...")
add_result = subprocess.run( # nosec B603
[tea_bin, "login", "add", "--name", login_name, "--url", gitea_url, "--token", token],
capture_output=True,
text=True,
check=False,
)
if add_result.returncode != 0:
click.echo(f"tea: login configuration failed: {add_result.stderr.strip()}", err=True)
else:
# Set as default login
subprocess.run( # nosec B603
[tea_bin, "login", "default", login_name],
capture_output=True,
text=True,
check=False,
)
click.echo(f"tea: login '{login_name}' configured and set as default.")
def _verify(bin_dir: str) -> None:
"""Print versions of installed tools for verification."""
grm = str(Path(bin_dir) / "grm")
@@ -74,6 +133,9 @@ def main(bin_dir: str) -> None:
click.echo("Installing pre-commit hooks...")
_install_pre_commit_hooks(bin_dir)
click.echo("Configuring tea CLI login...")
_configure_tea_login()
click.echo("")
click.echo("Setup complete.")
click.echo("Activate the virtual environment with one of:")